agent-council 0.2.0 → 0.3.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.
@@ -0,0 +1,221 @@
1
+ # frozen_string_literal: true
2
+
3
+ # AgentCouncil::Harnesses::CodexHarness — all knowledge specific to the
4
+ # OpenAI Codex CLI harness: how to invoke `codex exec` for one agent,
5
+ # with the shared scaffolding, and how to read
6
+ # its --json event stream afterward.
7
+ #
8
+ # AgentCouncil is harness-agnostic and finds this module through
9
+ # AgentCouncil::Harnesses (lib/agent_council/harnesses.rb), by its NAME.
10
+ # Authentication and billing are left to Codex: an agent bills however
11
+ # codex would at the user's terminal (ChatGPT plan or API key).
12
+
13
+ require 'json'
14
+ require_relative 'scaffold'
15
+
16
+ module AgentCouncil
17
+ module Harnesses
18
+ module CodexHarness
19
+ NAME = 'codex'
20
+
21
+ # Invocation for one agent. The substantive prompt is not part of
22
+ # argv; it is piped to stdin ('-'). provider is not passed: like
23
+ # Claude Code's, an entry's provider only labels the agent, and Codex
24
+ # uses the model_provider its own config names.
25
+ #
26
+ # No --sandbox or approval flags: the user's sandbox_mode applies, as
27
+ # in an interactive session, along with the rest of their config.toml
28
+ # (MCP servers and plugins included) and the repository's AGENTS.md.
29
+ # Their approval_policy doesn't: codex exec never asks, so a command
30
+ # that needs approval is refused. Under workspace-write,
31
+ # Codex lets commands write $TMPDIR, which the scaffolding sends
32
+ # agents to; under read-only nothing is writable.
33
+ #
34
+ # developer_instructions replaces any the user's config sets; Codex
35
+ # has no way to append to it.
36
+ module_function def argv(_provider, model)
37
+ [
38
+ NAME, 'exec',
39
+ '--json', # JSONL event stream on stdout
40
+ '--ephemeral', # no session files saved; subagents then fail to spawn
41
+ '--skip-git-repo-check', # run anywhere, as pi and claude agents do
42
+ '--color', 'never',
43
+ '--model', model,
44
+ '-c', "developer_instructions=#{toml_string(Harnesses.scaffold_text)}",
45
+ '-' # read the prompt from stdin
46
+ ]
47
+ end
48
+
49
+ # A TOML basic string (JSON's string escapes are valid TOML ones), so
50
+ # -c reads the value as the string itself, not as TOML to parse.
51
+ module_function def toml_string(text)
52
+ JSON.generate(text)
53
+ end
54
+
55
+ # --- event stream reading ---
56
+
57
+ # The events of a --json stream, in order. Unparseable lines and
58
+ # non-objects are skipped.
59
+ module_function def events(jsonl)
60
+ jsonl.each_line.filter_map do |line|
61
+ event =
62
+ begin
63
+ JSON.parse(line)
64
+ rescue JSON::ParserError
65
+ nil
66
+ end
67
+ event if event.is_a?(Hash)
68
+ end
69
+ end
70
+
71
+ # The completed items of the given type, in order.
72
+ module_function def completed_items(jsonl, type)
73
+ events(jsonl).filter_map do |event|
74
+ item = event['item'] if event['type'] == 'item.completed'
75
+ item if item.is_a?(Hash) && item['type'] == type
76
+ end
77
+ end
78
+
79
+ # The agent's messages (its text between and after tool calls), in order.
80
+ module_function def assistant_messages(jsonl)
81
+ completed_items(jsonl, 'agent_message')
82
+ end
83
+
84
+ # The event that ended the agent's turn: turn.completed or
85
+ # turn.failed. nil if there is none, e.g. for an agent that was killed.
86
+ module_function def turn_end(jsonl)
87
+ events(jsonl).reverse_each.detect { |event| %w[turn.completed turn.failed].include?(event['type']) }
88
+ end
89
+
90
+ # The final response: the last agent message with text, for an agent
91
+ # whose turn completed. Earlier messages narrate its tool use.
92
+ module_function def final_response(jsonl)
93
+ return nil unless turn_end(jsonl)&.fetch('type') == 'turn.completed'
94
+
95
+ message = assistant_messages(jsonl).reverse_each.detect { |m| text_of(m) }
96
+ message && text_of(message)
97
+ end
98
+
99
+ # A message's text, or nil if it has none.
100
+ module_function def text_of(item)
101
+ text = item['text']
102
+ text if text.is_a?(String) && !text.strip.empty?
103
+ end
104
+
105
+ # Token usage as AgentCouncil expects it (see Harnesses), from the
106
+ # turn.completed event, which totals the turn's requests. Codex's
107
+ # input_tokens is all input, OpenAI's convention, so the cached reads
108
+ # and writes it reports are subtracted from it; reasoning tokens are
109
+ # part of output_tokens. Codex prices nothing, so there is no 'cost'.
110
+ # nil when the turn didn't complete or reports no usage.
111
+ module_function def usage(jsonl)
112
+ turn = turn_end(jsonl)
113
+ usage = turn['usage'] if turn && turn['type'] == 'turn.completed'
114
+ return nil unless usage.is_a?(Hash)
115
+
116
+ cache_read = count(usage['cached_input_tokens'])
117
+ cache_write = count(usage['cache_write_input_tokens'])
118
+ totals = { 'input' => [count(usage['input_tokens']) - cache_read - cache_write, 0].max,
119
+ 'output' => count(usage['output_tokens']),
120
+ 'cache_read' => cache_read,
121
+ 'cache_write' => cache_write }
122
+ totals.values.all?(&:zero?) ? nil : totals
123
+ end
124
+
125
+ module_function def count(value)
126
+ value.is_a?(Numeric) && value.positive? ? value : 0
127
+ end
128
+
129
+ # Why the agent produced no usable report, or nil. A failed turn ends
130
+ # with turn.failed, whose error says why (e.g. a model the account
131
+ # can't use); a completed one with no text is reported as such.
132
+ module_function def error_message(jsonl)
133
+ turn = turn_end(jsonl)
134
+ return 'the stream has no turn.completed or turn.failed event' unless turn
135
+ return failure_reason(jsonl) || 'the turn failed and gave no reason' if turn['type'] == 'turn.failed'
136
+
137
+ 'the agent gave no final message' unless final_response(jsonl)
138
+ end
139
+
140
+ # The reason a failed turn gives: its turn.failed error message, else
141
+ # the last top-level error event's. nil if neither gives one.
142
+ module_function def failure_reason(jsonl)
143
+ all = events(jsonl)
144
+ failed = all.reverse_each.detect { |event| event['type'] == 'turn.failed' }
145
+ error = failed['error'] if failed
146
+ reason = error['message'] if error.is_a?(Hash)
147
+ return reason.strip if reason.is_a?(String) && !reason.strip.empty?
148
+
149
+ nonblank(all.filter_map { |event| event['message'] if event['type'] == 'error' }).last
150
+ end
151
+
152
+ # Why an agent that exited nonzero failed. Codex explains a failed
153
+ # turn in the stream, with stderr usually empty, so that comes first;
154
+ # then stderr (e.g. for a bad flag or a crash).
155
+ module_function def exit_detail(stdout, stderr)
156
+ reason = failure_reason(stdout)
157
+ return reason if reason
158
+
159
+ stderr.lines.map(&:strip).reject(&:empty?).first || 'no error output'
160
+ end
161
+
162
+ # Warnings about an agent, failed or not, each listing at most
163
+ # MAX_LISTED_ERRORS distinct entries, each cut to MAX_ERROR_LENGTH
164
+ # characters, so they stay short:
165
+ # - the error items Codex reports without failing the turn (e.g.
166
+ # "Model metadata for `x` not found"), which may explain a weak
167
+ # report;
168
+ # - the MCP tool calls that failed, such as those refused because
169
+ # codex exec never asks for approval ("MCP tool call requires
170
+ # approval, but approval policy is never"), whose results the
171
+ # report may lack.
172
+ module_function def warnings(jsonl)
173
+ errors = nonblank(completed_items(jsonl, 'error').map { |item| item['message'] }).uniq
174
+ calls = failed_calls(completed_items(jsonl, 'mcp_tool_call').select do |item|
175
+ item['status'] == 'failed'
176
+ end)
177
+ warnings = []
178
+ unless errors.empty?
179
+ warnings << listing("Codex reported #{errors.size == 1 ? 'an error' : 'errors'}",
180
+ errors)
181
+ end
182
+ unless calls.empty?
183
+ warnings << listing("Codex MCP tool call#{'s' unless calls.size == 1} failed",
184
+ calls)
185
+ end
186
+ warnings
187
+ end
188
+
189
+ # A warning: label, then entries, at most MAX_LISTED_ERRORS of them.
190
+ module_function def listing(label, entries)
191
+ listed = entries.first(MAX_LISTED_ERRORS).map { |entry| shorten(entry, MAX_ERROR_LENGTH) }
192
+ listed << "and #{entries.size - listed.size} more" if entries.size > listed.size
193
+ "#{label}: #{listed.join('; ')}"
194
+ end
195
+
196
+ # Failed MCP tool calls as distinct "server.tool (reason)" entries.
197
+ module_function def failed_calls(calls)
198
+ calls.map do |call|
199
+ server, name = nonblank([call['server']]).first, nonblank([call['tool']]).first
200
+ tool = name ? [server, name].compact.join('.') : 'unknown tool'
201
+ error = call['error']
202
+ reason = error['message'] if error.is_a?(Hash)
203
+ reason.is_a?(String) && !reason.strip.empty? ? "#{tool} (#{reason.strip})" : tool
204
+ end.uniq
205
+ end
206
+
207
+ MAX_LISTED_ERRORS = 5
208
+ MAX_ERROR_LENGTH = 200
209
+
210
+ # text, cut to max characters with "..." if longer.
211
+ module_function def shorten(text, max)
212
+ text.length > max ? "#{text[0, max]}..." : text
213
+ end
214
+
215
+ # The values that are nonblank strings, stripped.
216
+ module_function def nonblank(values)
217
+ values.grep(String).map(&:strip).reject(&:empty?)
218
+ end
219
+ end
220
+ end
221
+ end
@@ -19,8 +19,17 @@ module AgentCouncil
19
19
  # Analysis toolset (pi's built-in tool names): edit/write (and the
20
20
  # Windows-only powershell) are withheld. bash, a full shell, stays for
21
21
  # investigation (git log/status, dependency inspection, ...); only the
22
- # scaffolding text asks the model to use it read-only.
23
- TOOLS = 'read,grep,find,ls,bash'
22
+ # scaffolding text asks the model to use it read-only. The rest give
23
+ # agents the MCP tools pi gives an interactive session, however a
24
+ # server exposes them: codemode for the default exposure (a server's
25
+ # tools are called from a codemode script, which reaches only the
26
+ # tools listed here), mcp__* for servers with direct exposure,
27
+ # tool_search for servers with deferred exposure (it loads only MCP
28
+ # tools, not withheld built-in ones), and pi's MCP resource tools,
29
+ # which --tools would drop once it names an mcp__ entry.
30
+ TOOLS = %w[read grep find ls bash
31
+ codemode mcp__* tool_search
32
+ list_mcp_resources list_mcp_resource_templates read_mcp_resource].join(',')
24
33
 
25
34
  # Invocation for one agent. The substantive prompt is not part of argv;
26
35
  # it is piped to stdin (byte-identical for every analyst; the
@@ -31,7 +40,6 @@ module AgentCouncil
31
40
  '-p', # non-interactive: process prompt and exit
32
41
  '--mode', 'json', # JSONL event stream on stdout
33
42
  '--no-session', # ephemeral: no session state between agents
34
- '--no-mcp', # built-in MCP off: configured servers don't start
35
43
  '--tools', TOOLS,
36
44
  '--provider', provider,
37
45
  '--model', model,
@@ -44,6 +52,12 @@ module AgentCouncil
44
52
  # The assistant message from a single pi JSONL line, or nil if the line
45
53
  # is not an assistant message_end event.
46
54
  module_function def assistant_message_from_line(line)
55
+ message_from_line(line, %w[assistant])
56
+ end
57
+
58
+ # The message with one of the given roles from a single pi JSONL line,
59
+ # or nil if the line is not a message_end event for one.
60
+ module_function def message_from_line(line, roles)
47
61
  event =
48
62
  begin
49
63
  JSON.parse(line)
@@ -55,7 +69,7 @@ module AgentCouncil
55
69
  return nil unless event['type'] == 'message_end'
56
70
 
57
71
  message = event['message']
58
- return nil unless message && message['role'] == 'assistant'
72
+ return nil unless message.is_a?(Hash) && roles.include?(message['role'])
59
73
 
60
74
  message
61
75
  end
@@ -92,12 +106,14 @@ module AgentCouncil
92
106
  'cache_read' => 'cacheRead', 'cache_write' => 'cacheWrite' }.freeze
93
107
 
94
108
  # Token usage summed over all assistant messages (each is a separate
95
- # model request), as AgentCouncil expects it: integer 'input', 'output',
96
- # 'cache_read', and 'cache_write', plus 'cost' in US dollars when pi
97
- # priced the usage. nil when the stream reports no usage, as some
98
- # providers don't.
109
+ # model request) and tool results, as AgentCouncil expects it: integer
110
+ # 'input', 'output', 'cache_read', and 'cache_write', plus 'cost' in US
111
+ # dollars when pi priced the usage. A tool result has usage when the
112
+ # tool called a model itself, e.g. a codemode script's models.classify
113
+ # call. nil when the stream reports no usage, as some providers don't.
99
114
  module_function def usage(jsonl)
100
- usages = assistant_messages(jsonl).map { |m| m['usage'] }.grep(Hash)
115
+ messages = jsonl.each_line.filter_map { |line| message_from_line(line, %w[assistant toolResult]) }
116
+ usages = messages.map { |m| m['usage'] }.grep(Hash)
101
117
  totals = USAGE_KEYS.transform_values { |key| usages.sum { |u| count(u[key]) } }
102
118
  return nil if totals.values.all?(&:zero?)
103
119
 
@@ -138,7 +154,9 @@ module AgentCouncil
138
154
  stderr.lines.map(&:strip).reject(&:empty?).first || 'no error output'
139
155
  end
140
156
 
141
- # Warnings about an agent that succeeded; pi's stream has none to give.
157
+ # Warnings about an agent that succeeded: none. A failed tool call,
158
+ # MCP calls included, is an ordinary tool error the agent sees, not a
159
+ # refusal (DESIGN.md §11).
142
160
  module_function def warnings(_jsonl)
143
161
  []
144
162
  end
@@ -21,6 +21,10 @@ module AgentCouncil
21
21
  configuration file. Do not change git state, install or remove
22
22
  software, modify user or system configuration, or start persistent
23
23
  services.
24
+ - Do not act outside this machine, with any tool (MCP tools
25
+ included): do not post, comment, send messages, or create or
26
+ change issues, tickets, records, or other remote data. Reading
27
+ remote information is fine.
24
28
  - Do not run test suites, builds, coverage tools, application servers,
25
29
  or database tasks unless the user's prompt explicitly permits it:
26
30
  other agents may be running concurrently and such commands can collide
@@ -28,10 +28,11 @@
28
28
 
29
29
  require_relative 'harnesses/pi_harness'
30
30
  require_relative 'harnesses/claude_code_harness'
31
+ require_relative 'harnesses/codex_harness'
31
32
 
32
33
  module AgentCouncil
33
34
  module Harnesses
34
- BY_NAME = [PiHarness, ClaudeCodeHarness].to_h { |harness| [harness::NAME, harness] }.freeze
35
+ BY_NAME = [PiHarness, ClaudeCodeHarness, CodexHarness].to_h { |harness| [harness::NAME, harness] }.freeze
35
36
 
36
37
  # The harness of a report or run.json entry without a 'harness' key:
37
38
  # agent-council writes one in each, so these are hand-edited or older
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AgentCouncil
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
@@ -14,8 +14,8 @@ module AgentCouncil
14
14
  # whose rescue calls #shutdown: it sends every agent SIGTERM, waits up to
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
- # groups of their own, which these signals don't reach; pi and Claude
18
- # Code stop them on SIGTERM, but an agent that is SIGKILLed can't, so
17
+ # groups of their own, which these signals don't reach; pi, Claude
18
+ # Code, and Codex stop them on SIGTERM, but an agent that is SIGKILLed can't, so
19
19
  # they may keep running. A second trapped signal other
20
20
  # 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
data/lib/agent_council.rb CHANGED
@@ -135,7 +135,7 @@ module AgentCouncil
135
135
  # CLAUDE_CODE_TMPDIR, the base under which Claude Code's sandbox makes
136
136
  # the writable TMPDIR it gives commands. Without the latter, a user's
137
137
  # own CLAUDE_CODE_TMPDIR would put that dir outside the scratch dir,
138
- # shared and never cleaned up. pi ignores it.
138
+ # shared and never cleaned up. pi and Codex ignore it.
139
139
  module_function def agent_env(scratch_dir)
140
140
  { 'TMPDIR' => scratch_dir, 'CLAUDE_CODE_TMPDIR' => scratch_dir }
141
141
  end
@@ -236,8 +236,9 @@ module AgentCouncil
236
236
  end
237
237
 
238
238
  # The harness's warnings about an agent: what the harness kept it
239
- # from doing (see ClaudeCodeHarness.warnings), which a report may lack,
240
- # or which may be why the agent failed. Advisory only: failing to read them
239
+ # from doing, or errors it hit without failing (see
240
+ # ClaudeCodeHarness.warnings and CodexHarness.warnings), which a report
241
+ # may lack, or which may be why the agent failed. Advisory only: failing to read them
241
242
  # must not fail an agent whose report is saved, so that becomes a warning
242
243
  # too.
243
244
  # Each is a plain_line, so wherever it goes (console, report, run.json,
@@ -722,6 +723,14 @@ module AgentCouncil
722
723
  costs.all?(Numeric) ? total.merge('cost' => costs.sum.round(6)) : total
723
724
  end
724
725
 
726
+ # The summed cost of the usages whose cost is known, and how many those
727
+ # are, for when sum_usage leaves the cost out because some are unknown
728
+ # (e.g. a Codex agent's, which Codex doesn't price). [nil, 0] if none is.
729
+ module_function def known_cost(usages)
730
+ costs = usages.grep(Hash).map { |u| u['cost'] }.grep(Numeric)
731
+ [costs.empty? ? nil : costs.sum.round(6), costs.size]
732
+ end
733
+
725
734
  # The run dir's machine-readable record of its analysts and consolidations.
726
735
  RUN_FILENAME = 'run.json'
727
736
 
@@ -743,14 +752,17 @@ module AgentCouncil
743
752
  end
744
753
 
745
754
  # Writes the run dir's run.json: every analyst's and consolidation's
746
- # entry (see run_entry), their total usage, and how many entries it covers
747
- # (those that reported usage). Replaced atomically, so a
755
+ # entry (see run_entry), their total usage, how many entries it covers
756
+ # (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 atomically, so a
748
758
  # reader or a Ctrl-C never sees it half written. A failure to write it is
749
759
  # only warned about: the reports are what matter.
750
760
  module_function def write_run_file(run_dir, analysts, consolidations)
751
761
  usages = (analysts + consolidations).map { |entry| entry['usage'] }
762
+ cost, cost_runs = known_cost(usages)
752
763
  data = { 'analysts' => analysts, 'consolidations' => consolidations,
753
- 'total_usage' => sum_usage(usages), 'total_usage_runs' => usages.grep(Hash).size }
764
+ 'total_usage' => sum_usage(usages), 'total_usage_runs' => usages.grep(Hash).size,
765
+ 'known_cost' => cost, 'known_cost_runs' => cost_runs }
754
766
  path = File.join(run_dir, RUN_FILENAME)
755
767
  temp = "#{path}.#{Process.pid}.tmp"
756
768
  File.write(temp, "#{JSON.pretty_generate(data)}\n")
@@ -806,13 +818,20 @@ module AgentCouncil
806
818
  end
807
819
 
808
820
  # "; <total usage>" for a summary line, noting how many of the agents it
809
- # covers when some reported no usage; "" when none reported any.
821
+ # covers when some reported no usage; "" when none reported any. When
822
+ # only some agents' cost is known, the total leaves the cost out, so the
823
+ # known agents' cost follows, with how many they are.
810
824
  module_function def usage_summary(usages)
811
- total = format_usage(sum_usage(usages))
825
+ sum = sum_usage(usages)
826
+ total = format_usage(sum)
812
827
  return '' unless total
813
828
 
814
829
  reported = usages.grep(Hash).size
815
- "; #{total}#{" (from #{reported} of #{usages.size} agents)" if reported < usages.size}"
830
+ text = "; #{total}#{" (from #{reported} of #{usages.size} agents)" if reported < usages.size}"
831
+ cost, cost_runs = known_cost(usages)
832
+ return text if sum.key?('cost') || cost.nil? || !cost.positive?
833
+
834
+ "#{text}; #{format_cost(cost)} for the #{cost_runs} of #{usages.size} agents with a known cost"
816
835
  end
817
836
 
818
837
  # East Asian wide and fullwidth characters (CJK, Hangul, fullwidth forms,
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: agent-council
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith R. Bennett
@@ -11,7 +11,7 @@ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: |-
13
13
  AgentCouncil runs the same prompt through several AI coding agents on your
14
- project, under the pi or Claude Code harness, with any mix of harnesses,
14
+ project, under the pi, Claude Code, or Codex harness, with any mix of harnesses,
15
15
  providers, and models, and saves each agent's final response as a
16
16
  Markdown report. A consolidating agent then combines the reports into a
17
17
  findings report of consensus findings, unique findings, and
@@ -50,6 +50,7 @@ files:
50
50
  - lib/agent_council.rb
51
51
  - lib/agent_council/harnesses.rb
52
52
  - lib/agent_council/harnesses/claude_code_harness.rb
53
+ - lib/agent_council/harnesses/codex_harness.rb
53
54
  - lib/agent_council/harnesses/pi_harness.rb
54
55
  - lib/agent_council/harnesses/scaffold.rb
55
56
  - lib/agent_council/pi_models.rb
@@ -64,7 +65,7 @@ licenses:
64
65
  metadata:
65
66
  source_code_uri: https://github.com/keithrbennett/agent-council
66
67
  changelog_uri: https://github.com/keithrbennett/agent-council/blob/main/RELEASE_NOTES.md
67
- documentation_uri: https://github.com/keithrbennett/agent-council/tree/v0.2.0
68
+ documentation_uri: https://github.com/keithrbennett/agent-council/tree/v0.3.0
68
69
  bug_tracker_uri: https://github.com/keithrbennett/agent-council/issues
69
70
  rubygems_mfa_required: 'true'
70
71
  rdoc_options: []