crystil 0.6.0 → 0.7.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.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +110 -72
  3. data/README.md +6 -9
  4. data/lib/crystil/callables.rb +24 -0
  5. data/lib/crystil/client.rb +7 -0
  6. data/lib/crystil/collector.rb +127 -4
  7. data/lib/crystil/sentinel.rb +2 -19
  8. data/lib/crystil/version.rb +1 -1
  9. data/lib/crystil/wrappers/anthropic.rb +37 -36
  10. data/lib/crystil/wrappers/base.rb +25 -28
  11. data/lib/crystil/wrappers/blocked_call.rb +52 -0
  12. data/lib/crystil/wrappers/constants.rb +3 -0
  13. data/lib/crystil/wrappers/deep_copy.rb +52 -0
  14. data/lib/crystil/wrappers/event_stream.rb +21 -26
  15. data/lib/crystil/wrappers/geminiai.rb +57 -36
  16. data/lib/crystil/wrappers/google.rb +18 -18
  17. data/lib/crystil/wrappers/groq.rb +89 -101
  18. data/lib/crystil/wrappers/openai.rb +118 -101
  19. data/lib/crystil/wrappers/ruby_llm.rb +150 -101
  20. data/lib/crystil/wrappers/ruby_llm_query.rb +176 -0
  21. data/lib/crystil/wrappers/ruby_llm_record.rb +105 -0
  22. data/lib/crystil/wrappers/ruby_llm_response.rb +148 -0
  23. data/lib/crystil/wrappers/stopped_call.rb +88 -0
  24. data/lib/crystil.rb +2 -1
  25. data/sig/crystil/callables.rbs +6 -0
  26. data/sig/crystil/client.rbs +2 -0
  27. data/sig/crystil/collector.rbs +33 -1
  28. data/sig/crystil/sentinel.rbs +0 -1
  29. data/sig/crystil/wrappers/base.rbs +7 -5
  30. data/sig/crystil/wrappers/blocked_call.rbs +15 -0
  31. data/sig/crystil/wrappers/constants.rbs +1 -0
  32. data/sig/crystil/wrappers/deep_copy.rbs +12 -0
  33. data/sig/crystil/wrappers/event_stream.rbs +2 -2
  34. data/sig/crystil/wrappers/geminiai.rbs +2 -0
  35. data/sig/crystil/wrappers/openai.rbs +12 -0
  36. data/sig/crystil/wrappers/ruby_llm.rbs +11 -4
  37. data/sig/crystil/wrappers/ruby_llm_query.rbs +35 -0
  38. data/sig/crystil/wrappers/ruby_llm_record.rbs +21 -0
  39. data/sig/crystil/wrappers/ruby_llm_response.rbs +39 -0
  40. data/sig/crystil/wrappers/stopped_call.rbs +26 -0
  41. metadata +16 -2
@@ -2,13 +2,19 @@
2
2
 
3
3
  require "time"
4
4
 
5
+ require_relative "blocked_call"
6
+ require_relative "deep_copy"
5
7
  require_relative "event_stream"
8
+ require_relative "stopped_call"
6
9
 
7
10
  module Crystil
8
11
  module Wrappers
9
12
  # Base functionality for all provider wrappers
10
13
  module Base
14
+ include BlockedCall
15
+ include DeepCopy
11
16
  include EventStream
17
+ include StoppedCall
12
18
 
13
19
  def crystil_wrap_method(method_name, _provider_name)
14
20
  return if method(method_name).source_location&.first&.include?("crystil")
@@ -49,7 +55,7 @@ module Crystil
49
55
 
50
56
  def crystil_submit_analytics(method:, args:, kwargs:, response:, start_time:,
51
57
  end_time:, provider: nil, title: nil, version: nil,
52
- status: "succeeded", exception: nil)
58
+ status: "succeeded", exception: nil, reason: nil)
53
59
  collector = instance_variable_get(:@crystil_collector)
54
60
  config = instance_variable_get(:@crystil_config)
55
61
 
@@ -65,7 +71,8 @@ module Crystil
65
71
  provider: provider,
66
72
  title: title,
67
73
  version: version,
68
- exception: exception
74
+ exception: exception,
75
+ reason: reason
69
76
  )
70
77
 
71
78
  collector.submit_async(payload)
@@ -114,11 +121,13 @@ module Crystil
114
121
  accumulated
115
122
  end
116
123
 
124
+ # Returns a copy of the chunk with every delta holding all of its keys, nil
125
+ # when missing, as the Python SDK sends them. A copy, so the chunk the app's
126
+ # callback gets is the gem's own, and the merge can keep it.
117
127
  def crystil_normalize_openai_chunk(chunk)
118
- # Normalize OpenAI streaming chunks to match Python SDK format
119
- # Ensure delta objects have all keys present with null values
120
128
  return chunk unless chunk.is_a?(Hash)
121
129
 
130
+ chunk = deep_copy(chunk)
122
131
  if chunk["choices"].is_a?(Array)
123
132
  chunk["choices"].each do |choice|
124
133
  next unless choice.is_a?(Hash) && choice.key?("delta")
@@ -141,20 +150,17 @@ module Crystil
141
150
  private
142
151
 
143
152
  def extract_query(_method, _args, kwargs)
144
- # Deep copy to avoid mutation issues
145
- query = deep_copy(kwargs)
146
-
147
- # Normalize stream parameter: convert Proc to boolean true
148
- # This ensures streaming requests are properly serialized to JSON
149
- query[:stream] = true if query.is_a?(Hash) && query[:stream].is_a?(Proc)
150
-
151
- query
153
+ # Callbacks become `true` first: they cannot be sent as JSON, and Marshal
154
+ # cannot copy them, so the deep copy would fall back to a shallow `dup`.
155
+ deep_copy(Crystil::Callables.replace_with_true(kwargs))
152
156
  rescue StandardError
153
157
  {}
154
158
  end
155
159
 
156
160
  def extract_response(response)
157
161
  case response
162
+ when nil
163
+ nil
158
164
  when Hash, Array
159
165
  deep_copy(response)
160
166
  when String
@@ -205,16 +211,6 @@ module Crystil
205
211
  end
206
212
  end
207
213
 
208
- def deep_copy(obj)
209
- Marshal.load(Marshal.dump(obj))
210
- rescue StandardError
211
- begin
212
- obj.dup
213
- rescue StandardError
214
- obj
215
- end
216
- end
217
-
218
214
  # Build the payload that is sent to the collector API which then sends the payload
219
215
  # to the backend.
220
216
  # - conversion.client - Routes the payload to the correct service
@@ -222,9 +218,13 @@ module Crystil
222
218
  # openai, and anthropic) are common title values.
223
219
  # - conversion.query - Holds the request information.
224
220
  # - conversion.response - Holds the response from the LLM. This is different for each LLM,
225
- # and the different services handle them on the backend.
221
+ # and the different services handle them on the backend. `nil` only for a blocked call.
222
+ # - meta.fnfg.reason - Sentinel's reason, sent only for a blocked call.
226
223
  def build_payload(query:, response:, start_time:, end_time:, config:, status:,
227
- provider: nil, title: nil, version: nil, exception: nil)
224
+ provider: nil, title: nil, version: nil, exception: nil, reason: nil)
225
+ fnfg = { status: status, exc: exception }
226
+ fnfg[:reason] = reason if reason
227
+
228
228
  {
229
229
  attribution: config.attribution&.to_h,
230
230
  conversation: {
@@ -240,10 +240,7 @@ module Crystil
240
240
  api: {
241
241
  key: config.api_key
242
242
  },
243
- fnfg: {
244
- status: status,
245
- exc: exception
246
- },
243
+ fnfg: fnfg,
247
244
  sdk: {
248
245
  client: "ruby",
249
246
  version: config.version
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crystil
4
+ module Wrappers
5
+ # The Sentinel check every wrapper runs before the provider call, and the
6
+ # record it sends when Sentinel blocks that call.
7
+ #
8
+ # Mixed into `Base`, like `EventStream`, so a wrapper reaches it the same
9
+ # way it reaches `crystil_submit_analytics`.
10
+ module BlockedCall
11
+ # Asks Sentinel whether the call may go ahead. A blocked call is still
12
+ # recorded, with no response, before the block is raised.
13
+ #
14
+ # `query` is what the payload records, when it differs from the `request`
15
+ # Sentinel judges. The payload is sent here, not from the wrapper's outer
16
+ # rescue: that also catches a block from a nested call (a RubyLLM tool
17
+ # calling another registered client), and the outer call was not blocked.
18
+ def crystil_raise_if_irrelevant!(request:, start_time:, title:, provider: nil, version: nil, query: request)
19
+ sentinel = instance_variable_get(:@crystil_sentinel)
20
+ sentinel&.raise_if_irrelevant!(title: title, request: request, provider: provider, version: version)
21
+ rescue CrystilRequestInterceptedError => e
22
+ crystil_submit_blocked_analytics(
23
+ query: query, reason: e.message, start_time: start_time,
24
+ provider: provider, title: title, version: version
25
+ )
26
+ raise e
27
+ end
28
+
29
+ # True when `error` is the block this call's own Sentinel check raised, already
30
+ # recorded above. A block from a nested call made after the check passed is
31
+ # false: this call reached the provider, so the wrapper records it as failed.
32
+ def crystil_block_already_recorded?(error, passed_sentinel)
33
+ error.is_a?(CrystilRequestInterceptedError) && !passed_sentinel
34
+ end
35
+
36
+ private
37
+
38
+ # Sends the payload for a call Sentinel blocked: no response, and Sentinel's
39
+ # reason. A failure here must never replace the block, so it is reported and swallowed.
40
+ def crystil_submit_blocked_analytics(query:, reason:, start_time:, provider:, title:, version:)
41
+ crystil_submit_analytics(
42
+ method: :crystil_raise_if_irrelevant!, args: [], kwargs: query, response: nil,
43
+ start_time: start_time, end_time: Time.now,
44
+ provider: provider, title: title, version: version,
45
+ status: "blocked", reason: reason
46
+ )
47
+ rescue StandardError => e
48
+ warn "Crystil: Failed to record blocked call: #{e.message}"
49
+ end
50
+ end
51
+ end
52
+ end
@@ -8,5 +8,8 @@ module Crystil
8
8
  GOOGLE_CLIENT_TITLE = "google"
9
9
  RUBY_LLM_PROVIDER = "ruby_llm"
10
10
  GROQ_PROVIDER = "groq"
11
+
12
+ # The RubyLLM providers that send Gemini's request format: Vertex AI's subclasses Gemini's.
13
+ RUBY_LLM_GEMINI_PROVIDERS = %w[gemini vertexai].freeze
11
14
  end
12
15
  end
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crystil
4
+ module Wrappers
5
+ # The copy a recorded query or response is taken as. Mixed into `Base`, like
6
+ # `BlockedCall`, so a wrapper reaches `deep_copy` the same way it always has.
7
+ module DeepCopy
8
+ private
9
+
10
+ # A deep copy, taken before the payload is queued, so a change the app makes later
11
+ # cannot reach what is sent. A frozen String is shared, not copied: it cannot change,
12
+ # and a recorded attachment's base64 can run to megabytes. `copies` gives an object
13
+ # held twice, or a structure that holds itself, the same shape in the copy.
14
+ def deep_copy(obj, copies = {}.compare_by_identity)
15
+ case obj
16
+ when Integer, Float, Symbol, nil, true, false
17
+ obj # nothing can change these
18
+ else
19
+ if obj.instance_of?(String)
20
+ obj.frozen? ? obj : obj.dup
21
+ elsif obj.instance_of?(Hash) || obj.instance_of?(Array)
22
+ copies.key?(obj) ? copies[obj] : crystil_copy_container(obj, copies)
23
+ else
24
+ crystil_marshal_copy(obj)
25
+ end
26
+ end
27
+ end
28
+
29
+ def crystil_copy_container(obj, copies)
30
+ if obj.instance_of?(Hash)
31
+ copy = copies[obj] = {}
32
+ obj.each { |key, value| copy[deep_copy(key, copies)] = deep_copy(value, copies) }
33
+ else
34
+ copy = copies[obj] = []
35
+ obj.each { |value| copy << deep_copy(value, copies) }
36
+ end
37
+ copy
38
+ end
39
+
40
+ # Any other object is copied through Marshal, or with `dup` when it cannot be marshaled.
41
+ def crystil_marshal_copy(obj)
42
+ Marshal.load(Marshal.dump(obj))
43
+ rescue StandardError
44
+ begin
45
+ obj.dup
46
+ rescue StandardError
47
+ obj
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -13,7 +13,6 @@ module Crystil
13
13
  # object for OpenAI and Groq, record the events as a list for Anthropic.
14
14
  module EventStream
15
15
  STREAM_CUT_SHORT_MESSAGE = "Stream read ended before the provider finished sending events"
16
- STREAM_CLOSED_UNREAD_MESSAGE = "Stream closed before any events were read"
17
16
 
18
17
  # Record a lazy event stream as the unmerged list of events the provider
19
18
  # sent, once the caller has finished reading it.
@@ -45,33 +44,30 @@ module Crystil
45
44
  reported = false
46
45
  claim_report = -> { reported ? false : (reported = true) }
47
46
  recorder = self
48
- report = lambda do |error|
47
+ report = lambda do |finished:, error: nil, caller_block: nil, stopped: nil|
49
48
  recorder.crystil_report_event_stream(
50
- events: events.dup, error: error, method: method, kwargs: kwargs,
51
- start_time: start_time, title: title, version: version
49
+ events: events.dup, finished: finished, error: error, caller_block: caller_block, stopped: stopped,
50
+ method: method, kwargs: kwargs, start_time: start_time, title: title, version: version
52
51
  )
53
52
  end
54
53
 
55
54
  stream.define_singleton_method(:each) do |*args, &block|
56
55
  next original_each.call(*args, &block) if block.nil?
57
56
 
58
- failure = nil
57
+ error = nil
59
58
  finished = false
60
- in_caller_block = false
59
+ caller_block = Crystil::Wrappers::StoppedCall::CallerBlock.new(block)
61
60
  reading_fibers << Fiber.current
62
61
 
63
62
  begin
64
63
  result = original_each.call(*args) do |event|
65
64
  events << Crystil::Wrappers::EventStream.crystil_event_to_data(event)
66
- in_caller_block = true
67
- block.call(event)
68
- in_caller_block = false
65
+ caller_block.call(event)
69
66
  end
70
67
  finished = true
71
68
  result
72
- rescue Exception => e # rubocop:disable Lint/RescueException -- re-raised; Interrupt must count as failed
73
- # An error from the caller's own code is theirs, not the provider's, and its message may hold their data.
74
- failure = e unless in_caller_block
69
+ rescue Exception => e # rubocop:disable Lint/RescueException -- re-raised; an Interrupt is recorded too
70
+ error = e
75
71
  raise e
76
72
  ensure
77
73
  reading_fibers.delete_at(reading_fibers.rindex(Fiber.current) || reading_fibers.size)
@@ -81,19 +77,14 @@ module Crystil
81
77
  # there would leave a caller who stops early — a cancel button, a
82
78
  # timeout guard — with a billed call and no record of it, which is
83
79
  # the exact failure this recorder exists to prevent.
84
- # A `break`/`throw` from the caller's block is a deliberate stop; the same exit while waiting
85
- # on the provider is a cut-off, e.g. `Timeout.timeout` (throws on Ruby 3.2; raises on 3.3+).
86
- failure ||= Crystil::StreamError.new(STREAM_CUT_SHORT_MESSAGE) unless finished || in_caller_block
87
-
88
- report.call(failure) if claim_report.call
80
+ report.call(finished: finished, error: error, caller_block: caller_block) if claim_report.call
89
81
  end
90
82
  end
91
83
 
92
84
  crystil_route_enumerators(stream)
85
+ # The app closing the stream before reading it to the end is a stop, with whatever was read.
93
86
  crystil_report_on_close(closer, reading_fibers, claim_report) do
94
- # Closed with nothing read is a call we saw start but not finish; after a partial read it is a deliberate
95
- # stop, reported like a `break`.
96
- report.call(events.empty? ? Crystil::StreamError.new(STREAM_CLOSED_UNREAD_MESSAGE) : nil)
87
+ report.call(finished: false, stopped: true)
97
88
  end
98
89
 
99
90
  stream
@@ -136,14 +127,18 @@ module Crystil
136
127
  # Anthropic event list always opens with `message_start`, so any non-empty
137
128
  # capture is one the backend extractor can read; an empty one is not, and
138
129
  # falls back to the plain error shape, or to sending nothing at all.
139
- def crystil_report_event_stream(events:, error:, method:, kwargs:, start_time:, title: nil, version: nil)
130
+ #
131
+ # A read that did not finish is `stopped` when the app ended it (see
132
+ # `StoppedCall`) and `failed` otherwise; `stopped` says so outright for a `close`.
133
+ def crystil_report_event_stream(events:, finished:, error:, caller_block:, method:, kwargs:, start_time:,
134
+ stopped: nil, title: nil, version: nil)
140
135
  response = events.empty? ? nil : events
141
136
 
142
- if error
143
- crystil_submit_error_analytics(
144
- method: method, args: [], kwargs: kwargs, error: error,
145
- start_time: start_time, end_time: Time.now,
146
- title: title, version: version, response: response
137
+ if !finished
138
+ crystil_submit_unfinished_analytics(
139
+ error, caller_block,
140
+ method: method, kwargs: kwargs, start_time: start_time, title: title, version: version,
141
+ received: response, failed_response: response, stopped: stopped
147
142
  )
148
143
  elsif response
149
144
  crystil_submit_analytics(
@@ -55,6 +55,17 @@ module Crystil
55
55
  parameters.merge(model: model)
56
56
  end
57
57
 
58
+ # The block the gem calls with each streamed event: it keeps the event, then
59
+ # calls the app's block, so a stream the app stops is recorded with what arrived.
60
+ def self.watched_block(caller_block, received)
61
+ return nil if caller_block.nil?
62
+
63
+ proc do |event, *rest|
64
+ received << event
65
+ caller_block.call(event, *rest)
66
+ end
67
+ end
68
+
58
69
  private
59
70
 
60
71
  def validate_client!(client)
@@ -90,17 +101,23 @@ module Crystil
90
101
  model = Crystil::Wrappers::GeminiAI.extract_model(self)
91
102
  recorded_query = Crystil::Wrappers::GeminiAI.parameters_with_model(parameters, model)
92
103
 
93
- sentinel = instance_variable_get(:@crystil_sentinel)
94
- sentinel&.raise_if_irrelevant!(
104
+ crystil_raise_if_irrelevant!(
95
105
  title: GOOGLE_CLIENT_TITLE,
96
106
  request: recorded_query,
107
+ start_time: start_time,
97
108
  version: version
98
109
  )
110
+ passed_sentinel = true
99
111
 
100
- # Call original method
101
- response = original_generate_content(*args, **kwargs, &block)
112
+ # A block makes the gem stream. It is wrapped to keep each event, so a stop is recorded with them.
113
+ received = []
114
+ caller_block = block && StoppedCall::CallerBlock.new(block)
115
+ response = original_generate_content(
116
+ *args, **kwargs, &Crystil::Wrappers::GeminiAI.watched_block(caller_block, received)
117
+ )
102
118
 
103
119
  # Submit analytics
120
+ recorded = true
104
121
  crystil_submit_analytics(
105
122
  method: :generate_content,
106
123
  args: [],
@@ -113,22 +130,21 @@ module Crystil
113
130
  )
114
131
 
115
132
  response
116
- rescue CrystilRequestInterceptedError => e
117
- # We don't want to send intercepts to collector
118
- raise e
119
- rescue StandardError => e
120
- crystil_submit_error_analytics(
121
- method: :generate_content,
122
- args: [],
123
- kwargs: recorded_query,
124
- error: e,
125
- start_time: start_time,
126
- end_time: Time.now,
127
- title: GOOGLE_CLIENT_TITLE,
128
- version: version
129
- )
133
+ rescue SignalException, StandardError => e
134
+ # A block from a nested call falls through: this call reached the provider, so it failed.
135
+ raise e if crystil_block_already_recorded?(e, passed_sentinel)
130
136
 
137
+ error = e
131
138
  raise e
139
+ ensure
140
+ # Recorded here, not in the rescue: a `break` or `throw` from the app's block raises nothing.
141
+ if !recorded && passed_sentinel
142
+ crystil_submit_unfinished_analytics(
143
+ error, caller_block,
144
+ method: :generate_content, kwargs: recorded_query, received: received,
145
+ start_time: start_time, title: GOOGLE_CLIENT_TITLE, version: version
146
+ )
147
+ end
132
148
  end
133
149
  end
134
150
  end
@@ -162,17 +178,23 @@ module Crystil
162
178
  model = Crystil::Wrappers::GeminiAI.extract_model(self)
163
179
  recorded_query = Crystil::Wrappers::GeminiAI.parameters_with_model(parameters, model)
164
180
 
165
- sentinel = instance_variable_get(:@crystil_sentinel)
166
- sentinel&.raise_if_irrelevant!(
181
+ crystil_raise_if_irrelevant!(
167
182
  title: GOOGLE_CLIENT_TITLE,
168
183
  request: recorded_query,
184
+ start_time: start_time,
169
185
  version: version
170
186
  )
187
+ passed_sentinel = true
171
188
 
172
- # Call original method with wrapped block
173
- response = original_stream_generate_content(*args, **kwargs, &block)
189
+ # A block makes the gem stream. It is wrapped to keep each event, so a stop is recorded with them.
190
+ received = []
191
+ caller_block = block && StoppedCall::CallerBlock.new(block)
192
+ response = original_stream_generate_content(
193
+ *args, **kwargs, &Crystil::Wrappers::GeminiAI.watched_block(caller_block, received)
194
+ )
174
195
 
175
196
  # Submit analytics with accumulated response
197
+ recorded = true
176
198
  crystil_submit_analytics(
177
199
  method: :stream_generate_content,
178
200
  args: [],
@@ -185,22 +207,21 @@ module Crystil
185
207
  )
186
208
 
187
209
  response
188
- rescue CrystilRequestInterceptedError => e
189
- # We don't want to send intercepts to collector
190
- raise e
191
- rescue StandardError => e
192
- crystil_submit_error_analytics(
193
- method: :stream_generate_content,
194
- args: [],
195
- kwargs: recorded_query,
196
- error: e,
197
- start_time: start_time,
198
- end_time: Time.now,
199
- title: GOOGLE_CLIENT_TITLE,
200
- version: version
201
- )
210
+ rescue SignalException, StandardError => e
211
+ # A block from a nested call falls through: this call reached the provider, so it failed.
212
+ raise e if crystil_block_already_recorded?(e, passed_sentinel)
202
213
 
214
+ error = e
203
215
  raise e
216
+ ensure
217
+ # Recorded here, not in the rescue: a `break` or `throw` from the app's block raises nothing.
218
+ if !recorded && passed_sentinel
219
+ crystil_submit_unfinished_analytics(
220
+ error, caller_block,
221
+ method: :stream_generate_content, kwargs: recorded_query, received: received,
222
+ start_time: start_time, title: GOOGLE_CLIENT_TITLE, version: version
223
+ )
224
+ end
204
225
  end
205
226
  end
206
227
  end
@@ -81,12 +81,13 @@ module Crystil
81
81
  # Extract parameters for analytics
82
82
  params = kwargs.any? ? kwargs : (args.first || {})
83
83
 
84
- sentinel = instance_variable_get(:@crystil_sentinel)
85
- sentinel&.raise_if_irrelevant!(
84
+ crystil_raise_if_irrelevant!(
86
85
  title: GOOGLE_CLIENT_TITLE,
87
86
  request: params,
87
+ start_time: start_time,
88
88
  version: version
89
89
  )
90
+ passed_sentinel = true
90
91
 
91
92
  # Call original method
92
93
  response = if kwargs.any?
@@ -96,6 +97,7 @@ module Crystil
96
97
  end
97
98
 
98
99
  # Submit analytics
100
+ recorded = true
99
101
  crystil_submit_analytics(
100
102
  method: :generate_content,
101
103
  args: args,
@@ -108,24 +110,22 @@ module Crystil
108
110
  )
109
111
 
110
112
  response
111
- rescue CrystilRequestInterceptedError => e
112
- # We don't want to send intercepts to collector
113
- raise e
114
- rescue StandardError => e
115
- params = kwargs.any? ? kwargs : (args.first || {})
116
-
117
- crystil_submit_error_analytics(
118
- method: :generate_content,
119
- args: args,
120
- kwargs: params,
121
- error: e,
122
- start_time: start_time,
123
- end_time: Time.now,
124
- title: GOOGLE_CLIENT_TITLE,
125
- version: version
126
- )
113
+ rescue SignalException, StandardError => e
114
+ # Recorded as blocked where Sentinel answers (crystil_raise_if_irrelevant!), never as a failure.
115
+ raise e if crystil_block_already_recorded?(e, passed_sentinel)
127
116
 
117
+ error = e
128
118
  raise e
119
+ ensure
120
+ # Recorded here, not in the rescue: a timeout or a killed thread skips the rescue.
121
+ # A signal is recorded as stopped, anything else as failed; a call that never passed Sentinel is not.
122
+ if !recorded && passed_sentinel
123
+ crystil_submit_unfinished_analytics(
124
+ error, nil,
125
+ method: :generate_content, kwargs: params,
126
+ start_time: start_time, title: GOOGLE_CLIENT_TITLE, version: version
127
+ )
128
+ end
129
129
  end
130
130
  end
131
131
  end