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
52 changes: 52 additions & 0 deletions packages/mcp-core/src/tools/catalog/search-events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,58 @@ describe("search_events", () => {
expect(result).toContain("No results found");
});

it.each([
["a bare", "issue:PROJ-2B3", "issue:PROJ-2B"],
[
"a grouped",
"(issue:PROJ-2B3 OR issue:OTHER-1)",
"(issue:PROJ-2B OR issue:OTHER-1)",
],
])(
"keeps %s issue short ID the agent rewrites",
async (_, query, rewritten) => {
mockGenerateText.mockResolvedValueOnce(
mockAIResponse("errors", rewritten, ["count()"], undefined, "-count()"),
);

mswServer.use(
http.get(
"https://sentry.io/api/0/organizations/test-org/events/",
({ request }) => {
const url = new URL(request.url);
expect(url.searchParams.get("query")).toBe(query);
return HttpResponse.json({ data: [] });
},
),
);

const result = await searchEvents.handler(
{
organizationSlug: "test-org",
regionUrl: null,
projectSlug: null,
dataset: "errors",
query,
fields: ["count()"],
sort: null,
period: "24h",
limit: 10,
includeExplanation: false,
},
{
accessToken: "test-token",
userId: "user-123",
clientId: "client-123",
grantedSkills: new Set(),
constraints: {},
sentryHost: "sentry.io",
},
);

expect(result).toContain("No results found");
},
);

it("renders a timeseries when the agent returns timeSeries (per-hour)", async () => {
const output = {
dataset: "errors" as const,
Expand Down
27 changes: 25 additions & 2 deletions packages/mcp-core/src/tools/support/search-events/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,28 @@ function preservesSearchTokens(originalQuery: string, repairedQuery: string) {
);
}

// `issue:` or `issue.id:` filters at a token start or after `(`, with a quoted,
// bracketed list, or bare value, e.g. `(!issue:"PROJ-1" OR issue:[A, B])`.
const ISSUE_FILTER_PATTERN =
/(?<=^|[\s(])!?issue(?:\.id)?:(?:"[^"]*"|\[[^\]]*\]|[^\s()]+)/g;

/**
* Whether the agent changed or dropped an explicit `issue:` filter.
*
* Issue IDs are exact identifiers, but the agent has rewritten them, for
* example `issue:PROJ-2B3` to `issue:PROJ-2B`. Sentry then rejects the search
* or matches a different issue, and validateSearch can't catch it because the
* errors dataset skips validation.
*/
function changesIssueFilter(originalQuery: string, repairedQuery: string) {
const repairedFilters = new Set(
repairedQuery.match(ISSUE_FILTER_PATTERN) ?? [],
);
return (originalQuery.match(ISSUE_FILTER_PATTERN) ?? []).some(
(filter) => !repairedFilters.has(filter),
);
}

function choosePreservingRepairedQuery(params: {
Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved
originalQuery: string;
repairedQuery?: string | null;
Expand Down Expand Up @@ -977,11 +999,12 @@ async function executeSearchEvents(
dataset,
})
: looksLikeSentrySearchSyntax(params.query, dataset) &&
isSemanticFilterDowngrade(
(isSemanticFilterDowngrade(
params.query ?? "",
parsed.query || "",
dataset,
)
) ||
changesIssueFilter(params.query ?? "", parsed.query || ""))
? (params.query ?? "")
: parsed.query || "";
sortParam =
Expand Down
Loading