|
| 1 | +module McpTools |
| 2 | + class FailingServers < MCP::Tool |
| 3 | + extend Helpers |
| 4 | + |
| 5 | + MAX_MATCHES = 2000 |
| 6 | + |
| 7 | + tool_name "failing_servers" |
| 8 | + description "Given a failure log substring (e.g. a test name or assertion message), list the " \ |
| 9 | + "(server, branch, option) configurations where that failure occurred, with first " \ |
| 10 | + "and last occurrence and whether the latest build of each configuration still has " \ |
| 11 | + "it. Distinguishes an environment-specific failure from a global regression." |
| 12 | + input_schema( |
| 13 | + properties: { |
| 14 | + query: { type: "string", description: "Substring to match in failure logs" }, |
| 15 | + branch: { type: "string", description: "Filter by branch, e.g. 'master'" }, |
| 16 | + from: { type: "string", description: "Start date (YYYY-MM-DD, UTC; default 14 days ago)" }, |
| 17 | + to: { type: "string", description: "End date (YYYY-MM-DD, UTC, inclusive)" }, |
| 18 | + }, |
| 19 | + required: ["query"], |
| 20 | + ) |
| 21 | + annotations(Helpers::READ_ONLY) |
| 22 | + |
| 23 | + def self.call(query:, branch: nil, from: nil, to: nil, server_context: nil) |
| 24 | + return error_response("query must not be blank") if query.strip.empty? |
| 25 | + from_t = parse_time(from) || 14.days.ago.utc |
| 26 | + to_t = parse_time(to, end_of_day: true) |
| 27 | + |
| 28 | + scope = Report.joins(:log_excerpt).merge(LogExcerpt.content_match(query)). |
| 29 | + includes(:server). |
| 30 | + where("reports.datetime >= ?", from_t). |
| 31 | + order("reports.datetime ASC").limit(MAX_MATCHES) |
| 32 | + scope = scope.where("reports.datetime <= ?", to_t) if to_t |
| 33 | + scope = scope.where(branch: branch) if branch.present? |
| 34 | + matches = scope.to_a.reject { |r| r.server.nil? } |
| 35 | + if matches.empty? |
| 36 | + return json_response(query: query, branch: branch, message: "No matching failure in the window") |
| 37 | + end |
| 38 | + match_ids = matches.map(&:id).to_set |
| 39 | + |
| 40 | + configs = matches.group_by { |r| [r.server_id, r.branch, r.option] }.map do |(server_id, br, opt), reports| |
| 41 | + first = reports.first |
| 42 | + last = reports.last |
| 43 | + latest = Report.where(server_id: server_id, branch: br, option: opt).order(datetime: :desc).first |
| 44 | + { |
| 45 | + server: first.server.name, |
| 46 | + server_id: server_id, |
| 47 | + branch: br, |
| 48 | + option: opt, |
| 49 | + occurrences: reports.size, |
| 50 | + still_failing: match_ids.include?(latest.id), |
| 51 | + first_seen: first.as_mcp_json, |
| 52 | + last_seen: last.as_mcp_json.merge(matching_line: last.log_excerpt&.matching_line(query)).compact, |
| 53 | + } |
| 54 | + end |
| 55 | + configs.sort_by! { |c| c[:first_seen][:datetime] } |
| 56 | + |
| 57 | + json_response( |
| 58 | + query: query, |
| 59 | + branch: branch, |
| 60 | + from: from_t.iso8601, |
| 61 | + to: to_t&.iso8601, |
| 62 | + config_count: configs.size, |
| 63 | + total_occurrences: matches.size, |
| 64 | + truncated: matches.size >= MAX_MATCHES, |
| 65 | + configs: configs, |
| 66 | + ) |
| 67 | + end |
| 68 | + end |
| 69 | +end |
0 commit comments