sentrace 0.0.1

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 (55) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +192 -0
  4. data/app/controllers/sentrace/application_controller.rb +4 -0
  5. data/app/controllers/sentrace/ingestion_controller.rb +169 -0
  6. data/app/controllers/sentrace/internal/base_controller.rb +47 -0
  7. data/app/controllers/sentrace/internal/events_controller.rb +25 -0
  8. data/app/controllers/sentrace/internal/issues_controller.rb +36 -0
  9. data/app/controllers/sentrace/internal/projects_controller.rb +58 -0
  10. data/app/controllers/sentrace/internal/stats_controller.rb +10 -0
  11. data/app/controllers/sentrace/mcp_controller.rb +74 -0
  12. data/app/jobs/sentrace/retention_cleanup_job.rb +9 -0
  13. data/app/models/sentrace/application_record.rb +5 -0
  14. data/app/models/sentrace/event.rb +41 -0
  15. data/app/models/sentrace/issue.rb +32 -0
  16. data/app/models/sentrace/mcp_token.rb +82 -0
  17. data/app/models/sentrace/project.rb +50 -0
  18. data/app/models/sentrace/project_key.rb +60 -0
  19. data/config/routes.rb +11 -0
  20. data/db/migrate/20261116000001_create_sentrace_projects.rb +12 -0
  21. data/db/migrate/20261116000002_create_sentrace_project_keys.rb +14 -0
  22. data/db/migrate/20261116000003_create_sentrace_issues.rb +25 -0
  23. data/db/migrate/20261116000004_create_sentrace_events.rb +31 -0
  24. data/db/migrate/20261116000005_create_sentrace_mcp_tokens.rb +17 -0
  25. data/lib/generators/sentrace/install/install_generator.rb +91 -0
  26. data/lib/generators/sentrace/install/templates/initializer.rb.tt +34 -0
  27. data/lib/generators/sentrace/install/templates/sentrace_setup.rake.tt +21 -0
  28. data/lib/sentrace/benchmark/storage_runner.rb +289 -0
  29. data/lib/sentrace/configuration.rb +198 -0
  30. data/lib/sentrace/dsn.rb +123 -0
  31. data/lib/sentrace/engine.rb +10 -0
  32. data/lib/sentrace/errors.rb +20 -0
  33. data/lib/sentrace/ingestion/envelope_parser.rb +143 -0
  34. data/lib/sentrace/ingestion/event_normalizer.rb +315 -0
  35. data/lib/sentrace/ingestion/processor.rb +90 -0
  36. data/lib/sentrace/ingestion/sanitizer.rb +293 -0
  37. data/lib/sentrace/issues/fingerprint.rb +177 -0
  38. data/lib/sentrace/issues/grouper.rb +184 -0
  39. data/lib/sentrace/mcp/dispatcher.rb +23 -0
  40. data/lib/sentrace/mcp/environment_router.rb +266 -0
  41. data/lib/sentrace/mcp/local_backend.rb +425 -0
  42. data/lib/sentrace/mcp/remote_http_client.rb +310 -0
  43. data/lib/sentrace/mcp/server.rb +417 -0
  44. data/lib/sentrace/mcp/stdio_runner.rb +55 -0
  45. data/lib/sentrace/mcp/token_manager.rb +73 -0
  46. data/lib/sentrace/project_manager.rb +64 -0
  47. data/lib/sentrace/retention/cleaner.rb +105 -0
  48. data/lib/sentrace/storage_stats.rb +136 -0
  49. data/lib/sentrace/version.rb +3 -0
  50. data/lib/sentrace.rb +40 -0
  51. data/lib/tasks/sentrace_benchmark.rake +9 -0
  52. data/lib/tasks/sentrace_mcp.rake +36 -0
  53. data/lib/tasks/sentrace_projects.rake +22 -0
  54. data/lib/tasks/sentrace_retention.rake +16 -0
  55. metadata +148 -0
@@ -0,0 +1,310 @@
1
+ require "json"
2
+ require "openssl"
3
+ require "securerandom"
4
+ require "timeout"
5
+ require "uri"
6
+
7
+ module Sentrace
8
+ module Mcp
9
+ class RemoteHttpClient
10
+ HttpResponse = Data.define(:status, :headers, :body)
11
+
12
+ SUPPORTED_OPERATIONS = %w[
13
+ list_projects
14
+ list_issues
15
+ get_issue
16
+ update_issue_status
17
+ list_events
18
+ get_event
19
+ get_error_trends
20
+ ].freeze
21
+
22
+ REQUEST_CONTENT_TYPE = "application/json".freeze
23
+ REQUEST_ACCEPT = "application/json, text/event-stream".freeze
24
+
25
+ def initialize(http_requester: nil)
26
+ @http_requester = http_requester || method(:perform_net_http_post)
27
+ end
28
+
29
+ def call_environment(env_def, operation, **kwargs)
30
+ op_name = operation.to_s
31
+ unless SUPPORTED_OPERATIONS.include?(op_name)
32
+ raise McpInvalidParamsError, "Unsupported remote MCP operation #{op_name.inspect}"
33
+ end
34
+
35
+ arguments = kwargs.compact.transform_keys(&:to_s)
36
+ body = JSON.generate(
37
+ jsonrpc: "2.0",
38
+ id: SecureRandom.uuid,
39
+ method: "tools/call",
40
+ params: {
41
+ name: op_name,
42
+ arguments: arguments
43
+ }
44
+ )
45
+
46
+ response = post_json_rpc(env_def, body)
47
+ unwrap_tool_response!(env_def, response)
48
+ end
49
+
50
+ def environment_status(env_def)
51
+ return "unconfigured" if env_def.url.to_s.strip.empty?
52
+
53
+ body = JSON.generate(
54
+ jsonrpc: "2.0",
55
+ id: "ping",
56
+ method: "ping",
57
+ params: {}
58
+ )
59
+ response = post_json_rpc(env_def, body)
60
+
61
+ case response.status
62
+ when 200..299
63
+ parsed = JSON.parse(response.body.to_s)
64
+ parsed.is_a?(Hash) && parsed.key?("result") ? "connected" : "protocol_error"
65
+ when 401, 403
66
+ "authentication_failed"
67
+ else
68
+ "unreachable"
69
+ end
70
+ rescue McpRemoteError => e
71
+ e.category
72
+ rescue JSON::ParserError
73
+ "protocol_error"
74
+ end
75
+
76
+ private
77
+ def post_json_rpc(env_def, body)
78
+ uri = validate_endpoint!(env_def)
79
+ headers = build_headers(env_def)
80
+ timeout = env_def.timeout
81
+
82
+ @http_requester.call(uri: uri, headers: headers, body: body, timeout: timeout)
83
+ rescue McpError
84
+ raise
85
+ rescue Timeout::Error
86
+ raise McpRemoteError.new(
87
+ "Timed out after #{env_def.timeout}s querying environment #{env_def.name}",
88
+ environment: env_def.name,
89
+ category: "timeout"
90
+ )
91
+ rescue SocketError, EOFError, IOError, SystemCallError, OpenSSL::SSL::SSLError
92
+ raise McpRemoteError.new(
93
+ "Could not reach environment #{env_def.name}",
94
+ environment: env_def.name,
95
+ category: "unreachable"
96
+ )
97
+ end
98
+
99
+ def validate_endpoint!(env_def)
100
+ raw_url = env_def.url.to_s.strip
101
+ if raw_url.empty?
102
+ raise McpRemoteError.new(
103
+ "Missing MCP endpoint URL for environment #{env_def.name}",
104
+ environment: env_def.name,
105
+ category: "invalid_endpoint"
106
+ )
107
+ end
108
+
109
+ uri = URI.parse(raw_url)
110
+ unless uri.is_a?(URI::HTTP) && uri.host && !uri.host.empty?
111
+ raise McpRemoteError.new(
112
+ "Invalid MCP endpoint URL for environment #{env_def.name}",
113
+ environment: env_def.name,
114
+ category: "invalid_endpoint"
115
+ )
116
+ end
117
+
118
+ if uri.scheme != "https" && !Configuration::LOOPBACK_HOSTS.include?(uri.host.downcase)
119
+ raise McpRemoteError.new(
120
+ "HTTPS is required for remote environment #{env_def.name}",
121
+ environment: env_def.name,
122
+ category: "insecure_transport"
123
+ )
124
+ end
125
+
126
+ uri
127
+ rescue URI::InvalidURIError
128
+ raise McpRemoteError.new(
129
+ "Malformed MCP endpoint URL for environment #{env_def.name}",
130
+ environment: env_def.name,
131
+ category: "invalid_endpoint"
132
+ )
133
+ end
134
+
135
+ def build_headers(env_def)
136
+ headers = {
137
+ "Content-Type" => REQUEST_CONTENT_TYPE,
138
+ "Accept" => REQUEST_ACCEPT
139
+ }
140
+ token = env_def.token.to_s
141
+ headers["Authorization"] = "Bearer #{token}" unless token.empty?
142
+ headers
143
+ end
144
+
145
+ def unwrap_tool_response!(env_def, response)
146
+ status = response.status.to_i
147
+
148
+ case status
149
+ when 401, 403
150
+ raise McpRemoteError.new(
151
+ "Authentication failed for environment #{env_def.name} (HTTP #{status})",
152
+ environment: env_def.name,
153
+ category: "authentication_failed"
154
+ )
155
+ when 404
156
+ raise McpRemoteError.new(
157
+ "MCP endpoint not found for environment #{env_def.name} (HTTP 404)",
158
+ environment: env_def.name,
159
+ category: "unreachable"
160
+ )
161
+ when 413
162
+ raise McpRemoteError.new(
163
+ "Request payload rejected as too large by environment #{env_def.name}",
164
+ environment: env_def.name,
165
+ category: "protocol_error"
166
+ )
167
+ when 200..299
168
+ parse_json_rpc_result!(env_def, response.body.to_s)
169
+ else
170
+ raise McpRemoteError.new(
171
+ "Remote environment #{env_def.name} returned HTTP #{status}",
172
+ environment: env_def.name,
173
+ category: "unreachable"
174
+ )
175
+ end
176
+ end
177
+
178
+ def parse_json_rpc_result!(env_def, raw_body)
179
+ parsed = JSON.parse(raw_body)
180
+ unless parsed.is_a?(Hash)
181
+ raise McpRemoteError.new(
182
+ "Malformed JSON-RPC envelope from environment #{env_def.name}",
183
+ environment: env_def.name,
184
+ category: "protocol_error"
185
+ )
186
+ end
187
+
188
+ if parsed["error"].is_a?(Hash)
189
+ rpc_msg = scrub_secret(parsed.dig("error", "message") || "JSON-RPC error", env_def)
190
+ raise McpRemoteError.new(
191
+ "Remote MCP error from #{env_def.name}: #{rpc_msg}",
192
+ environment: env_def.name,
193
+ category: "protocol_error"
194
+ )
195
+ end
196
+
197
+ result = parsed["result"]
198
+ unless result.is_a?(Hash)
199
+ raise McpRemoteError.new(
200
+ "Missing JSON-RPC result from environment #{env_def.name}",
201
+ environment: env_def.name,
202
+ category: "protocol_error"
203
+ )
204
+ end
205
+
206
+ structured = result["structuredContent"] || parse_text_content_fallback(result["content"])
207
+
208
+ if result["isError"] == true
209
+ handle_remote_tool_error!(env_def, structured)
210
+ end
211
+
212
+ unless structured.is_a?(Hash)
213
+ raise McpRemoteError.new(
214
+ "Missing structured tool payload from environment #{env_def.name}",
215
+ environment: env_def.name,
216
+ category: "protocol_error"
217
+ )
218
+ end
219
+
220
+ normalize_environment_attribution(structured, env_def.name.to_s)
221
+ rescue JSON::ParserError
222
+ raise McpRemoteError.new(
223
+ "Invalid JSON response from environment #{env_def.name}",
224
+ environment: env_def.name,
225
+ category: "protocol_error"
226
+ )
227
+ end
228
+
229
+ def handle_remote_tool_error!(env_def, structured)
230
+ category = structured.is_a?(Hash) ? structured["category"].to_s : ""
231
+ raw_msg = structured.is_a?(Hash) ? structured["error"] : nil
232
+ safe_msg = scrub_secret(raw_msg || "Remote tool execution failed in #{env_def.name}", env_def)
233
+
234
+ case category
235
+ when "not_found"
236
+ raise McpNotFoundError, safe_msg
237
+ when "invalid_params"
238
+ raise McpInvalidParamsError, safe_msg
239
+ else
240
+ raise McpRemoteError.new(
241
+ safe_msg,
242
+ environment: env_def.name,
243
+ category: "remote_tool_error"
244
+ )
245
+ end
246
+ end
247
+
248
+ def parse_text_content_fallback(content_list)
249
+ return nil unless content_list.is_a?(Array)
250
+
251
+ first_text = content_list.find { |item| item.is_a?(Hash) && item["type"] == "text" && item["text"].is_a?(String) }
252
+ return nil unless first_text
253
+
254
+ JSON.parse(first_text["text"])
255
+ rescue JSON::ParserError
256
+ nil
257
+ end
258
+
259
+ def normalize_environment_attribution(payload, target_env)
260
+ remote_env = payload["environment"].to_s.strip
261
+ normalized = payload.merge("environment" => target_env)
262
+ if !remote_env.empty? && remote_env != target_env
263
+ normalized["remote_deployment_environment"] = remote_env
264
+ end
265
+
266
+ %w[projects issues events].each do |collection_key|
267
+ next unless normalized[collection_key].is_a?(Array)
268
+
269
+ normalized[collection_key] = normalized[collection_key].map do |item|
270
+ item.is_a?(Hash) ? item.merge("environment" => target_env) : item
271
+ end
272
+ end
273
+
274
+ %w[issue event].each do |record_key|
275
+ next unless normalized[record_key].is_a?(Hash)
276
+
277
+ normalized[record_key] = normalized[record_key].merge("environment" => target_env)
278
+ end
279
+
280
+ normalized
281
+ end
282
+
283
+ def scrub_secret(message, env_def)
284
+ text = message.to_s.dup
285
+ secret = env_def.token.to_s
286
+ text.gsub!(secret, "[REDACTED]") unless secret.empty?
287
+ text
288
+ end
289
+
290
+ def perform_net_http_post(uri:, headers:, body:, timeout:)
291
+ require "net/http"
292
+ http = Net::HTTP.new(uri.host, uri.port)
293
+ http.use_ssl = (uri.scheme == "https")
294
+ http.open_timeout = timeout
295
+ http.read_timeout = timeout
296
+ http.write_timeout = timeout if http.respond_to?(:write_timeout=)
297
+
298
+ request = Net::HTTP::Post.new(uri.request_uri, headers)
299
+ request.body = body
300
+
301
+ response = http.request(request)
302
+ HttpResponse.new(
303
+ status: response.code.to_i,
304
+ headers: response.each_header.to_h,
305
+ body: response.body.to_s
306
+ )
307
+ end
308
+ end
309
+ end
310
+ end