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 +4 -4
- data/lib/posthog/feature_flag_evaluations.rb +0 -1
- data/lib/posthog/feature_flags.rb +0 -1
- data/lib/posthog/mcp/README.md +0 -3
- data/lib/posthog/mcp/analytics.rb +4 -4
- data/lib/posthog/mcp/ids.rb +1 -1
- data/lib/posthog/mcp/instrumentation.rb +5 -5
- data/lib/posthog/mcp/request_scope.rb +4 -10
- data/lib/posthog/mcp/sanitization.rb +66 -38
- data/lib/posthog/mcp/tracking_data.rb +2 -2
- data/lib/posthog/mcp.rb +3 -0
- data/lib/posthog/transport.rb +2 -2
- data/lib/posthog/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 74ded46673020dda3dfaaf97e5911e0075f34b3c9eb482ba5ba3af9f2c67207a
|
|
4
|
+
data.tar.gz: 4a8609d238306cccbc1b80dd263dec54dd9a7317461cce2e15abfea9d1d2fac6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 57ebfdb7c813f13604d5ba589d16188076609e5945cba79b7bfd822380800b7aba81f9d41c085fcd20974c0a489b2cad32b13c9b73cc993b3cffdf6c022134d3
|
|
7
|
+
data.tar.gz: b02e0e22a71a2e2af00b31498f4ca05b137b4468500cce5966c8fb5fd22a6a90084f2ed0c43930e96bd957b35783da82b19d54222a42ee49d5313df449724bd6
|
data/lib/posthog/mcp/README.md
CHANGED
|
@@ -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
|
|
67
|
-
#
|
|
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.
|
|
94
|
-
'
|
|
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
|
data/lib/posthog/mcp/ids.rb
CHANGED
|
@@ -11,7 +11,7 @@ module PostHog
|
|
|
11
11
|
module Ids
|
|
12
12
|
module_function
|
|
13
13
|
|
|
14
|
-
# RFC 9562 UUIDv7 (time-ordered), implemented inline
|
|
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(&
|
|
127
|
+
def dispatch_kind(&)
|
|
128
128
|
case @kind
|
|
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(&
|
|
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[]
|
|
12
|
-
#
|
|
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
|
-
|
|
28
|
+
Fiber[KEY]
|
|
31
29
|
end
|
|
32
30
|
|
|
33
31
|
def current=(value)
|
|
34
|
-
|
|
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
|
-
|
|
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][
|
|
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 <<
|
|
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
|
|
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'] =
|
|
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
|
-
|
|
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
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
params.
|
|
357
|
-
|
|
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.
|
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
|
|
data/lib/posthog/transport.rb
CHANGED
|
@@ -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, &
|
|
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, &
|
|
140
|
+
retry_with_backoff(retries_remaining - 1, &)
|
|
141
141
|
else
|
|
142
142
|
[result, caught_exception]
|
|
143
143
|
end
|
data/lib/posthog/version.rb
CHANGED
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.
|
|
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.
|
|
103
|
+
version: '3.2'
|
|
104
104
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
105
105
|
requirements:
|
|
106
106
|
- - ">="
|