Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/mcp-core/src/internal/formatting.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { getSeerActionabilityLabel } from "./formatting";
import { getSeerActionabilityLabel, selectN1EvidenceSpans } from "./formatting";

describe("getSeerActionabilityLabel", () => {
it("returns correct labels for threshold boundaries", () => {
Expand Down Expand Up @@ -41,3 +41,14 @@ describe("getSeerActionabilityLabel", () => {
expect(getSeerActionabilityLabel(0)).toBe("super_low");
});
});

describe("selectN1EvidenceSpans", () => {
it("falls back to the offending span IDs without a trace or spans entry", () => {
expect(
selectN1EvidenceSpans(
{ offenderSpanIds: ["aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb"] },
undefined,
).map((span) => span.span_id),
).toEqual(["aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb"]);
});
});
21 changes: 12 additions & 9 deletions packages/mcp-core/src/internal/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,19 +318,22 @@ export function selectN1EvidenceSpans(
MAX_SPANS_IN_TREE,
)
: [];
if (traceRoots.length > 0 || !isValidSpanArray(spansData)) {
if (traceRoots.length > 0) {
return traceRoots;
}
const spanTree = selectN1QuerySpans(
spansData,
evidenceData,
MAX_SPANS_IN_TREE,
);
if (spanTree.length > 0) {
return spanTree;
const eventSpans = isValidSpanArray(spansData) ? spansData : [];
if (eventSpans.length > 0) {
const spanTree = selectN1QuerySpans(
eventSpans,
evidenceData,
MAX_SPANS_IN_TREE,
);
if (spanTree.length > 0) {
return spanTree;
}
}
const spansById = new Map<string, RawSpan>();
for (const span of spansData) {
for (const span of eventSpans) {
const identifier = getSpanIdentifier(span);
if (identifier) {
spansById.set(identifier, span);
Expand Down
48 changes: 48 additions & 0 deletions packages/mcp-core/src/tools/support/issue-event-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,54 @@ describe("buildIssueEventPayload", () => {
expect(result.metricAlert).toBeNull();
});

describe("performance issues detected from spans", () => {
function genericEvent(occurrenceType: number): Event {
return {
id: "span-n1-event",
type: "generic",
title: "N+1 Query",
message: null,
platform: "python",
dateCreated: "2026-11-23T01:30:00.000Z",
entries: [],
occurrence: {
id: "occ-n1",
projectId: 1,
eventId: "span-n1-event",
fingerprint: ["n1"],
issueTitle: "N+1 Query",
type: occurrenceType,
culprit: "GET /items",
evidenceData: {
transactionName: "GET /items",
parentSpan: "http.server - GET /items",
offenderSpanIds: ["aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb"],
numberRepeatingSpans: "5",
},
},
} as unknown as Event;
}

it("keeps N+1 detector evidence on a generic event", () => {
const { performance } = payload(genericEvent(1006));

expect(performance).toMatchObject({
issueType: "performance_n_plus_one_db_queries",
transactionName: "GET /items",
parentOperation: "http.server - GET /items",
repeatCount: 5,
});
expect(performance?.spans.map((span) => span.spanId)).toEqual([
"aaaaaaaaaaaaaaaa",
"bbbbbbbbbbbbbbbb",
]);
});

it("does not fill performance for other generic occurrences", () => {
expect(payload(genericEvent(8001)).performance).toBeNull();
});
});

describe("metric alert", () => {
it("maps the monitor query from camelCased evidence", () => {
expect(
Expand Down
10 changes: 9 additions & 1 deletion packages/mcp-core/src/tools/support/issue-event-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,22 @@ function mapPerformance(
occurrence: Record<string, unknown> | undefined,
performanceTrace: Trace | undefined,
): IssueEventPayload["performance"] {
if (event.type !== "transaction" || !occurrence) {
if (!occurrence) {
return null;
}
const issueType =
typeof occurrence["type"] === "number"
? (OCCURRENCE_TYPE_TO_ISSUE_TYPE[occurrence["type"]] ?? null)
: (nonEmptyString(occurrence["issueType"]) ??
nonEmptyString(occurrence["type"]));
// Span-detected performance issues are saved as `generic` events.
const isPerformanceEvent =
event.type === "transaction" ||
(event.type === "generic" &&
issueType?.startsWith("performance_") === true);
if (!isPerformanceEvent) {
return null;
}
const evidenceData: N1EvidenceData | undefined = record(
occurrence["evidenceData"],
);
Expand Down
Loading