Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
05d85c4
feat(search): trace-level AND scope across spans (#3004)
eugeneo17 Sep 18, 2026
66a65e1
refactor(search): address review feedback on trace scope
eugeneo17 Sep 18, 2026
b3cfe62
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 18, 2026
847b8fc
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 21, 2026
08c2269
fix(search): address review findings on trace scope
eugeneo17 Sep 22, 2026
77a6f8f
fix(search): make exclusion filters work at trace scope
eugeneo17 Sep 22, 2026
16a92a3
fix(search): correct trace-scope negation and windowed-range membership
eugeneo17 Sep 22, 2026
79dca70
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Sep 22, 2026
916499e
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 23, 2026
74d2a6c
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 24, 2026
9cc2dad
fix(search): keep trace scope out of delta internals and classify neg…
eugeneo17 Sep 29, 2026
eccbf4d
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 29, 2026
6682dac
perf(search): reuse one SQL parser in isNegatedFilterCondition
eugeneo17 Sep 29, 2026
320f9b1
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Sep 30, 2026
e2c5d86
fix(search): use GLOBAL IN for trace-scope membership on distributed …
eugeneo17 Oct 2, 2026
69c5d65
fix(search): treat lucene range/group brackets as grouped in negation…
eugeneo17 Oct 2, 2026
a6cb1fa
fix(search): scope the new query-failed banner to trace mode
eugeneo17 Oct 2, 2026
a80c4a2
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 2, 2026
989fbd9
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 5, 2026
6c9dccc
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 6, 2026
63f1116
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 6, 2026
a9fedab
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 7, 2026
afdabd9
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 8, 2026
86a8027
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 9, 2026
90c2442
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Oct 19, 2026
9d2df50
Merge remote-tracking branch 'upstream/main' into feature/3004-trace-…
eugeneo17 Oct 28, 2026
66e5410
fix(search): persist search scope and restore span-error retry (#3004)
eugeneo17 Nov 5, 2026
a48e323
Merge branch 'main' into feature/3004-trace-scope
eugeneo17 Nov 11, 2026
8aa2c35
fix(search): keep Trace scope through history sync, saved searches an…
eugeneo17 Nov 17, 2026
5f4ddba
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Nov 17, 2026
06711f4
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Nov 23, 2026
0fd3c03
fix(search): address trace-scope review findings
eugeneo17 Nov 24, 2026
d0cf978
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Nov 24, 2026
ea67a50
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Nov 30, 2026
c7ac108
fix(search): click the visible scope label in e2e and drop unsafe tes…
eugeneo17 Nov 30, 2026
46fd39b
fix(api): expose searchScope in external saved search JSON and MCP save
eugeneo17 Dec 1, 2026
3237b50
style(app): use the theme default for the trace scope badge
eugeneo17 Dec 1, 2026
add04c2
Merge remote-tracking branch 'origin/main' into feature/3004-trace-scope
eugeneo17 Dec 2, 2026
b68924a
fix: address review round on trace-scope negation and alert samples
eugeneo17 Dec 2, 2026
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
18 changes: 18 additions & 0 deletions .changeset/trace-scope-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@hyperdx/common-utils': minor
'@hyperdx/app': minor
---

Add a trace-level scope for search so multi-predicate `AND` can match across the
spans of a trace, not only within a single span.

Search gains a **Span | Trace** scope control. **Span** (the default) is
unchanged: every predicate must hold in the same span. **Trace** matches traces
where the predicates are satisfied by different spans of the same trace. Under
the hood each predicate is rewritten into its own
`TraceId IN (SELECT TraceId FROM <source> WHERE <predicate> AND <time>)` and the
parts are `AND`-composed, with the time filter pushed into every subquery.

Trace scope is fail-closed: it is offered only for trace sources that expose a
trace-id expression, and it resets to Span when the source is switched, so
existing searches and saved queries are unaffected.
10 changes: 10 additions & 0 deletions packages/api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -5144,6 +5144,16 @@
}
]
},
"searchScope": {
"type": "string",
"enum": [
"span",
"trace"
],
"readOnly": true,
"description": "Whether the search matches single spans or whole traces. Absent means span.",
"example": "trace"
},
"teamId": {
"type": "string",
"readOnly": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ export function registerSaveSavedSearch({
'against a data source. Use clickstack_list_sources to find the sourceId.\n\n' +
'UPDATES REPLACE FIELDS, they do not merge: name, sourceId, select, where, and ' +
'tags are overwritten, and omitted select/where/tags are reset to empty. ' +
'Omitted whereLanguage, orderBy, and filters keep their stored values. To ' +
'Omitted whereLanguage, orderBy, filters, and searchScope keep their ' +
'stored values. To ' +
'change one field, read the saved search with clickstack_get_saved_search ' +
'and resend everything you want to keep.\n\n' +
'Alerts can be attached to a saved search and evaluate its current query, ' +
Expand Down Expand Up @@ -67,6 +68,7 @@ export function registerSaveSavedSearch({
source: input.sourceId,
tags: input.tags ?? [],
filters: input.filters,
searchScope: input.searchScope,
};

if (isUpdate) {
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/mcp/tools/savedSearches/schemas.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { SearchScopeSchema } from '@hyperdx/common-utils/dist/types';
import { z } from 'zod/v3';

import { mcpUnion } from '@/mcp/utils/errors';
Expand Down Expand Up @@ -87,4 +88,9 @@ export const mcpSaveSavedSearchSchema = z.object({
.array(mcpFilterSchema)
.optional()
.describe('Additional structured filters.'),
searchScope: SearchScopeSchema.optional().describe(
'span matches each span on its own; trace matches spans of traces where ' +
'the predicates hold across spans (trace sources only). Omitted on ' +
'create means span; on update, omitting it keeps the stored value.',
),
});
39 changes: 39 additions & 0 deletions packages/api/src/models/__tests__/savedSearch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import mongoose from 'mongoose';

import { SavedSearch } from '@/models/savedSearch';

// Mongoose strict mode silently drops paths the schema does not declare, so
// these build documents in memory (no DB) to pin that searchScope survives.
describe('SavedSearch model searchScope', () => {
const base = {
team: new mongoose.Types.ObjectId(),
source: new mongoose.Types.ObjectId(),
name: 'trace search',
where: 'ServiceName:frontend',
};

it('keeps a Trace scope', () => {
const doc = new SavedSearch({ ...base, searchScope: 'trace' });
expect(doc.searchScope).toBe('trace');
expect(doc.validateSync()).toBeUndefined();
});

it('leaves the scope unset for legacy saved searches', () => {
const doc = new SavedSearch(base);
expect(doc.searchScope).toBeUndefined();
expect(doc.validateSync()).toBeUndefined();
});

it('rejects an unknown scope', () => {
const doc = new SavedSearch({ ...base, searchScope: 'session' });
expect(doc.validateSync()?.errors.searchScope).toBeDefined();
});

// The external API and the MCP tools serialize through toExternalJSON, so
// a Trace search read there must not come back looking like a Span one.
it('includes the scope in the external JSON', () => {
const doc = new SavedSearch({ ...base, searchScope: 'trace' });
expect(doc.toExternalJSON().searchScope).toBe('trace');
expect(new SavedSearch(base).toExternalJSON().searchScope).toBeUndefined();
});
});
15 changes: 14 additions & 1 deletion packages/api/src/models/savedSearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { SavedSearchSchema } from '@hyperdx/common-utils/dist/types';
import {
SavedSearchSchema,
type SearchScope,
SearchScopeSchema,
} from '@hyperdx/common-utils/dist/types';
import mongoose, { Schema } from 'mongoose';
import { z } from 'zod/v3';

Expand All @@ -14,6 +18,7 @@ type ExternalSavedSearch = {
sourceId?: string;
tags?: string[];
filters?: unknown[];
searchScope?: SearchScope;
teamId: string;
createdAt?: string;
updatedAt?: string;
Expand Down Expand Up @@ -63,6 +68,13 @@ const savedSearchSchema = new Schema<
},
tags: [String],
filters: [{ type: mongoose.Schema.Types.Mixed }],
// Strict mode drops undeclared paths, so a saved Trace search would
// silently reload as Span without this.
searchScope: {
type: String,
enum: SearchScopeSchema.options,
required: false,
},

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

External saved search drops scope

Low Severity

toExternalJSON and the MCP save payload never include searchScope, even though the Mongoose model and SavedSearchSchema now persist it. MCP get/save and any other toExternalJSON consumer omit the field, so a Trace saved search round-trips as Span through those APIs.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit c7ac108. Configure here.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 46fd39b. toExternalJSON now returns searchScope (documented on the SavedSearch response schema), so the external API and MCP get/save report a Trace search as Trace. The MCP save tool accepts an optional searchScope; omitted on update it keeps the stored value, like whereLanguage and filters. Covered by the new case in models/__tests__/savedSearch.test.ts.

defaultTimeRange: { type: mongoose.Schema.Types.Mixed, required: false },
// Immutable: ownership attribution is set once at creation.
createdBy: {
Expand Down Expand Up @@ -98,6 +110,7 @@ savedSearchSchema.methods.toExternalJSON = function (): ExternalSavedSearch {
sourceId: this.source ? String(this.source) : undefined,
tags: this.tags,
filters: this.filters,
searchScope: this.searchScope,
teamId: this.team ? String(this.team) : '',
createdAt:
this.createdAt instanceof Date ? this.createdAt.toISOString() : undefined,
Expand Down
6 changes: 6 additions & 0 deletions packages/api/src/routers/external-api/v2/savedSearches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ async function requireValidSourceId(
* example:
* - type: sql
* condition: "ServiceName IN ('checkout', 'payments')"
* searchScope:
* type: string
* enum: [span, trace]
* readOnly: true
* description: Whether the search matches single spans or whole traces. Absent means span.
* example: trace
* teamId:
* type: string
* readOnly: true
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/tasks/checkAlerts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,7 @@ const getChartConfigFromAlert = (
where: savedSearch.where,
whereLanguage: savedSearch.whereLanguage,
filters: savedSearch.filters?.map(f => ({ ...f })),
searchScope: savedSearch.searchScope,
groupBy: alert.groupBy,
select: ALERT_COUNT_DEFAULT_SELECT,
displayType: DisplayType.Line,
Expand Down
11 changes: 10 additions & 1 deletion packages/api/src/tasks/checkAlerts/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,13 @@ export const fetchSampleLines = async ({
metadata: Metadata;
savedSearch: Pick<
ISavedSearch,
'id' | 'select' | 'where' | 'whereLanguage' | 'orderBy' | 'filters'
| 'id'
| 'select'
| 'where'
| 'whereLanguage'
| 'orderBy'
| 'filters'
| 'searchScope'
>;
source: ISource;
startTime: Date;
Expand All @@ -554,6 +560,9 @@ export const fetchSampleLines = async ({
where: savedSearch.where,
whereLanguage: savedSearch.whereLanguage,
filters: savedSearch.filters,
// The alert fires on the trace-scoped count, so its sample rows have to
// come from the same scope or a cross-span search samples nothing.
searchScope: savedSearch.searchScope,
orderBy: savedSearch.orderBy,
dateRangeStartInclusive: true,
dateRangeEndInclusive: false,
Expand Down
Loading
Loading