agent-cli-runtime 0.2.0 → 0.2.5
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/CHANGELOG.md +102 -15
- data/README.md +54 -18
- data/agent-cli-runtime.gemspec +8 -7
- data/lib/agent_cli_runtime/error_extractors.rb +154 -0
- data/lib/agent_cli_runtime/opencode/inspection.rb +7 -1
- data/lib/agent_cli_runtime/opencode/overlay.rb +41 -120
- data/lib/agent_cli_runtime/opencode/permissions.rb +144 -0
- data/lib/agent_cli_runtime/opencode/probe.rb +86 -20
- data/lib/agent_cli_runtime/opencode/result_parser.rb +46 -21
- data/lib/agent_cli_runtime/profile.rb +46 -17
- data/lib/agent_cli_runtime/profiles.rb +194 -5
- data/lib/agent_cli_runtime/runtime.rb +104 -11
- data/lib/agent_cli_runtime/usage_extractors.rb +210 -32
- data/lib/agent_cli_runtime/values.rb +81 -11
- data/lib/agent_cli_runtime/version.rb +1 -1
- data/lib/agent_cli_runtime.rb +6 -0
- metadata +12 -9
|
@@ -13,6 +13,7 @@ module AgentCliRuntime
|
|
|
13
13
|
argv = [ request.executable || profile.bin ]
|
|
14
14
|
prompt_style = profile.prompt_style
|
|
15
15
|
argv << profile.headless_flag
|
|
16
|
+
argv << profile.prompt_argument if profile.prompt_argument
|
|
16
17
|
argv << request.prompt if prompt_style == :headless_flag_value
|
|
17
18
|
|
|
18
19
|
argv.concat(
|
|
@@ -46,12 +47,23 @@ module AgentCliRuntime
|
|
|
46
47
|
end
|
|
47
48
|
argv.concat(request.trusted_cli_arguments)
|
|
48
49
|
argv.concat(profile.output_format_flags) if request.include_output_format
|
|
49
|
-
|
|
50
|
-
prompt_style
|
|
50
|
+
stdin_data =
|
|
51
|
+
case prompt_style
|
|
52
|
+
when :positional
|
|
53
|
+
argv << request.prompt
|
|
54
|
+
nil
|
|
55
|
+
when :headless_flag_value
|
|
56
|
+
nil
|
|
57
|
+
when :stdin
|
|
58
|
+
argv << "-"
|
|
59
|
+
request.prompt
|
|
60
|
+
when :piped_stdin
|
|
61
|
+
request.prompt
|
|
62
|
+
end
|
|
51
63
|
|
|
52
64
|
CompiledInvocation.new(
|
|
53
65
|
argv: request.command_prefix + argv,
|
|
54
|
-
stdin_data
|
|
66
|
+
stdin_data:,
|
|
55
67
|
provider: profile.name,
|
|
56
68
|
launcher_identity: profile.launcher_identity,
|
|
57
69
|
capability_evidence: evidence
|
|
@@ -107,6 +119,51 @@ module AgentCliRuntime
|
|
|
107
119
|
end
|
|
108
120
|
end
|
|
109
121
|
|
|
122
|
+
# A provider failure seen mid-stream, normalized to
|
|
123
|
+
# {kind:, provider:, status_code:, message:}, or nil when the event is
|
|
124
|
+
# clean. Callers use this to tell a provider-side stop (quota, credit
|
|
125
|
+
# ceiling, rate limit, model output truncation) apart from an agent that
|
|
126
|
+
# genuinely produced nothing: several CLIs report the former on the stream
|
|
127
|
+
# and still exit zero.
|
|
128
|
+
def extract_provider_error(profile, event)
|
|
129
|
+
resolved = Profiles.resolve(profile)
|
|
130
|
+
extracted = resolved.extract_error_event(event)
|
|
131
|
+
return nil if extracted.nil?
|
|
132
|
+
|
|
133
|
+
kind, text = normalize_extracted_error(extracted)
|
|
134
|
+
return nil if text.nil? || text.strip.empty?
|
|
135
|
+
|
|
136
|
+
{
|
|
137
|
+
kind: kind,
|
|
138
|
+
provider: resolved.name,
|
|
139
|
+
status_code: status_code_from(text),
|
|
140
|
+
message: Redactor.diagnostic(text)
|
|
141
|
+
}.freeze
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def normalize_extracted_error(extracted)
|
|
145
|
+
return [ extracted.kind, extracted.message ] if extracted.is_a?(ExtractedFailure)
|
|
146
|
+
return [ failure_kind_from(extracted), extracted ] if extracted.is_a?(String)
|
|
147
|
+
|
|
148
|
+
[ nil, nil ]
|
|
149
|
+
end
|
|
150
|
+
private_class_method :normalize_extracted_error
|
|
151
|
+
|
|
152
|
+
def failure_kind_from(text)
|
|
153
|
+
status = status_code_from(text)
|
|
154
|
+
return :provider_limit if status == 402
|
|
155
|
+
return :rate_limited if status == 429
|
|
156
|
+
|
|
157
|
+
normalized = text.to_s.downcase
|
|
158
|
+
return :rate_limited if normalized.match?(/\brate[\s_-]*limit(?:ed|s)?\b/)
|
|
159
|
+
return :provider_limit if normalized.match?(
|
|
160
|
+
/\b(?:quota|billing|credits?|tokens?[\s_-]*limit|usage[\s_-]*limit)\b/
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
:provider_error
|
|
164
|
+
end
|
|
165
|
+
private_class_method :failure_kind_from
|
|
166
|
+
|
|
110
167
|
def observe(profile, result)
|
|
111
168
|
resolved = Profiles.resolve(profile)
|
|
112
169
|
raw = result.is_a?(Hash) ? result : {}
|
|
@@ -267,22 +324,58 @@ module AgentCliRuntime
|
|
|
267
324
|
end
|
|
268
325
|
private_class_method :tool_csv
|
|
269
326
|
|
|
327
|
+
# Providers prefix the status onto the text ("402: {...}") or carry it in
|
|
328
|
+
# the embedded payload. Either is enough to classify without matching on
|
|
329
|
+
# human-readable wording, which differs per provider and changes freely.
|
|
330
|
+
def status_code_from(text)
|
|
331
|
+
value = text[/\A\s*(\d{3})\s*:/, 1] ||
|
|
332
|
+
text[/"(?:code|http_status)"\s*:\s*(\d{3})\b/, 1] ||
|
|
333
|
+
text[/\Aunexpected status (\d{3})\b/, 1]
|
|
334
|
+
code = value.to_i
|
|
335
|
+
code.between?(100, 599) ? code : nil
|
|
336
|
+
end
|
|
337
|
+
private_class_method :status_code_from
|
|
338
|
+
|
|
270
339
|
def normalize_usage(usage)
|
|
271
340
|
return nil unless usage.is_a?(Hash)
|
|
272
341
|
|
|
273
|
-
|
|
274
|
-
output = usage.key?(:output) ? usage[:output] : usage["output"]
|
|
275
|
-
cached = usage.key?(:cached) ? usage[:cached] : usage["cached"]
|
|
276
|
-
model = usage.key?(:model) ? usage[:model] : usage["model"]
|
|
342
|
+
value = ->(key) { usage.key?(key) ? usage[key] : usage[key.to_s] }
|
|
277
343
|
{
|
|
278
|
-
input:
|
|
279
|
-
output:
|
|
280
|
-
cached:
|
|
281
|
-
|
|
344
|
+
input: normalized_count(value.call(:input)),
|
|
345
|
+
output: normalized_count(value.call(:output)),
|
|
346
|
+
cached: normalized_count(value.call(:cached)),
|
|
347
|
+
cache_read: normalized_count(value.call(:cache_read)),
|
|
348
|
+
cache_write: normalized_count(value.call(:cache_write)),
|
|
349
|
+
reasoning: normalized_count(value.call(:reasoning)),
|
|
350
|
+
input_includes_cache_read: normalized_boolean(value.call(:input_includes_cache_read)),
|
|
351
|
+
input_includes_cache_write: normalized_boolean(value.call(:input_includes_cache_write)),
|
|
352
|
+
output_includes_reasoning: normalized_boolean(value.call(:output_includes_reasoning)),
|
|
353
|
+
model: value.call(:model)&.to_s&.dup&.freeze,
|
|
354
|
+
# Carried as a float: this is money, and normalized_count would round
|
|
355
|
+
# a fraction-of-a-cent charge to zero.
|
|
356
|
+
provider_reported_cost: normalized_cost(value.call(:provider_reported_cost))
|
|
282
357
|
}.freeze
|
|
283
358
|
end
|
|
284
359
|
private_class_method :normalize_usage
|
|
285
360
|
|
|
361
|
+
def normalized_cost(value)
|
|
362
|
+
return nil unless value.is_a?(Numeric)
|
|
363
|
+
|
|
364
|
+
cost = value.to_f
|
|
365
|
+
cost if cost >= 0 && cost.finite?
|
|
366
|
+
end
|
|
367
|
+
private_class_method :normalized_cost
|
|
368
|
+
|
|
369
|
+
def normalized_count(value)
|
|
370
|
+
value.nil? ? nil : [ value.to_i, 0 ].max
|
|
371
|
+
end
|
|
372
|
+
private_class_method :normalized_count
|
|
373
|
+
|
|
374
|
+
def normalized_boolean(value)
|
|
375
|
+
value if value == true || value == false
|
|
376
|
+
end
|
|
377
|
+
private_class_method :normalized_boolean
|
|
378
|
+
|
|
286
379
|
def unsupported!(profile, capability, diagnostic)
|
|
287
380
|
evidence = unsupported_evidence(profile, capability, diagnostic)
|
|
288
381
|
raise UnsupportedCapability.new(
|
|
@@ -7,9 +7,9 @@ module AgentCliRuntime
|
|
|
7
7
|
|
|
8
8
|
usage = usage_hash(event)
|
|
9
9
|
if event["type"] == "result" && usage
|
|
10
|
-
usage_result(event, usage, model_from(event))
|
|
10
|
+
usage_result(event, usage, model_from(event), provider: :claude)
|
|
11
11
|
elsif event["type"] == "stream_event" && usage
|
|
12
|
-
usage_result(event, usage, model_from(event))
|
|
12
|
+
usage_result(event, usage, model_from(event), provider: :claude)
|
|
13
13
|
end
|
|
14
14
|
end
|
|
15
15
|
|
|
@@ -17,21 +17,112 @@ module AgentCliRuntime
|
|
|
17
17
|
next nil unless event.is_a?(Hash)
|
|
18
18
|
|
|
19
19
|
usage = usage_hash(event)
|
|
20
|
-
usage_result(event, usage, model_from(event)) if usage
|
|
20
|
+
usage_result(event, usage, model_from(event), provider: :codex) if usage
|
|
21
21
|
end
|
|
22
22
|
|
|
23
23
|
PI = lambda do |event|
|
|
24
24
|
next nil unless event.is_a?(Hash)
|
|
25
25
|
|
|
26
26
|
usage = usage_hash(event)
|
|
27
|
-
usage_result(event, usage, model_from(event)) if usage
|
|
27
|
+
usage_result(event, usage, model_from(event), provider: :pi) if usage
|
|
28
28
|
end
|
|
29
29
|
|
|
30
30
|
GROK = lambda do |event|
|
|
31
31
|
next nil unless event.is_a?(Hash)
|
|
32
32
|
|
|
33
33
|
usage = usage_hash(event)
|
|
34
|
-
usage_result(event, usage, model_from(event)) if usage
|
|
34
|
+
usage_result(event, usage, model_from(event), provider: :grok) if usage
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Muse reports one terminal `run.usage` event whose payload carries a
|
|
38
|
+
# per-model breakdown (`models: [{model:, input_tokens:, ...}]`) instead
|
|
39
|
+
# of a single usage hash. Sum the breakdown; cache and reasoning
|
|
40
|
+
# inclusion in the headline counts is not documented, so both stay
|
|
41
|
+
# unknown rather than inferred.
|
|
42
|
+
MUSE = lambda do |event|
|
|
43
|
+
next nil unless event.is_a?(Hash) && event["payload_type"] == "run.usage"
|
|
44
|
+
|
|
45
|
+
payload = event["payload"]
|
|
46
|
+
next nil unless payload.is_a?(Hash)
|
|
47
|
+
|
|
48
|
+
models = payload["models"]
|
|
49
|
+
next nil unless models.is_a?(Array)
|
|
50
|
+
|
|
51
|
+
entries = models.select { |entry| entry.is_a?(Hash) }
|
|
52
|
+
next nil if entries.empty?
|
|
53
|
+
|
|
54
|
+
names = entries.map { |entry| entry["model"].to_s }.reject(&:empty?).uniq
|
|
55
|
+
{
|
|
56
|
+
input: UsageExtractors.muse_total(entries, "input_tokens"),
|
|
57
|
+
output: UsageExtractors.muse_total(entries, "output_tokens"),
|
|
58
|
+
cached: UsageExtractors.muse_total(entries, "cached_tokens"),
|
|
59
|
+
cache_read: nil,
|
|
60
|
+
cache_write: nil,
|
|
61
|
+
reasoning: UsageExtractors.muse_total(entries, "reasoning_tokens"),
|
|
62
|
+
input_includes_cache_read: nil,
|
|
63
|
+
input_includes_cache_write: nil,
|
|
64
|
+
output_includes_reasoning: nil,
|
|
65
|
+
model: names.length == 1 ? names.first : nil,
|
|
66
|
+
provider_reported_cost: nil
|
|
67
|
+
}
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Antigravity CLI (`agy`) emits per-step usage on `step_update` events and
|
|
71
|
+
# cumulative run usage on the terminal `result` event under `result.usage`.
|
|
72
|
+
# Captured streams show `input_tokens + output_tokens == total_tokens` with
|
|
73
|
+
# `thinking_tokens <= output_tokens`, so `output_includes_reasoning` is true
|
|
74
|
+
# when thinking tokens are reported; cache-read inclusion in `input_tokens`
|
|
75
|
+
# stays unknown (`nil`). Zero-turn error results (such as an empty prompt)
|
|
76
|
+
# emit zero counters without a model call and return nil.
|
|
77
|
+
AGY = lambda do |event|
|
|
78
|
+
next nil unless event.is_a?(Hash)
|
|
79
|
+
|
|
80
|
+
kind = event["event"]
|
|
81
|
+
envelope = case kind
|
|
82
|
+
when "result"
|
|
83
|
+
event["result"]
|
|
84
|
+
when "step_update"
|
|
85
|
+
event["step_update"]
|
|
86
|
+
end
|
|
87
|
+
next nil unless envelope.is_a?(Hash)
|
|
88
|
+
|
|
89
|
+
usage = envelope["usage"]
|
|
90
|
+
next nil unless usage.is_a?(Hash) && !usage.empty?
|
|
91
|
+
|
|
92
|
+
if kind == "result" &&
|
|
93
|
+
envelope["status"].to_s.upcase != "SUCCESS" &&
|
|
94
|
+
envelope["num_turns"].to_i.zero? &&
|
|
95
|
+
usage["total_tokens"].to_i.zero?
|
|
96
|
+
next nil
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
input = UsageExtractors.optional_token_count(usage, "input_tokens")
|
|
100
|
+
output = UsageExtractors.optional_token_count(usage, "output_tokens")
|
|
101
|
+
cache_read = UsageExtractors.optional_token_count(usage, "cache_read_tokens")
|
|
102
|
+
reasoning = UsageExtractors.optional_token_count(usage, "thinking_tokens")
|
|
103
|
+
next nil if input.nil? && output.nil? && cache_read.nil? && reasoning.nil?
|
|
104
|
+
|
|
105
|
+
model = envelope["model"].to_s
|
|
106
|
+
{
|
|
107
|
+
input: input,
|
|
108
|
+
output: output,
|
|
109
|
+
cached: nil,
|
|
110
|
+
cache_read: cache_read,
|
|
111
|
+
cache_write: nil,
|
|
112
|
+
reasoning: reasoning,
|
|
113
|
+
input_includes_cache_read: nil,
|
|
114
|
+
input_includes_cache_write: nil,
|
|
115
|
+
output_includes_reasoning: reasoning.nil? ? nil : true,
|
|
116
|
+
model: model.empty? ? nil : model,
|
|
117
|
+
provider_reported_cost: nil
|
|
118
|
+
}
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def muse_total(entries, key)
|
|
122
|
+
present = entries.select { |entry| entry.key?(key) }
|
|
123
|
+
return nil if present.empty?
|
|
124
|
+
|
|
125
|
+
present.sum { |entry| entry[key].to_i }
|
|
35
126
|
end
|
|
36
127
|
|
|
37
128
|
def usage_hash(event)
|
|
@@ -40,6 +131,9 @@ module AgentCliRuntime
|
|
|
40
131
|
event["token_usage"],
|
|
41
132
|
event.dig("event", "usage"),
|
|
42
133
|
event.dig("event", "message", "usage"),
|
|
134
|
+
# pi reports usage on the assistant message itself, not under an
|
|
135
|
+
# "event" envelope. Without this every pi run is unmetered.
|
|
136
|
+
event.dig("message", "usage"),
|
|
43
137
|
event.dig("info", "usage"),
|
|
44
138
|
event.dig("info", "total_token_usage"),
|
|
45
139
|
event.dig("response", "usage"),
|
|
@@ -48,42 +142,126 @@ module AgentCliRuntime
|
|
|
48
142
|
candidates.find { |value| value.is_a?(Hash) }
|
|
49
143
|
end
|
|
50
144
|
|
|
51
|
-
def usage_result(event, usage, model)
|
|
145
|
+
def usage_result(event, usage, model, provider:)
|
|
146
|
+
cache_read = cache_read_tokens(usage)
|
|
147
|
+
cache_write = cache_write_tokens(usage)
|
|
148
|
+
aggregate_cached = aggregate_cached_tokens(usage)
|
|
149
|
+
reasoning = optional_token_count(
|
|
150
|
+
usage, "reasoning_tokens", "reasoningTokens"
|
|
151
|
+
)
|
|
152
|
+
reasoning ||= optional_token_count(
|
|
153
|
+
nested_hash(usage, "completion_tokens_details"),
|
|
154
|
+
"reasoning_tokens", "reasoningTokens"
|
|
155
|
+
)
|
|
52
156
|
{
|
|
53
|
-
input
|
|
54
|
-
|
|
157
|
+
# The bare "input"/"output" spellings are pi's and are matched last,
|
|
158
|
+
# so a provider using an explicit *_tokens key keeps its own reading.
|
|
159
|
+
input: optional_token_count(
|
|
160
|
+
usage, "input_tokens", "inputTokens", "prompt_tokens", "promptTokens",
|
|
161
|
+
"input"
|
|
55
162
|
),
|
|
56
|
-
output:
|
|
163
|
+
output: optional_token_count(
|
|
57
164
|
usage, "output_tokens", "outputTokens", "completion_tokens",
|
|
58
|
-
"completionTokens"
|
|
165
|
+
"completionTokens", "output"
|
|
59
166
|
),
|
|
60
|
-
cached:
|
|
61
|
-
|
|
167
|
+
cached: aggregate_cached || complete_cached(cache_read, cache_write),
|
|
168
|
+
cache_read: cache_read,
|
|
169
|
+
cache_write: cache_write,
|
|
170
|
+
reasoning: reasoning,
|
|
171
|
+
input_includes_cache_read:
|
|
172
|
+
inclusion_or_inferred(
|
|
173
|
+
usage, "input_includes_cache_read",
|
|
174
|
+
inferred_input_cache_inclusion(provider, cache_read)
|
|
175
|
+
),
|
|
176
|
+
input_includes_cache_write:
|
|
177
|
+
inclusion_or_inferred(
|
|
178
|
+
usage, "input_includes_cache_write",
|
|
179
|
+
inferred_input_cache_inclusion(provider, cache_write)
|
|
180
|
+
),
|
|
181
|
+
output_includes_reasoning:
|
|
182
|
+
inclusion_or_inferred(
|
|
183
|
+
usage, "output_includes_reasoning",
|
|
184
|
+
inferred_reasoning_inclusion(provider, reasoning)
|
|
185
|
+
),
|
|
186
|
+
model: model || model_from_usage(usage) || model_from(event),
|
|
187
|
+
provider_reported_cost: reported_cost(usage)
|
|
62
188
|
}
|
|
63
189
|
end
|
|
64
190
|
|
|
65
|
-
|
|
66
|
-
|
|
191
|
+
# A charge the provider itself reported for this turn, in USD. This is an
|
|
192
|
+
# observed amount, not an estimate from the pricing catalog, and it is the
|
|
193
|
+
# only cost available for a model the catalog does not carry — an
|
|
194
|
+
# OpenRouter-routed model, for one. OpenRouter sends a per-category
|
|
195
|
+
# breakdown carrying a total; other providers may send a scalar.
|
|
196
|
+
def reported_cost(usage)
|
|
197
|
+
value = usage["cost"] || usage["total_cost"] || usage["totalCost"]
|
|
198
|
+
value = value["total"] || value["total_cost"] if value.is_a?(Hash)
|
|
199
|
+
return nil unless value.is_a?(Numeric) || value.is_a?(String)
|
|
200
|
+
|
|
201
|
+
cost = begin
|
|
202
|
+
Float(value)
|
|
203
|
+
rescue ArgumentError, TypeError
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
cost if cost && cost >= 0 && cost.finite?
|
|
207
|
+
end
|
|
208
|
+
|
|
209
|
+
def aggregate_cached_tokens(usage)
|
|
210
|
+
optional_token_count(
|
|
67
211
|
usage, "cached", "cached_tokens", "cachedTokens",
|
|
68
212
|
"cached_input_tokens", "cachedInputTokens"
|
|
69
213
|
)
|
|
70
|
-
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def cache_read_tokens(usage)
|
|
217
|
+
optional_token_count(
|
|
218
|
+
usage, "cache_read_input_tokens", "cacheReadInputTokens"
|
|
219
|
+
) || optional_token_count(
|
|
220
|
+
nested_hash(usage, "prompt_tokens_details"), "cached_tokens", "cachedTokens"
|
|
221
|
+
) || optional_token_count(
|
|
222
|
+
nested_hash(usage, "input_tokens_details"), "cached_tokens", "cachedTokens"
|
|
223
|
+
) || optional_token_count(usage, "cacheRead")
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
def cache_write_tokens(usage)
|
|
227
|
+
optional_token_count(
|
|
228
|
+
usage, "cache_creation_input_tokens", "cacheCreationInputTokens",
|
|
229
|
+
"cache_write_input_tokens", "cacheWriteInputTokens", "cacheWrite"
|
|
230
|
+
)
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
def complete_cached(cache_read, cache_write)
|
|
234
|
+
return nil if cache_read.nil? || cache_write.nil?
|
|
235
|
+
|
|
236
|
+
cache_read + cache_write
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def inferred_input_cache_inclusion(provider, value)
|
|
240
|
+
return nil if value.nil?
|
|
241
|
+
return false if provider == :claude
|
|
242
|
+
return true if provider == :codex
|
|
71
243
|
|
|
72
|
-
|
|
73
|
-
token_count(usage, "cache_creation_input_tokens", "cacheCreationInputTokens") +
|
|
74
|
-
token_count(
|
|
75
|
-
nested_hash(usage, "prompt_tokens_details"), "cached_tokens", "cachedTokens"
|
|
76
|
-
) +
|
|
77
|
-
token_count(
|
|
78
|
-
nested_hash(usage, "input_tokens_details"), "cached_tokens", "cachedTokens"
|
|
79
|
-
)
|
|
244
|
+
nil
|
|
80
245
|
end
|
|
81
246
|
|
|
82
|
-
def
|
|
83
|
-
return
|
|
247
|
+
def inferred_reasoning_inclusion(provider, value)
|
|
248
|
+
return nil if value.nil?
|
|
249
|
+
|
|
250
|
+
provider == :codex ? true : nil
|
|
251
|
+
end
|
|
252
|
+
|
|
253
|
+
def inclusion_or_inferred(hash, key, inferred)
|
|
254
|
+
return inferred unless hash.is_a?(Hash) && hash.key?(key)
|
|
255
|
+
|
|
256
|
+
value = hash[key]
|
|
257
|
+
value if value == true || value == false
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def optional_token_count(hash, *keys)
|
|
261
|
+
return nil unless hash.is_a?(Hash)
|
|
84
262
|
|
|
85
263
|
key = keys.find { |candidate| hash.key?(candidate) }
|
|
86
|
-
key ? hash[key].to_i :
|
|
264
|
+
key ? hash[key].to_i : nil
|
|
87
265
|
end
|
|
88
266
|
|
|
89
267
|
def nested_hash(hash, key)
|
|
@@ -92,11 +270,6 @@ module AgentCliRuntime
|
|
|
92
270
|
end
|
|
93
271
|
|
|
94
272
|
def model_from(event)
|
|
95
|
-
model_usage = event["modelUsage"]
|
|
96
|
-
if model_usage.is_a?(Hash) && !model_usage.empty?
|
|
97
|
-
return model_usage.keys.first.to_s
|
|
98
|
-
end
|
|
99
|
-
|
|
100
273
|
candidates = [
|
|
101
274
|
event["model"],
|
|
102
275
|
event.dig("message", "model"),
|
|
@@ -104,7 +277,12 @@ module AgentCliRuntime
|
|
|
104
277
|
event.dig("response", "model"),
|
|
105
278
|
event.dig("item", "model")
|
|
106
279
|
]
|
|
107
|
-
candidates.find { |value| !value.to_s.empty? }
|
|
280
|
+
explicit = candidates.find { |value| !value.to_s.empty? }
|
|
281
|
+
return explicit.to_s if explicit
|
|
282
|
+
|
|
283
|
+
# Run totals may include helper models; their order is not agent identity.
|
|
284
|
+
model_usage = event["modelUsage"]
|
|
285
|
+
model_usage.keys.first.to_s if model_usage.is_a?(Hash) && model_usage.size == 1
|
|
108
286
|
end
|
|
109
287
|
|
|
110
288
|
def model_from_usage(usage)
|
|
@@ -6,6 +6,14 @@ module AgentCliRuntime
|
|
|
6
6
|
OPENCODE_DISABLE_AUTOUPDATE OPENCODE_PURE
|
|
7
7
|
].freeze
|
|
8
8
|
|
|
9
|
+
# Environment keys that Ruby, Bundler, and RubyGems honor at startup to
|
|
10
|
+
# inject load paths. The OpenCode probe scrubs them from every child
|
|
11
|
+
# environment, so request constructors must refuse to select them back as
|
|
12
|
+
# credential keys.
|
|
13
|
+
SCRUBBED_RUNTIME_ENVIRONMENT_KEYS = %w[
|
|
14
|
+
BUNDLE_BIN_PATH BUNDLE_GEMFILE GEM_HOME GEM_PATH RUBYLIB RUBYOPT
|
|
15
|
+
].freeze
|
|
16
|
+
|
|
9
17
|
module Immutable
|
|
10
18
|
module_function
|
|
11
19
|
|
|
@@ -81,6 +89,12 @@ module AgentCliRuntime
|
|
|
81
89
|
end
|
|
82
90
|
end
|
|
83
91
|
|
|
92
|
+
ExtractedFailure = Data.define(:kind, :message) do
|
|
93
|
+
def initialize(kind:, message:)
|
|
94
|
+
super(kind: kind.to_sym, message: Immutable.string(message))
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
84
98
|
Request = Data.define(
|
|
85
99
|
:profile, :prompt, :permission_mode, :permission_arguments,
|
|
86
100
|
:add_dirs, :require_add_dirs,
|
|
@@ -265,13 +279,14 @@ module AgentCliRuntime
|
|
|
265
279
|
:configuration_path, :configuration,
|
|
266
280
|
:credential_environment_keys, :credential_file,
|
|
267
281
|
:permission_policy, :additional_read_roots,
|
|
268
|
-
:additional_write_roots, :edit_patterns, :plugins, :pure
|
|
282
|
+
:additional_write_roots, :edit_patterns, :bash_patterns, :plugins, :pure
|
|
269
283
|
) do
|
|
270
284
|
def initialize(request:, working_directory:, invocation_root:,
|
|
271
285
|
configuration_path: nil, configuration: nil,
|
|
272
286
|
credential_environment_keys: [], credential_file: nil,
|
|
273
287
|
permission_policy: nil, additional_read_roots: [],
|
|
274
|
-
additional_write_roots: [], edit_patterns: [],
|
|
288
|
+
additional_write_roots: [], edit_patterns: [],
|
|
289
|
+
bash_patterns: [], plugins: [], pure: true)
|
|
275
290
|
unless request.is_a?(Request)
|
|
276
291
|
raise ArgumentError, "request must be an AgentCliRuntime::Request"
|
|
277
292
|
end
|
|
@@ -294,6 +309,15 @@ module AgentCliRuntime
|
|
|
294
309
|
raise ArgumentError,
|
|
295
310
|
"credential environment keys cannot override the OpenCode overlay: #{reserved.join(', ')}"
|
|
296
311
|
end
|
|
312
|
+
scrubbed = keys & SCRUBBED_RUNTIME_ENVIRONMENT_KEYS
|
|
313
|
+
unless scrubbed.empty?
|
|
314
|
+
raise ArgumentError,
|
|
315
|
+
"credential environment keys cannot restore scrubbed runtime injection keys: #{scrubbed.join(', ')}"
|
|
316
|
+
end
|
|
317
|
+
normalized_bash_patterns = Immutable.strings(bash_patterns)
|
|
318
|
+
if normalized_bash_patterns.any?(&:empty?)
|
|
319
|
+
raise ArgumentError, "OpenCode bash patterns must be non-empty"
|
|
320
|
+
end
|
|
297
321
|
|
|
298
322
|
super(
|
|
299
323
|
request: request,
|
|
@@ -310,6 +334,7 @@ module AgentCliRuntime
|
|
|
310
334
|
additional_read_roots: Immutable.strings(additional_read_roots),
|
|
311
335
|
additional_write_roots: Immutable.strings(additional_write_roots),
|
|
312
336
|
edit_patterns: Immutable.strings(edit_patterns),
|
|
337
|
+
bash_patterns: normalized_bash_patterns,
|
|
313
338
|
plugins: Immutable.strings(plugins),
|
|
314
339
|
pure: pure != false
|
|
315
340
|
)
|
|
@@ -318,20 +343,37 @@ module AgentCliRuntime
|
|
|
318
343
|
|
|
319
344
|
ProbeRequest = Data.define(
|
|
320
345
|
:profile, :route, :variant, :environment,
|
|
321
|
-
:credential_environment_keys, :credential_file_staged
|
|
346
|
+
:credential_environment_keys, :credential_file_staged,
|
|
347
|
+
:configured_variants, :executable
|
|
322
348
|
) do
|
|
323
349
|
def initialize(profile:, route:, variant: nil, environment: {},
|
|
324
350
|
credential_environment_keys: [],
|
|
325
|
-
credential_file_staged: false
|
|
351
|
+
credential_file_staged: false,
|
|
352
|
+
configured_variants: nil,
|
|
353
|
+
executable: nil)
|
|
326
354
|
parsed_route = route.is_a?(Route) ? route : Route.parse(route)
|
|
355
|
+
normalized_environment = Immutable.hash(environment)
|
|
356
|
+
unallowed = normalized_environment.keys - OPENCODE_OVERLAY_ENVIRONMENT_KEYS
|
|
357
|
+
unless unallowed.empty?
|
|
358
|
+
raise ArgumentError,
|
|
359
|
+
"ProbeRequest environment keys must be OpenCode overlay keys: #{unallowed.join(', ')}"
|
|
360
|
+
end
|
|
361
|
+
normalized_credential_keys = Immutable.strings(credential_environment_keys)
|
|
362
|
+
scrubbed = normalized_credential_keys & SCRUBBED_RUNTIME_ENVIRONMENT_KEYS
|
|
363
|
+
unless scrubbed.empty?
|
|
364
|
+
raise ArgumentError,
|
|
365
|
+
"ProbeRequest credential environment keys cannot restore scrubbed runtime injection keys: #{scrubbed.join(', ')}"
|
|
366
|
+
end
|
|
327
367
|
super(
|
|
328
368
|
profile: profile,
|
|
329
369
|
route: parsed_route,
|
|
330
370
|
variant: variant.nil? ? nil : Immutable.string(variant),
|
|
331
|
-
environment:
|
|
332
|
-
credential_environment_keys:
|
|
333
|
-
|
|
334
|
-
|
|
371
|
+
environment: normalized_environment,
|
|
372
|
+
credential_environment_keys: normalized_credential_keys,
|
|
373
|
+
credential_file_staged: credential_file_staged == true,
|
|
374
|
+
configured_variants:
|
|
375
|
+
configured_variants.nil? ? nil : Immutable.strings(configured_variants),
|
|
376
|
+
executable: executable.nil? ? nil : Immutable.string(executable)
|
|
335
377
|
)
|
|
336
378
|
end
|
|
337
379
|
end
|
|
@@ -445,17 +487,35 @@ module AgentCliRuntime
|
|
|
445
487
|
end
|
|
446
488
|
|
|
447
489
|
NormalizedUsage = Data.define(
|
|
448
|
-
:input, :output, :cache_read, :cache_write, :reasoning,
|
|
490
|
+
:input, :output, :cache_read, :cache_write, :reasoning,
|
|
491
|
+
:input_includes_cache_read, :input_includes_cache_write,
|
|
492
|
+
:output_includes_reasoning, :cost
|
|
449
493
|
) do
|
|
450
494
|
def initialize(input: nil, output: nil, cache_read: nil, cache_write: nil,
|
|
451
|
-
reasoning: nil,
|
|
495
|
+
reasoning: nil, input_includes_cache_read: nil,
|
|
496
|
+
input_includes_cache_write: nil,
|
|
497
|
+
output_includes_reasoning: nil,
|
|
498
|
+
provider_reported_cost: nil, cost: nil)
|
|
499
|
+
if !provider_reported_cost.nil? && !cost.nil? && provider_reported_cost != cost
|
|
500
|
+
raise ArgumentError, "provider_reported_cost and cost disagree"
|
|
501
|
+
end
|
|
452
502
|
super(
|
|
453
503
|
input: number(input, :input, integer: true),
|
|
454
504
|
output: number(output, :output, integer: true),
|
|
455
505
|
cache_read: number(cache_read, :cache_read, integer: true),
|
|
456
506
|
cache_write: number(cache_write, :cache_write, integer: true),
|
|
457
507
|
reasoning: number(reasoning, :reasoning, integer: true),
|
|
458
|
-
|
|
508
|
+
input_includes_cache_read:
|
|
509
|
+
boolean(input_includes_cache_read, :input_includes_cache_read),
|
|
510
|
+
input_includes_cache_write:
|
|
511
|
+
boolean(input_includes_cache_write, :input_includes_cache_write),
|
|
512
|
+
output_includes_reasoning:
|
|
513
|
+
boolean(output_includes_reasoning, :output_includes_reasoning),
|
|
514
|
+
cost: number(
|
|
515
|
+
provider_reported_cost.nil? ? cost : provider_reported_cost,
|
|
516
|
+
:provider_reported_cost,
|
|
517
|
+
integer: false
|
|
518
|
+
)
|
|
459
519
|
)
|
|
460
520
|
end
|
|
461
521
|
|
|
@@ -465,6 +525,8 @@ module AgentCliRuntime
|
|
|
465
525
|
cache_read + cache_write
|
|
466
526
|
end
|
|
467
527
|
|
|
528
|
+
alias provider_reported_cost cost
|
|
529
|
+
|
|
468
530
|
private
|
|
469
531
|
|
|
470
532
|
def number(value, label, integer:)
|
|
@@ -477,6 +539,14 @@ module AgentCliRuntime
|
|
|
477
539
|
|
|
478
540
|
value
|
|
479
541
|
end
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
def boolean(value, label)
|
|
545
|
+
return nil if value.nil?
|
|
546
|
+
return value if value == true || value == false
|
|
547
|
+
|
|
548
|
+
raise ArgumentError, "#{label} must be true, false, or nil"
|
|
549
|
+
end
|
|
480
550
|
end
|
|
481
551
|
|
|
482
552
|
RouteIdentity = Data.define(:requested, :actual, :resolution_status) do
|
data/lib/agent_cli_runtime.rb
CHANGED
|
@@ -8,12 +8,14 @@ end
|
|
|
8
8
|
|
|
9
9
|
require "agent_cli_runtime/redactor"
|
|
10
10
|
require "agent_cli_runtime/usage_extractors"
|
|
11
|
+
require "agent_cli_runtime/error_extractors"
|
|
11
12
|
require "agent_cli_runtime/opencode/result_parser"
|
|
12
13
|
require "agent_cli_runtime/profile"
|
|
13
14
|
require "agent_cli_runtime/profiles"
|
|
14
15
|
require "agent_cli_runtime/probe"
|
|
15
16
|
require "agent_cli_runtime/runtime"
|
|
16
17
|
require "agent_cli_runtime/opencode/probe"
|
|
18
|
+
require "agent_cli_runtime/opencode/permissions"
|
|
17
19
|
require "agent_cli_runtime/opencode/overlay"
|
|
18
20
|
require "agent_cli_runtime/opencode/inspection"
|
|
19
21
|
require "agent_cli_runtime/cli"
|
|
@@ -37,6 +39,10 @@ module AgentCliRuntime
|
|
|
37
39
|
Runtime.extract_usage(profile, event)
|
|
38
40
|
end
|
|
39
41
|
|
|
42
|
+
def extract_provider_error(profile, event)
|
|
43
|
+
Runtime.extract_provider_error(profile, event)
|
|
44
|
+
end
|
|
45
|
+
|
|
40
46
|
def observe(profile, result)
|
|
41
47
|
Runtime.observe(profile, result)
|
|
42
48
|
end
|