From 19185bc23efd0dbd2bfd2f365d5781e86a8cd1cb Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:25:27 +0000 Subject: [PATCH 1/2] fix(search-events): Keep issue filters the agent rewrites A structured errors query still goes through the embedded agent, and its query runs as-is unless it downgrades a filter. The agent has dropped the last character of an issue short ID (issue:PROJ-2B3 -> issue:PROJ-2B), so Sentry rejected the search with "Issue IDs do not exist". validateSearch can't catch this because the validate endpoint skips the errors dataset. Issue IDs are exact identifiers, so when the agent changes or drops an explicit issue: or issue.id: filter, run the caller's structured query. Fixes #2443 --- .../src/tools/catalog/search-events.test.ts | 48 +++++++++++++++++++ .../src/tools/support/search-events/search.ts | 23 ++++++++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/packages/mcp-core/src/tools/catalog/search-events.test.ts b/packages/mcp-core/src/tools/catalog/search-events.test.ts index bf66b3085..944ab711c 100644 --- a/packages/mcp-core/src/tools/catalog/search-events.test.ts +++ b/packages/mcp-core/src/tools/catalog/search-events.test.ts @@ -272,6 +272,54 @@ describe("search_events", () => { expect(result).toContain("No results found"); }); + it("keeps an issue short ID the agent rewrites", async () => { + mockGenerateText.mockResolvedValueOnce( + mockAIResponse( + "errors", + "issue:PROJ-2B", + ["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("issue:PROJ-2B3"); + return HttpResponse.json({ data: [] }); + }, + ), + ); + + const result = await searchEvents.handler( + { + organizationSlug: "test-org", + regionUrl: null, + projectSlug: null, + dataset: "errors", + query: "issue:PROJ-2B3", + 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, diff --git a/packages/mcp-core/src/tools/support/search-events/search.ts b/packages/mcp-core/src/tools/support/search-events/search.ts index f1c53f0b1..2da23e8ae 100644 --- a/packages/mcp-core/src/tools/support/search-events/search.ts +++ b/packages/mcp-core/src/tools/support/search-events/search.ts @@ -403,6 +403,24 @@ function preservesSearchTokens(originalQuery: string, repairedQuery: string) { ); } +const ISSUE_FILTER_PATTERN = /^!?issue(?:\.id)?:\S/; + +/** + * 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) { + return tokenizeSearchQuery(originalQuery).some( + (token) => + ISSUE_FILTER_PATTERN.test(token) && + !containsSearchToken(repairedQuery, token), + ); +} + function choosePreservingRepairedQuery(params: { originalQuery: string; repairedQuery?: string | null; @@ -977,11 +995,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 = From 3985be2a21458bedcc68239a246df496ca731e8a Mon Sep 17 00:00:00 2001 From: "sentry-junior[bot]" <264270552+sentry-junior[bot]@users.noreply.github.com> Date: Fri, 20 Nov 2026 00:32:21 +0000 Subject: [PATCH 2/2] fix(search-events): Check issue filters inside groups Match issue: and issue.id: filters after an opening parenthesis too, so an agent rewrite inside (issue:A OR issue:B) also falls back to the caller's query. --- .../src/tools/catalog/search-events.test.ts | 92 ++++++++++--------- .../src/tools/support/search-events/search.ts | 14 ++- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/packages/mcp-core/src/tools/catalog/search-events.test.ts b/packages/mcp-core/src/tools/catalog/search-events.test.ts index 944ab711c..10225f5e9 100644 --- a/packages/mcp-core/src/tools/catalog/search-events.test.ts +++ b/packages/mcp-core/src/tools/catalog/search-events.test.ts @@ -272,53 +272,57 @@ describe("search_events", () => { expect(result).toContain("No results found"); }); - it("keeps an issue short ID the agent rewrites", async () => { - mockGenerateText.mockResolvedValueOnce( - mockAIResponse( - "errors", - "issue:PROJ-2B", - ["count()"], - undefined, - "-count()", - ), - ); + 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("issue:PROJ-2B3"); - return HttpResponse.json({ data: [] }); - }, - ), - ); + 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: "issue:PROJ-2B3", - 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", - }, - ); + 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"); - }); + expect(result).toContain("No results found"); + }, + ); it("renders a timeseries when the agent returns timeSeries (per-hour)", async () => { const output = { diff --git a/packages/mcp-core/src/tools/support/search-events/search.ts b/packages/mcp-core/src/tools/support/search-events/search.ts index 2da23e8ae..2d70792fc 100644 --- a/packages/mcp-core/src/tools/support/search-events/search.ts +++ b/packages/mcp-core/src/tools/support/search-events/search.ts @@ -403,7 +403,10 @@ function preservesSearchTokens(originalQuery: string, repairedQuery: string) { ); } -const ISSUE_FILTER_PATTERN = /^!?issue(?:\.id)?:\S/; +// `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. @@ -414,10 +417,11 @@ const ISSUE_FILTER_PATTERN = /^!?issue(?:\.id)?:\S/; * errors dataset skips validation. */ function changesIssueFilter(originalQuery: string, repairedQuery: string) { - return tokenizeSearchQuery(originalQuery).some( - (token) => - ISSUE_FILTER_PATTERN.test(token) && - !containsSearchToken(repairedQuery, token), + const repairedFilters = new Set( + repairedQuery.match(ISSUE_FILTER_PATTERN) ?? [], + ); + return (originalQuery.match(ISSUE_FILTER_PATTERN) ?? []).some( + (filter) => !repairedFilters.has(filter), ); }