posthog-ruby 3.34.3 → 3.35.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93c8a3304a464eb03c4ed4801f27c591ab001fa60077ea87503cac3ff5cb00de
4
- data.tar.gz: 3c90a896cad52dcd061fd6e954cf9041cda10b910662759dd412cfee998d7bc1
3
+ metadata.gz: 74ded46673020dda3dfaaf97e5911e0075f34b3c9eb482ba5ba3af9f2c67207a
4
+ data.tar.gz: 4a8609d238306cccbc1b80dd263dec54dd9a7317461cce2e15abfea9d1d2fac6
5
5
  SHA512:
6
- metadata.gz: 43bac42e2f95c9969fce0358edaa83e94a26099aa62b17f4dfd168cfdfbcc94d28a21587db7ee2198fa7bf3d88a1b2460bbf113b0680f9369a1c72f263eb5f97
7
- data.tar.gz: 3a4f6e66fd8e63ca10bd7a68d104a97aa1a894fdca96d011f8a76de8cd87fff44e00366597c4f9973822d94c2c235e37087a5b66fdba66b018c767c888930e6b
6
+ metadata.gz: 57ebfdb7c813f13604d5ba589d16188076609e5945cba79b7bfd822380800b7aba81f9d41c085fcd20974c0a489b2cad32b13c9b73cc993b3cffdf6c022134d3
7
+ data.tar.gz: b02e0e22a71a2e2af00b31498f4ca05b137b4468500cce5966c8fb5fd22a6a90084f2ed0c43930e96bd957b35783da82b19d54222a42ee49d5313df449724bd6
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'set'
4
3
  require 'posthog/feature_flag_error'
5
4
 
6
5
  module PostHog
@@ -12,7 +12,6 @@ require 'posthog/flag_definition_cache'
12
12
  require 'posthog/unresolved_flag_reason'
13
13
  require 'posthog/flag_evaluation_errors'
14
14
  require 'digest'
15
- require 'set'
16
15
 
17
16
  module PostHog
18
17
  # Polls and evaluates feature flag definitions for {PostHog::Client}.
@@ -29,9 +29,6 @@ These are the few things that differ from what the docs describe for the other S
29
29
  * **Truncation is tighter.** Events are truncated to fit the core client's 32KB per-message limit,
30
30
  because posthog-ruby drops larger messages at batch time - truncating harder beats losing the
31
31
  event.
32
- * **Request scope needs Ruby 3.2+ to be inherited.** A custom event captured from a thread or fiber
33
- a tool spawns is attributed to that request on 3.2+ (fiber storage); before 3.2 capture from the
34
- tool body itself, or an HTTP server falls back to a standalone session.
35
32
  * **Composed schemas are left alone.** A tool whose `input_schema` is `oneOf`/`allOf`/`anyOf` or a
36
33
  `$ref` has nothing injected into it and nothing stripped from its calls.
37
34
  * **stdio servers own `$stdout`.** The integration's own messages go only to the `logger:` you pass
@@ -63,8 +63,8 @@ module PostHog
63
63
  # that has served an HTTP request the server-wide session is not a safe
64
64
  # fallback: it belongs to whichever request settled it last, which under
65
65
  # concurrency is somebody else's. That happens when a tool hands its work to
66
- # a thread or fiber it spawned on Ruby 3.0/3.1, where {RequestScope} is
67
- # fiber-local and is not inherited. Fail closed with a standalone session
66
+ # a thread that did not inherit the request's fiber storage, such as a pool
67
+ # created before the request. Fail closed with a standalone session
68
68
  # rather than filing the event under another caller's identity; over stdio a
69
69
  # server only ever talks to one client, so the fallback stays.
70
70
  def current_session_id(data, scope)
@@ -90,8 +90,8 @@ module PostHog
90
90
  Log.warn(
91
91
  data.options,
92
92
  'Warning: analytics.capture() ran without the scope of the request that started it, so its event got ' \
93
- 'a standalone $session_id instead of the caller\'s. On Ruby 3.2+ the scope follows threads and fibers ' \
94
- 'a tool spawns; before that it does not, so capture custom events from the tool body itself.'
93
+ 'a standalone $session_id instead of the caller\'s. The scope follows threads and fibers a tool ' \
94
+ 'spawns, but not work handed to a thread created outside the request, such as an existing pool.'
95
95
  )
96
96
  end
97
97
  end
@@ -11,7 +11,7 @@ module PostHog
11
11
  module Ids
12
12
  module_function
13
13
 
14
- # RFC 9562 UUIDv7 (time-ordered), implemented inline so Ruby 3.0/3.1 work too.
14
+ # RFC 9562 UUIDv7 (time-ordered), implemented inline because `SecureRandom.uuid_v7` needs Ruby 3.3.
15
15
  #
16
16
  # @return [String] lowercase, hyphenated uuid
17
17
  def uuid_v7
@@ -124,12 +124,12 @@ module PostHog
124
124
 
125
125
  private
126
126
 
127
- def dispatch_kind(&handler)
127
+ def dispatch_kind(&)
128
128
  case @kind
129
- when :tools_call then dispatch_tool_call(&handler)
130
- when :tools_list then dispatch_tools_list(&handler)
131
- when :initialize then dispatch_initialize(&handler)
132
- else dispatch_generic(&handler)
129
+ when :tools_call then dispatch_tool_call(&)
130
+ when :tools_list then dispatch_tools_list(&)
131
+ when :initialize then dispatch_initialize(&)
132
+ else dispatch_generic(&)
133
133
  end
134
134
  end
135
135
 
@@ -8,14 +8,12 @@ module PostHog
8
8
  # HTTP headers cannot travel with the request object; this is the one place
9
9
  # the integration relies on ambient state.
10
10
  #
11
- # Uses fiber storage (`Fiber[]`, Ruby 3.2+), which is per fiber, isolated per
12
- # Ractor, and inherited by fibers/threads a tool spawns. Falls back to
13
- # fiber-local `Thread.current[]` on Ruby 3.0/3.1.
11
+ # Uses fiber storage (`Fiber[]`), which is per fiber, isolated per Ractor, and
12
+ # inherited by fibers/threads a tool spawns.
14
13
  #
15
14
  # @api private
16
15
  module RequestScope
17
16
  KEY = :posthog_mcp_request_scope
18
- FIBER_STORAGE = Fiber.respond_to?(:[]) && Fiber.respond_to?(:[]=)
19
17
 
20
18
  module_function
21
19
 
@@ -27,15 +25,11 @@ module PostHog
27
25
  # even while another request on the same server is in flight. `actor` is absent until
28
26
  # the request resolves identity, which is what tells {Analytics} it has none to use.
29
27
  def current
30
- FIBER_STORAGE ? Fiber[KEY] : Thread.current[KEY]
28
+ Fiber[KEY]
31
29
  end
32
30
 
33
31
  def current=(value)
34
- if FIBER_STORAGE
35
- Fiber[KEY] = value
36
- else
37
- Thread.current[KEY] = value
38
- end
32
+ Fiber[KEY] = value
39
33
  end
40
34
 
41
35
  # Rack `env` keys for the two request headers Rack stores without `HTTP_`.
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require 'set'
5
4
  require 'uri'
6
5
 
7
6
  module PostHog
@@ -44,7 +43,7 @@ module PostHog
44
43
  /x
45
44
  US_SSN_PATTERN = /\b\d{3}[ .-]\d{2}[ .-]\d{4}\b/
46
45
  CREDIT_CARD_CANDIDATE_PATTERN = %r{\b\d(?:[ ./-]?\d){12,}\b}
47
- DIGIT_GROUP_PATTERN = /\d+/
46
+ SEPARATED_DIGIT_GROUP_PATTERN = /(\D*)(\d+)/
48
47
  PHONE_NANP_PATTERN = %r{(?<![\w+])(?:\+?1[ ./-]?)?(?:\(\d{3}\)[ ./-]?|\d{3}[ ./-])\d{3}[ ./-]\d{4}(?!\w)}
49
48
  PHONE_INTL_PATTERN = %r{(?<!\w)\+\d{1,3}(?:[ ./()-]{0,2}\d){7,13}(?!\w)}
50
49
 
@@ -140,32 +139,32 @@ module PostHog
140
139
 
141
140
  # Within a card candidate, redact every run of whole separator-delimited
142
141
  # digit groups whose joined digits are 13-19 long and pass Luhn.
142
+ #
143
+ # Splits into `[separator, digits]` pairs rather than asking each match
144
+ # for its offset: `MatchData#begin` inside `scan` recounts characters from
145
+ # the start of the string, which made this pass quadratic.
143
146
  def redact_card_in_match(text)
144
- groups = []
145
- text.scan(DIGIT_GROUP_PATTERN) do
146
- groups << [Regexp.last_match[0], Regexp.last_match.begin(0), Regexp.last_match.end(0)]
147
- end
147
+ groups = text.scan(SEPARATED_DIGIT_GROUP_PATTERN)
148
148
  output = +''
149
- cursor = 0
150
149
  first = 0
151
150
  while first < groups.length
152
151
  digits = +''
153
152
  matched_last = -1
154
153
  (first...groups.length).each do |last|
155
- digits << groups[last][0]
154
+ digits << groups[last][1]
156
155
  break if digits.length > 19
157
156
 
158
157
  matched_last = last if digits.length >= 13 && passes_luhn?(digits)
159
158
  end
160
159
  if matched_last >= 0
161
- output << text[cursor...groups[first][1]] << REDACTED_VALUE
162
- cursor = groups[matched_last][2]
160
+ output << groups[first][0] << REDACTED_VALUE
163
161
  first = matched_last + 1
164
162
  else
163
+ output << groups[first][0] << groups[first][1]
165
164
  first += 1
166
165
  end
167
166
  end
168
- output << text[cursor..]
167
+ output
169
168
  end
170
169
 
171
170
  # Redact structured personal identifiers (emails, IPs, cards, US SSNs,
@@ -196,19 +195,63 @@ module PostHog
196
195
  end
197
196
  end
198
197
 
198
+ # {sanitize_captured_value} within truncation's depth and breadth limits,
199
+ # so the cost follows what is captured rather than what the caller sent.
200
+ # {Truncation.normalize} of the result equals it of a full sanitize.
201
+ def sanitize_limited_value(value, remaining_depth = Truncation::MAX_DEPTH)
202
+ case value
203
+ when Array
204
+ return '[Array]' if remaining_depth <= 0
205
+
206
+ limited = value.first(Truncation::MAX_BREADTH).map { |item| sanitize_limited_value(item, remaining_depth - 1) }
207
+ value.length > Truncation::MAX_BREADTH ? limited << '[MaxProperties ~]' : limited
208
+ when Hash
209
+ return '[Object]' if remaining_depth <= 0
210
+
211
+ limited = value.first(Truncation::MAX_BREADTH).to_h do |key, nested|
212
+ [key.to_s, redact_key?(key) ? REDACTED_VALUE : sanitize_limited_value(nested, remaining_depth - 1)]
213
+ end
214
+ limited['...'] = '[MaxProperties ~]' if value.length > Truncation::MAX_BREADTH
215
+ limited
216
+ else sanitize_captured_value(value)
217
+ end
218
+ end
219
+
199
220
  # Sanitize an event's response, parameters, intent and error. Returns a
200
221
  # new shallow copy; does not mutate the input.
201
222
  def sanitize_event(event)
202
223
  result = event.dup
203
224
  result['response'] = sanitize_response(result['response']) unless result['response'].nil?
204
- result['parameters'] = sanitize_captured_value(result['parameters']) unless result['parameters'].nil?
205
- unless result['user_intent'].nil?
206
- result['user_intent'] = redact_pii(sanitize_captured_value(result['user_intent']))
207
- end
225
+ result['parameters'] = sanitize_limited_value(result['parameters']) unless result['parameters'].nil?
226
+ result['user_intent'] = sanitize_intent(result['user_intent']) unless result['user_intent'].nil?
208
227
  result['error'] = sanitize_exception_values(result['error']) unless result['error'].nil?
209
228
  result
210
229
  end
211
230
 
231
+ # The credential pass is linear and sees the whole intent: a private key
232
+ # block or a long word is judged on its full length. The personal-data
233
+ # passes are not, so they scan a head chosen to give the same kept text.
234
+ def sanitize_intent(intent)
235
+ return sanitize_captured_value(intent) unless intent.is_a?(String)
236
+
237
+ redact_pii_head(sanitize_string(intent), Truncation::MAX_USER_INTENT_LENGTH)
238
+ end
239
+
240
+ # Cutting the text changes at most the last ~1.5K characters a scan
241
+ # returns: a match reaches back at most an email's 345 characters, and a
242
+ # redaction grows a match at most from 3 characters to 10. So a head whose scan returns more
243
+ # than twice `keep` gives the same first `keep` characters as the full
244
+ # text. Otherwise the head grows to 40x `keep`, which no run of
245
+ # redactions can shrink below `keep`: each leaves 10 characters of at
246
+ # most 345.
247
+ def redact_pii_head(text, keep)
248
+ head = text[0, 4 * keep]
249
+ redacted = redact_pii(head)
250
+ return redacted if head.length == text.length || redacted.length > 2 * keep
251
+
252
+ redact_pii(text[0, 40 * keep])
253
+ end
254
+
212
255
  def sanitize_exception_values(error)
213
256
  return error unless error.is_a?(Hash)
214
257
 
@@ -340,32 +383,17 @@ module PostHog
340
383
  # dropping the SDK-injected arguments (they surface as dedicated properties).
341
384
  def build_captured_mcp_parameters(request)
342
385
  request = stringify_keys(request)
343
- return { 'request' => sanitize_captured_value(request) } unless request.is_a?(Hash)
344
-
345
- captured = {}
346
- %w[id jsonrpc method].each do |key|
347
- captured[key] = sanitize_captured_value(request[key]) if request.key?(key)
348
- end
349
- captured['params'] = build_captured_params(request['params']) if request.key?('params')
350
- { 'request' => captured }
386
+ sanitize_limited_value('request' => request.is_a?(Hash) ? captured_request_fields(request) : request)
351
387
  end
352
388
 
353
- def build_captured_params(params)
354
- return sanitize_captured_value(params) unless params.is_a?(Hash)
355
-
356
- params.to_h do |key, value|
357
- [key, key == 'arguments' ? build_captured_arguments(value) : sanitize_captured_value(value)]
358
- end
359
- end
360
-
361
- def build_captured_arguments(arguments)
362
- return sanitize_captured_value(arguments) unless arguments.is_a?(Hash)
363
-
364
- arguments.each_with_object({}) do |(key, value), captured|
365
- next if INJECTED_ARGUMENT_NAMES.include?(key)
366
-
367
- captured[key] = sanitize_captured_value(value)
389
+ def captured_request_fields(request)
390
+ captured = request.slice('id', 'jsonrpc', 'method')
391
+ params = request['params']
392
+ if params.is_a?(Hash) && params['arguments'].is_a?(Hash)
393
+ params = params.merge('arguments' => params['arguments'].except(*INJECTED_ARGUMENT_NAMES))
368
394
  end
395
+ captured['params'] = params if request.key?('params')
396
+ captured
369
397
  end
370
398
 
371
399
  # Last-resort credential detection for bare words.
@@ -40,11 +40,11 @@ module PostHog
40
40
  @tool_owned_params = {}
41
41
  end
42
42
 
43
- def synchronize(&block)
43
+ def synchronize(&)
44
44
  if @mutex.owned?
45
45
  yield
46
46
  else
47
- @mutex.synchronize(&block)
47
+ @mutex.synchronize(&)
48
48
  end
49
49
  end
50
50
 
data/lib/posthog/mcp.rb CHANGED
@@ -63,8 +63,11 @@ module PostHog
63
63
  # @return [PostHog::MCP::Analytics] handle for custom events; a no-op handle when
64
64
  # instrumentation fails (logged, never raised)
65
65
  # @raise [LoadError] when the `mcp` gem is not available
66
+ # Named so the documented `kwargs` parameter stays in the public signature.
67
+ # rubocop:disable Style/ArgumentsForwarding
66
68
  def instrument(server, client = nil, options: nil, **kwargs)
67
69
  opts = options.is_a?(Options) ? options : Options.new(**kwargs)
70
+ # rubocop:enable Style/ArgumentsForwarding
68
71
  ensure_mcp_sdk!
69
72
  experimental_notice!(opts)
70
73
 
@@ -122,7 +122,7 @@ module PostHog
122
122
  # duration to sleep between attempts
123
123
  #
124
124
  # Returns [last_result, raised_exception]
125
- def retry_with_backoff(retries_remaining, &block)
125
+ def retry_with_backoff(retries_remaining, &)
126
126
  result, caught_exception = nil
127
127
  should_retry = false
128
128
 
@@ -137,7 +137,7 @@ module PostHog
137
137
  if should_retry && (retries_remaining > 1)
138
138
  logger.debug("Retrying request, #{retries_remaining} retries left")
139
139
  sleep(retry_delay_seconds)
140
- retry_with_backoff(retries_remaining - 1, &block)
140
+ retry_with_backoff(retries_remaining - 1, &)
141
141
  else
142
142
  [result, caught_exception]
143
143
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PostHog
4
- VERSION = '3.34.3'
4
+ VERSION = '3.35.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: posthog-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.34.3
4
+ version: 3.35.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
@@ -100,7 +100,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
100
100
  requirements:
101
101
  - - ">="
102
102
  - !ruby/object:Gem::Version
103
- version: '3.0'
103
+ version: '3.2'
104
104
  required_rubygems_version: !ruby/object:Gem::Requirement
105
105
  requirements:
106
106
  - - ">="