Skip to content

fix: auto-repair malformed search queries and fix telemetry object serialization (CLI-FA)#872

Merged
BYK merged 6 commits intomainfrom
fix/cli-fa-query-sanitize-and-telemetry
Apr 29, 2026
Merged

fix: auto-repair malformed search queries and fix telemetry object serialization (CLI-FA)#872
BYK merged 6 commits intomainfrom
fix/cli-fa-query-sanitize-and-telemetry

Conversation

@BYK
Copy link
Copy Markdown
Member

@BYK BYK commented Apr 28, 2026

Summary

Investigation of actual CLI-FA events revealed the 400 Bad Request errors are caused by:

  1. Malformed search syntax from AI agents — e.g., error.http.status_code:[401,403,429,500,) (trailing comma + wrong closing delimiter)
  2. [object Object] in telemetry tagsTimeRange objects were serialized via String() instead of JSON

Changes

Search query auto-repair (src/lib/search-query.ts)

  • When the PEG parser fails, tryRepairQuery() attempts common fixes before passing through to the API
  • Currently repairs trailing commas (key:[a,b,]key:[a,b]) and wrong closing delimiters (key:[a,b,)key:[a,b])
  • Logs a warning when a repair is applied so the user knows what happened
  • Falls through to the API's own 400 error with detail if repair doesn't help

Telemetry fix (src/lib/telemetry.ts)

  • flagValueToTag() now JSON-serializes non-array objects to avoid [object Object]
  • Arrays still serialize as comma-separated strings (backwards compatible)
  • flag.period now shows {"type":"relative","period":"90d"} instead of [object Object]

Tests

  • 10 new tests for tryRepairQuery unit behavior
  • 2 integration tests verifying sanitizeQuery repairs and returns the fixed query
  • Existing telemetry tests pass with the new serialization

Fixes https://sentry.sentry.io/issues/7341195391/

…rialization (CLI-FA)

Two fixes for the CLI-FA issue group (95 users, 1589 events):

1. Search query auto-repair: When the PEG parser fails on a malformed query,
   tryRepairQuery attempts common fixes before passing through to the API:
   - Trailing commas in in-list filters: key:[a,b,] → key:[a,b]
   - Wrong closing delimiter: key:[a,b,) → key:[a,b]
   This prevents cryptic 400 Bad Request errors from the API when the user's
   intent is clear (common with AI agents constructing queries).

2. Telemetry tag serialization: setFlagContext now JSON-serializes object
   values (like TimeRange) instead of producing '[object Object]' via
   String(). Arrays still serialize as comma-separated for backwards compat.
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-872/

Built to branch gh-pages at 2026-04-29 00:37 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 28, 2026

Codecov Results 📊

6244 passed | Total: 6244 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +12
Passed Tests 📈 +12
Failed Tests
Skipped Tests

All tests are passing successfully.

❌ Patch coverage is 71.43%. Project has 13048 uncovered lines.
❌ Project coverage is 75.9%. Comparing base (base) to head (head).

Files with missing lines (1)
File Patch % Lines
src/lib/search-query.ts 62.16% ⚠️ 14 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    75.99%    75.90%    -0.09%
==========================================
  Files          293       293         —
  Lines        54000     54138      +138
  Branches         0         0         —
==========================================
+ Hits         41036     41090       +54
- Misses       12964     13048       +84
- Partials         0         0         —

Generated by Codecov Action

Comment thread test/lib/search-query.test.ts Outdated
Comment thread test/lib/search-query.test.ts
The PEG parser accepts trailing commas before ] (valid syntax), so only
the wrong-delimiter case (,) triggers auto-repair. Updated tests to
reflect actual behavior.

Addresses Seer and Cursor Bugbot review comments.
Comment thread src/lib/telemetry.ts
Ensures false would serialize as 'false' not 'true' if the caller
ever passes it through. Addresses Cursor Bugbot low-severity finding.
Comment thread src/lib/search-query.ts Outdated
Exclude '[' from the capture group so the in-list repair regex doesn't
greedily match past a second filter's opening bracket.

Addresses Cursor Bugbot medium-severity finding.
Copy link
Copy Markdown
Contributor

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit d728f30. Configure here.

Comment thread src/lib/search-query.ts Outdated
BYK added 2 commits April 29, 2026 00:28
Move log.warn after parse(repaired) confirms the repair produced valid
syntax. Prevents misleading warnings when the repair didn't actually help.

Addresses Cursor Bugbot low-severity finding.
@BYK BYK merged commit 590fbce into main Apr 29, 2026
26 checks passed
@BYK BYK deleted the fix/cli-fa-query-sanitize-and-telemetry branch April 29, 2026 00:46
BYK added a commit that referenced this pull request Apr 29, 2026
… query repair (CLI-FA) (#880)

## Summary
Follow-up to #872 — replaces the specific `tryRepairQuery` (single
regex, catch-block-only) with a generic `normalizeQuery` pipeline that
runs **before** PEG parsing on every query.

## What changed

### Before (PR #872)
- `tryRepairQuery` only ran when PEG parsing failed (catch block)
- Single regex for `[a,b,)` → `[a,b]`
- Couldn't fix patterns that PEG parses but the API rejects (e.g.,
`[a,b,]`)

### After (this PR)
- `normalizeQuery` runs on **every query** before PEG parsing — cheap
string ops
- Pipeline of focused transform functions:
1. `fixMismatchedBrackets`: `[a,b,)` → `[a,b]` (wrong closing delimiter)
2. `stripTrailingListCommas`: `[a,b,]` → `[a,b]` (balanced bracket
trailing comma)
  3. Whitespace collapse: double spaces → single, trim edges
- Architecture makes it easy to add more passes without growing
complexity

### Why pre-parse?
Only 1 of the observed CLI-FA patterns actually fails PEG parsing — the
rest parse fine but the Sentry API rejects them semantically. Running
normalization before parsing catches both syntactic and
PEG-valid-but-API-invalid patterns.

## Tests
- 62 tests: unit tests for each normalizer (brackets, commas,
whitespace, passthrough, cross-boundary safety)
- Integration tests verifying the full `sanitizeQuery` →
`normalizeQuery` → PEG parse flow
- Property tests still pass
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