Skip to content

fix(search-events): Keep explicit timestamp bounds the agent drops - #2139

Draft
sentry-junior[bot] wants to merge 2 commits into
mainfrom
fix/search-errors-keep-timestamp-bounds
Draft

sentry-junior[bot] wants to merge 2 commits into
mainfrom
fix/search-errors-keep-timestamp-bounds

Conversation

@sentry-junior

@sentry-junior sentry-junior Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

search_errors and the other runSearchEvents tools can silently run a fixed-window search over the whole period. This happens when the caller sends a structured query with timestamp:>= / timestamp:< bounds and a period.

What happens

  1. The embedded agent moves the bounds out of the query and into an absolute timeRange ({start, end}).
  2. For structured queries with an explicit period, the handler keeps period and discards the agent's timeRange (search.ts:936-938 on main).
  3. The query the handler sends no longer has the bounds. The search covers the full relative period, and nothing tells the caller.

The only guard on the agent's rewrite is isSemanticFilterDowngrade, and it only catches filters rewritten into full-text matching. It does not catch filters that were dropped.

User feedback reported this more than once: automated callers asked for fixed windows and got rolling-period totals back. In production traces, the agent returned an absolute timeRange for the requested window, and the final /events/ request still used the caller's statsPeriod.

Change

After the agent repairs a structured query, any timestamp: bounds from the caller's query that are missing from the repaired query are appended to it again. The bounds can only narrow the period, so sending them is always safe. Queries with OR/AND or parentheses are left unchanged, because a bound appended outside its group would change what it means.

The regression test sets up this case: period: "7d", the agent moves the bounds into timeRange and drops them from the query, and the request must still carry them. Without the fix, the request query has no bounds.

Out of scope: exposing start/end as tool inputs, which is #1215 / #1228. That work does not cover this path, where a period is passed alongside bounds in the query.

via @sergical.

--

View Session [Sentry]

When a structured query has timestamp:>= / timestamp:< bounds and the
caller also passes period, the embedded agent often moves the bounds
into an absolute timeRange and drops them from the query. The explicit
period then wins over that range, so the search silently runs over the
whole period instead of the requested window.

Put any dropped timestamp bounds back on the repaired query. They only
narrow the period, so they are safe to send. Grouped and boolean queries
are left alone.

Co-Authored-By: Sergiy Dybskiy <3760543+sergical@users.noreply.github.com>
@vercel

vercel Bot commented Nov 10, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
sentry-local Skipped Skipped Nov 10, 2026 12:27pm UTC

Request Review

The grouping guard skipped restoring dropped timestamp bounds whenever
a token contained a parenthesis, including parentheses inside a quoted
value such as message:"err (timeout)". Strip quoted values before the
check so only real grouping skips the restore.
@sentry-junior

sentry-junior Bot commented Nov 12, 2026

Copy link
Copy Markdown
Contributor Author

We got another user report of this exact path on search_errors (server 0.42.0). The caller passed timestamp:>= / timestamp:< bounds in a structured query plus period: "7d". The embedded agent returned an absolute timeRange and its explanation said it had used that range. The final /events/ request still sent statsPeriod=7d, and executedSearch.timeRange had null start/end.

On main @ 0a3801e the cause is unchanged: period wins over the agent's range (search.ts:933-936), and the downgrade guard only catches full-text rewrites, not dropped filters (utils.ts:554-595). When the agent also drops the timestamp: tokens, the search widens to the whole period. That's the case this PR fixes. The branch is still clean against main.

The report also says the explanation didn't match what ran. That part is tracked in #2147.

@sentry-junior

sentry-junior Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Another report of this path, from the same automated caller as the 11-12 report. This caller has now sent five reports since 11-08 (MCP-SERVER-GMH, GMK, GMZ, GP9, GPJ), on search_errors, server 0.42.0.

  • The feedback says a call with environment:<env> timestamp:>=… timestamp:<…, fields: ["count()"], and period: "7d" ran as only environment:<env> over statsPeriod=7d. I couldn't find the span for that exact call (sampling), so this part comes from the feedback text only.
  • A sampled retry in the same session shows the time-range half. The agent returned timeRange: {start, end} and an explanation saying it "replaced the conflicting rolling 7-day period with the specified absolute time range". The final /events/ request still sent statsPeriod=7d. The bounds survived only because the agent kept the timestamp: tokens that time.

I checked this PR against current main @ c8447f8. The diff still applies cleanly. The new regression test fails on main (Received: "environment:production is:unresolved", bounds dropped) and passes with the fix. All 100 tests in search-events.test.ts pass. This caller runs every day, so it's still worth getting this out of draft. Explanation/executedSearch drift stays in #2147, and typed start/end stays in #1215.

@sentry-junior

sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Another production report of this exact case: user feedback MCP-SERVER-GPW (2026-11-13, server 0.42.0, search_errors).

Trace evidence (same user, 2026-11-13 17:01–17:05Z, several search_errors calls before the feedback was filed):

  • Input: query: "environment:prod timestamp:>=2026-10-30T00:00:00Z timestamp:<2026-11-13T00:00:00Z issue:[…]", period: "30d".
  • The agent's events/validate call used start=2026-10-30T00:00:00Z&end=2026-11-13T00:00:00Z.
  • The final /events/ request used statsPeriod=30d, and its query had no timestamp: tokens.
  • executedSearch.timeRange was {statsPeriod: "30d", start: null, end: null}. The explanation still said "Converted the conflicting relative 30-day period to the supplied absolute timestamp range".
  • When the caller left out period, or the agent kept the tokens in the query, the bounds were applied correctly. The caller worked around the bug that way.

Checked against this branch: I added a scratch case with the reporter's query shapes to the regression test from this PR: bounds in the middle of the query, an issue:[A,B] list, and trailing prose (… count per issue per day), with period: "30d" and lockDataset: true. Both shapes pass. The request goes out with statsPeriod=30d and query=environment:prod issue:[…] timestamp:>=… timestamp:<…. So this PR covers the case.

Pattern count: I count 8 reports of timestamp: tokens being dropped in the last 30 days: GK5, GMH, GMK, GMZ, GP9, GPJ, GPV, and GPW. They came from 2 users on automated clients. GN4 is the prose-bounds variant, which #2146 covers.

One gap remains after this PR. The explanation can still say the range was converted to absolute bounds while timeRange shows statsPeriod, so callers will keep second-guessing executedSearch. That is optional follow-up and does not block this PR.

@sentry-junior

sentry-junior Bot commented Nov 13, 2026

Copy link
Copy Markdown
Contributor Author

Another report of this exact case: Sentry MCP feedback MCP-SERVER-GPV, search_errors, from a Linear agent on server 0.42.0. This time the call was sampled, so the drop shows up in the trace.

  • Input: period: "30d" and a query containing timestamp:>=<day> timestamp:<<day>, the structured filters plus some prose.
  • Agent: its events/validate call used start/end for the requested window, with the timestamp: tokens taken out of the query.
  • Executed: the final events-timeseries request sent statsPeriod=30d and a query with no timestamp: tokens. executedSearch.timeRange was {statsPeriod: "30d", start: null, end: null}, and the buckets began two weeks before the requested lower bound.

In the same session, 2 of the 6 search_errors calls that passed period plus timestamp: tokens lost their bounds this way. In the other 4, the agent kept the tokens, so the window was right even though statsPeriod replaced the agent's range. Calls without period used the agent's start/end correctly. That matches the search.ts:940-943 path, which is unchanged on current main (cc90ca1).

The feedback also points out that the explanation claims absolute bounds were applied. That part is #2147.

That makes at least 10 reports of this pattern in mcp-server feedback over the last 14 days.

joshuarli pushed a commit that referenced this pull request Nov 14, 2026
`search_issue_events` fails on open-ended "since X" windows such as
`timestamp:>=2026-11-12T06:39:00Z` or "since November 12 2026 at 06:39
UTC". It returns "The AI was unable to process your query";
`search_errors` handles the same window fine.

**Why it fails**

The embedded agent's output schema
(`tools/support/search-issue-events/agent.ts`) is stricter than its
prompt:

1. An absolute `timeRange` requires both `start` and `end`. A "since X"
request has no natural end, and the agent doesn't know the current time.
2. `timeRange` is required, but the prompt says to omit it for the
default window.

If the model returns `{start}` only, or omits `timeRange`, the AI SDK
raises `NoObjectGeneratedError` ("response did not match schema").
`rescueFromText` can't repair that, so the tool returns the "unable to
process" error (`internal/agents/callEmbeddedAgent.ts:226`). Production
logs show this exact error with `finishReason: stop` for these queries.
Which of the two shapes the model produced is inferred, since the traces
weren't sampled.

**Change**

- In an absolute `timeRange`, `end` is optional. When it's missing, the
handler runs the search until now.
- `timeRange` defaults to `null`, meaning the default 14d window.
- The prompt has open-ended examples and says "null" instead of "omit".

Existing outputs, whether full `{start, end}` or `{statsPeriod}`, behave
as before.

Related but separate: #1215 / #1228 (caller-supplied `start`/`end` for
`search_events` and `search_issues`) and #2139 / #2146 (dropped absolute
bounds in `runSearchEvents`). Longer term, #1896 removes the embedded
search agents.

<!-- junior-request-attribution:start -->
via @joshuarli.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CG2CP9ELW%3A1794596896.742009 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CG2CP9ELW%3A1794596896.742009)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CG2CP9ELW%3A1794596896.742009/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
joshuarli pushed a commit that referenced this pull request Nov 15, 2026
… also passed (#2146)

`search_errors` (and the other `runSearchEvents` tools) can silently
search the whole `period` when the query names a fixed window. This
happens when the caller names a window in the query text, uses some
structured filters, and also sends `period` as an outer guard.

**What happens**

1. The query has structured filters, so `hasStructuredQuery` is true.
Dataset tools set `lockDataset`, so the handler trusts the caller's
explicit params.
2. The embedded agent sees `statsPeriod` in its prompt. It still returns
an absolute `timeRange` (`{start, end}`) for the window that the query
names.
3. Because `period` is explicit, the handler discards that range and
sends only `statsPeriod` (`search.ts:936-938` on main).
`executedSearch.timeRange` shows `statsPeriod` with null `start`/`end`.
The explanation still describes the agent's plan.

A production trace from user feedback (MCP-SERVER-GN4) shows exactly
this. The query was `Between <start> and <end>, environment:production
<tag>:<value>, …` with `period: "3d"`. The agent's first
`events/validate` call used `start`/`end`. The final `/events/` request
and the Snuba query used the 3-day rolling window. The same session
without structured filters took the untrusted path, and the absolute
range was applied correctly.

**Change**

When the caller passes `period` and the agent returns an absolute range,
the search uses the overlap of the two. The overlap can only narrow
`period`, so the result never goes outside what the caller allowed. When
the two don't overlap, or the agent returned a relative period, the
caller's `period` is kept as before.

**Relation to other work**

- #2139 handles the case where the bounds are `timestamp:` tokens in the
query. It puts them back on the query after the agent drops them. This
PR covers bounds written in prose, where there are no tokens to put
back. The two don't overlap in code, and both can land.
- #1215 / #1228 (typed `start`/`end` inputs) is still the long-term fix
for callers that know the exact window.
- The Seer path has the same `explicit period wins` rule
(`search.ts:851-853`). I left it unchanged because I have no trace
evidence for it.

<!-- junior-request-attribution:start -->
via **Sentry**.
<!-- junior-request-attribution:end -->

<!-- junior-session-footer:start -->
<!-- junior-conversation-id:slack%3AC0CG2CP9ELW%3A1794328376.548829 -->

--

[View
Session](https://junior-prod.sentry.dev/conversations/slack%3AC0CG2CP9ELW%3A1794328376.548829)
[[Sentry]](https://sentry.sentry.io/explore/conversations/slack%3AC0CG2CP9ELW%3A1794328376.548829/?project=4510944073809921)

<!-- junior-session-footer:end -->

Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>

This branch was successfully deployed

1 active and 1 inactive deployments
Actions — 68cd2ca8 Deployed Nov 10, 2026 by sentry-junior[bot] via eval #1771
Preview — 68cd2ca8 Deployed Nov 10, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant