Repository navigation
Commit 5584f84
authored
fix(mcp): make event_patterns drill-down snippets executable (#3426)
## Summary
`clickstack_event_patterns` returned a `whereSnippet` per pattern that agents were told to pass to `clickstack_search`, but it often matched nothing. A template like `Sending <*> rows` became `Body:"Sending rows"` (a phrase that never occurs), and map-field body expressions produced Lucene that failed to parse. Agents hit dead ends on the main drill-down path.
Changes:
- **Executable drill-down.** `whereSnippet` is now a SQL `match()` predicate (`whereLanguage: "sql"`) built from the template: literal tokens in order, one non-whitespace token per `<*>`, whitespace matching Drain's JS `\s` tokenization. It already ANDs in the caller's `where` (rendered to SQL, dashboard variables expanded) and the source's table filter. If the caller filter can't be rendered as SQL, `summary.drilldown.includesFilters` is `false` with a warning. Templates over 4000 chars get `drilldownUnavailable` instead of an empty snippet that would match everything.
- **Source table filter.** Pattern mining (`event_patterns`, `emerging_signals`, dashboard pattern tiles) now applies the log source's `tableFilterExpression`. Counts can drop for sources that set one.
- **`bodyExpression`** also accepts `JSONExtractString(<column or map ref>, '<key>', ...)`. It is rebuilt from validated args, so the injection allowlist still holds.
- **Sampling transparency.** Response reports `sampleCoverage`, and `summary.trend` gives the real bucket interval and boundaries (granularity rounding means fewer or wider buckets than requested). Tool description and usage text explain that counts are extrapolated and a 0 bucket doesn't mean no events.
- **`minePatterns` template bug.** Each cluster now reports its own template. Before, it re-matched the first member's body and could pick up a different same-length cluster's template that other members didn't fit, so the drill-down missed some of the pattern's own events.
### How to reproduce
**Starting point:** `yarn dev`, MCP client connected to the local API, a log source with data.
**On `main`:**
1. Call `clickstack_event_patterns` on a log source.
2. Pass a pattern containing `<*>` as `where` to `clickstack_search`. It returns no rows, or a parse error when `bodyExpression` is a map field.
**On this branch:**
1. Same call. Each pattern has a SQL `whereSnippet` and `whereLanguage: "sql"`.
2. Pass it to `clickstack_search` with `whereLanguage: "sql"`. It returns that pattern's events.
Covered by new unit tests (`patternDrilldown.test.ts`, `mine-patterns.test.ts`, `query.test.ts`) and integration tests in `queryTool.int.test.ts` / `queryTile.int.test.ts` that run each snippet through search.
### References
HDX-57481 parent a13c80e commit 5584f84
18 files changed
Lines changed: 1401 additions & 151 deletions
File tree
- .changeset
- packages
- api/src
- mcp
- __tests__
- dashboards
- prompts/dashboards
- tools/query
- __tests__
- common-utils/src
- __tests__
- core
- drain
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
434 | 434 | | |
435 | 435 | | |
436 | 436 | | |
| 437 | + | |
437 | 438 | | |
438 | 439 | | |
439 | 440 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
891 | 891 | | |
892 | 892 | | |
893 | 893 | | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
894 | 918 | | |
895 | 919 | | |
896 | 920 | | |
| |||
931 | 955 | | |
932 | 956 | | |
933 | 957 | | |
| 958 | + | |
934 | 959 | | |
935 | 960 | | |
936 | 961 | | |
| |||
953 | 978 | | |
954 | 979 | | |
955 | 980 | | |
| 981 | + | |
956 | 982 | | |
957 | 983 | | |
958 | 984 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
109 | 110 | | |
110 | 111 | | |
111 | 112 | | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
112 | 167 | | |
113 | 168 | | |
114 | 169 | | |
| |||
0 commit comments