lex-llm-bedrock 0.5.8 → 0.5.10

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 860a45a8f5e161e2d46d3822a895dc659136ab19e9f5ad0233f690ec9742a8ae
4
- data.tar.gz: f5dac9cdcf27cecc7c7ed319febc79fc3bc390a92eafb3e40e27e768c4cd514f
3
+ metadata.gz: f71988b85a83937a63f4f746b47ced891eab1020bf8257530cacf1530c9dc221
4
+ data.tar.gz: 567c99945ef05ab78f7fcc9c6f2b2cddbfa439381c24db3c9b0d395c8067a127
5
5
  SHA512:
6
- metadata.gz: e075da15662be51f02f72286c894ebbc26fa09251f2cb8e62ff09801a5a869947886dbce7e5fa35b6fb9ca08aa2269618af28ff38803bcafc6b53ce579a71b6a
7
- data.tar.gz: 8bae12f419ea1f3b6bc13233a98a35ef988e0953a6149c55f419ea8a5a0461c0d00a12b8186061773d732c629efaf40c4658f2f1bd8e59a219262d72e645fc38
6
+ metadata.gz: 05bded0be0925b28bc33e504a29b112d2740e9601c27549c826071dd2ad5eb70da6b7b55bfe7cc1192ee23b919f23844f191d52dc61abe978126c2aa809479ed
7
+ data.tar.gz: 2b9ef635ea825e718397850cdfc0fa195ebf391c26a8a95087893d364ca82526aea78d08a10359f1c556dc94f5198d107f4c173427fab5729407fa8f466d3528
data/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.5.10] - 2026-11-18
4
+
5
+ ### Fixed
6
+ - Preserve explicit thinking budgets on Claude Sonnet 4.6 and Opus 4.6, which support both manual and adaptive thinking. A budget-only request no longer becomes adaptive low effort and silently permits thinking to be skipped. Both provider dispatch paths and canonical render targets use the shared thinking-mode decision; effort-only requests and adaptive-only models retain adaptive rendering.
7
+ - Preserve provider-reported `output_tokens_details.thinking_tokens` in canonical usage. Streaming completion shares the translator's usage parser and merges final cumulative output counts with initial input/cache counts, including when thinking text is omitted.
8
+
9
+ ## [0.5.9] - 2026-11-17
10
+
11
+ ### Fixed
12
+ - Classify Bedrock `ServiceUnavailableException` as request-local overload. Temporary AWS capacity failures no longer mark every offering on the instance unavailable and cause unrelated requests to fail routing.
13
+ - Classify an explicitly disposed callable as exact-instance unavailability; readiness remains responsible for recovery.
14
+ - Keep system-shape regression coverage valid when the shared canonical constructor rejects a non-String before the provider renderer receives it.
15
+
3
16
  ## [0.5.8] - 2026-08-25
4
17
 
5
18
  ### Added
@@ -155,6 +155,8 @@ module Legion
155
155
 
156
156
  def classify_error(error:)
157
157
  case error
158
+ when Legion::Extensions::Llm::Inventory::Errors::CallableDisposedError
159
+ :instance_unavailable
158
160
  when Aws::BedrockRuntime::Errors::ThrottlingException,
159
161
  Aws::Bedrock::Errors::ThrottlingException
160
162
  :rate_limited
@@ -172,8 +174,9 @@ module Legion
172
174
  :model_missing
173
175
  when Aws::BedrockRuntime::Errors::ServiceUnavailableException,
174
176
  Aws::Bedrock::Errors::ServiceUnavailableException
175
- # Explicit flat service-unavailable from AWS = instance_unavailable
176
- :instance_unavailable
177
+ # AWS documents this as temporary capacity exhaustion, not
178
+ # authoritative evidence that the exact instance is offline.
179
+ :overloaded
177
180
  when Aws::BedrockRuntime::Errors::ServiceError,
178
181
  Aws::Bedrock::Errors::ServiceError
179
182
  classify_service_error(error: error)
@@ -189,9 +192,7 @@ module Legion
189
192
  when 401 then :authentication
190
193
  when 403 then :authorization
191
194
  when 404 then :model_missing
192
- # NEVER classify raw 503/5xx as instance_unavailable by status alone.
193
- # Only the explicit ServiceUnavailableException (handled above) justifies
194
- # instance_unavailable. Everything else is request-local.
195
+ # Transport overload is request-local, including AWS's named 503.
195
196
  when 503, 529 then :overloaded
196
197
  else :provider_error
197
198
  end
@@ -227,27 +227,10 @@ module Legion
227
227
  translator.parse_response(normalize_response(response), model: fallback_model)
228
228
  end
229
229
 
230
- def cache_read_tokens(usage)
231
- return nil if usage.nil?
232
-
233
- value(usage, :cache_read_input_tokens) || value(usage, 'cache_read_input_tokens')
234
- end
235
-
236
- def cache_write_tokens(usage)
237
- return nil if usage.nil?
238
-
239
- value(usage, :cache_creation_input_tokens) || value(usage, 'cache_creation_input_tokens')
240
- end
241
-
242
230
  # Canonical usage from a wire usage hash/struct (provider spellings
243
231
  # translated at this edge, 13 §3).
244
232
  def usage_from(usage)
245
- Canonical::Usage.build(
246
- input_tokens: value(usage, :input_tokens),
247
- output_tokens: value(usage, :output_tokens),
248
- cache_read_tokens: cache_read_tokens(usage),
249
- cache_write_tokens: cache_write_tokens(usage)
250
- )
233
+ translator.parse_usage(usage)
251
234
  end
252
235
 
253
236
  # One stop-reason edge: the gem's Translator map is the single
@@ -314,6 +314,9 @@ module Legion
314
314
  end
315
315
  when 'message_delta'
316
316
  state[:stop_reason] = (event_json['delta'] || {})['stop_reason']
317
+ # Final output counts are cumulative; retain input/cache counts
318
+ # from message_start without adding output tokens twice.
319
+ state[:final_usage] = (state[:final_usage] || {}).merge(event_json['usage'] || {})
317
320
  end
318
321
  rescue StandardError => e
319
322
  handle_exception(e, level: :error, handled: false,
@@ -19,16 +19,21 @@ module Legion
19
19
  # 2. ADAPTIVE (Claude opus-4-6/4-7/4-8/5, sonnet-4-6/5):
20
20
  # { type: 'adaptive' } + output_config: { effort: <low|medium|high|xhigh|max> }
21
21
  # Gated by beta header 'effort-2025-11-24' in the anthropic_beta list.
22
- # These models REJECT { type: 'enabled', budget_tokens: N } with
22
+ # Adaptive-only models REJECT { type: 'enabled', budget_tokens: N } with
23
23
  # `ValidationException: "thinking.type.enabled" is not supported for
24
24
  # this model. Use "thinking.type.adaptive" and "output_config.effort"`.
25
25
  # The effort value is passed through directly from the canonical
26
26
  # Thinking::Config#resolved_effort (the full ladder matches 1:1).
27
27
  # 'none' or nil effort → omit output_config (API defaults to high).
28
28
  #
29
+ # Opus 4.6 and Sonnet 4.6 support BOTH shapes. Preserve an explicit
30
+ # budget on those models; effort-only requests use adaptive thinking.
31
+ # Converting a supplied budget into adaptive effort changes the request
32
+ # from manual thinking to a mode that may skip thinking altogether.
33
+ #
29
34
  # PRECEDENCE: adaptive fragments are checked BEFORE budgeted because
30
35
  # `claude-opus-4` (budgeted) is a substring of `claude-opus-4-7`
31
- # (adaptive). An adaptive-fragment match wins.
36
+ # (adaptive). Only the explicit dual-mode list permits both shapes.
32
37
  #
33
38
  # The match is a substring so it tolerates the many Bedrock model-id
34
39
  # decorations (geo prefixes `us.`/`eu.`/`ap.`, `anthropic.` provider
@@ -60,6 +65,10 @@ module Legion
60
65
  claude-sonnet-5
61
66
  ].freeze
62
67
 
68
+ # AWS still accepts manual budgets on these adaptive-capable models.
69
+ # https://docs.aws.amazon.com/bedrock/latest/userguide/claude-messages-adaptive-thinking.html
70
+ DUAL_MODE_THINKING_FRAGMENTS = %w[claude-opus-4-6 claude-sonnet-4-6].freeze
71
+
63
72
  # Model-id fragments for Claude families that support explicit budgeted
64
73
  # extended thinking via { type: 'enabled', budget_tokens: N }.
65
74
  BUDGETED_THINKING_FRAGMENTS = %w[
@@ -83,10 +92,12 @@ module Legion
83
92
  ADAPTIVE_EFFORT_FRAGMENTS.any? { |fragment| mid.include?(fragment) }
84
93
  end
85
94
 
86
- # @return [Boolean] true when the model supports { type: 'enabled', budget_tokens: N }
87
- # but NOT adaptive thinking (adaptive wins when both would substring-match).
95
+ # @return [Boolean] true when the model supports { type: 'enabled', budget_tokens: N }.
96
+ # Dual-mode 4.6 models support both; adaptive-only models must not
97
+ # accidentally match a broad budgeted-family substring.
88
98
  def budgeted_thinking?(model_id)
89
99
  return false if model_id.nil? || model_id.to_s.strip.empty?
100
+ return true if DUAL_MODE_THINKING_FRAGMENTS.any? { |fragment| model_id.to_s.include?(fragment) }
90
101
  return false if adaptive_thinking?(model_id)
91
102
 
92
103
  mid = model_id.to_s
@@ -95,6 +106,10 @@ module Legion
95
106
  BUDGETED_THINKING_FRAGMENTS.any? { |fragment| mid.include?(fragment) }
96
107
  end
97
108
 
109
+ def adaptive_mode?(thinking:, model_id:)
110
+ adaptive_thinking?(model_id) && !(thinking.budget && budgeted_thinking?(model_id))
111
+ end
112
+
98
113
  # @return [Boolean] true when the model is KNOWN not to support thinking.
99
114
  # Distinct from `!budgeted_thinking?`: a nil/blank/unknown model id is
100
115
  # NOT known-unsupported — we then honor an explicit thinking request and
@@ -156,8 +171,9 @@ module Legion
156
171
  return nil unless thinking_enabled?(thinking)
157
172
  return nil if known_non_thinking?(model_id)
158
173
 
159
- # Adaptive models use a different wire shape — no budget_tokens.
160
- return { type: 'adaptive' } if adaptive_thinking?(model_id)
174
+ # Preserve explicit budgets where supported; only resolve to an
175
+ # adaptive effort when that is the selected model's usable mode.
176
+ return { type: 'adaptive' } if adaptive_mode?(thinking: thinking, model_id: model_id)
161
177
 
162
178
  budget = thinking.resolved_budget
163
179
  if budget.nil?
@@ -185,7 +201,7 @@ module Legion
185
201
  # Converse: additionalModelRequestFields + beta array mechanism).
186
202
  def adaptive_wire(thinking:, model_id:)
187
203
  return nil unless thinking_enabled?(thinking)
188
- return nil unless adaptive_thinking?(model_id)
204
+ return nil unless adaptive_mode?(thinking: thinking, model_id: model_id)
189
205
 
190
206
  effort = wire_effort(thinking.resolved_effort)
191
207
  result = { thinking: { type: 'adaptive' }, beta_header: EFFORT_BETA_HEADER }
@@ -102,8 +102,10 @@ module Legion
102
102
  )
103
103
  Canonical::Chunk.tool_call_delta(tool_call: tc, request_id: request_id)
104
104
  when 'message_delta'
105
- stop = nested_read(raw, 'delta', 'stop_reason', :stop_reason) || ''
106
- Canonical::Chunk.done(request_id: request_id, stop_reason: map_stop_reason(stop))
105
+ stop = safe_key_read(safe_key_read(raw, :delta), :stop_reason)
106
+ usage_raw = safe_key_read(raw, :usage)
107
+ Canonical::Chunk.done(request_id: request_id, stop_reason: map_stop_reason(stop),
108
+ usage: usage_raw ? parse_usage(usage_raw) : nil)
107
109
  end
108
110
  end
109
111
  end
@@ -124,25 +124,18 @@ module Legion
124
124
  Canonical::Usage.from_hash(h)
125
125
  end
126
126
 
127
+ # Provider stream completion shares the same wire-to-canonical edge.
128
+ public :parse_usage
129
+
127
130
  def build_usage_hash(usage_raw)
128
- if usage_raw.is_a?(Hash)
129
- {
130
- input_tokens: usage_raw[:input_tokens] || usage_raw['input_tokens'],
131
- output_tokens: usage_raw[:output_tokens] || usage_raw['output_tokens'],
132
- cache_read_tokens: usage_raw[:cache_read_input_tokens] || usage_raw['cache_read_input_tokens'],
133
- cache_write_tokens: usage_raw[:cache_creation_input_tokens] ||
134
- usage_raw['cache_creation_input_tokens'],
135
- thinking_tokens: usage_raw[:thinking_tokens] || usage_raw['thinking_tokens']
136
- }
137
- else
138
- {
139
- input_tokens: safe_key_read(usage_raw, :input_tokens),
140
- output_tokens: safe_key_read(usage_raw, :output_tokens),
141
- cache_read_tokens: safe_key_read(usage_raw, :cache_read_input_tokens),
142
- cache_write_tokens: safe_key_read(usage_raw, :cache_creation_input_tokens),
143
- thinking_tokens: safe_key_read(usage_raw, :thinking_tokens)
144
- }
145
- end
131
+ details = safe_key_read(usage_raw, :output_tokens_details)
132
+ {
133
+ input_tokens: safe_key_read(usage_raw, :input_tokens),
134
+ output_tokens: safe_key_read(usage_raw, :output_tokens),
135
+ cache_read_tokens: safe_key_read(usage_raw, :cache_read_input_tokens),
136
+ cache_write_tokens: safe_key_read(usage_raw, :cache_creation_input_tokens),
137
+ thinking_tokens: safe_key_read(details, :thinking_tokens) || safe_key_read(usage_raw, :thinking_tokens)
138
+ }
146
139
  end
147
140
  end
148
141
  end
@@ -4,7 +4,7 @@ module Legion
4
4
  module Extensions
5
5
  module Llm
6
6
  module Bedrock
7
- VERSION = '0.5.8'
7
+ VERSION = '0.5.10'
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lex-llm-bedrock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.8
4
+ version: 0.5.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - LegionIO