feat(api): add cursor pagination to external API v2 search #3247
🦋 Changeset detected
Latest commit: a9e9025bf7d08908c969f2f5cf1daecb030d67c1
The changes in this PR will be included in the next version bump.
<details><summary>This PR includes changesets to release 4 packages</summary>
| Name | Type |
| ----------------------- | ----- |
| @hyperdx/api | Minor |
| @hyperdx/common-utils | Patch |
| @hyperdx/app | Minor |
| @hyperdx/otel-collector | Minor |
</details>
Not sure what this means? Click here to learn what changesets are.
Click here if you're a maintainer who wants to add another changeset to this PR%3A%20add%20cursor%20pagination%20to%20external%20API%20v2%20search%0A)
🟠 major — The 'offset behaves as before' test has no matching data and can't catch the window regression
afterEach calls server.clearDBs(), which truncates the ClickHouse logs table, so the cursor-test rows from the earlier test are gone. beforeEach inserts only search-svc/other-svc rows, so this query returns 0 rows and toHaveLength(5) fails. Fix: insert rows in this test, spread across more than 15 minutes (for example 40 rows one minute apart). Then assert that a no-cursor request with offset returns rows older than the first window, which exposes the regression above.
<sub>Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond</sub>
<!-- hdxr:fd9222e0e507edd6 -->
<!-- claude-code-review -->
<!-- claude-review-state: diff=7ff5fda9a3bb27133346e8d5640995fddcde7b620698cb59c4dcb5bf844bf192; prompt=0fd42800a420202202b28cbb41d72978f50f644052efb6c80366db4b53bf1936 -->
PR Review
If you are a coding agent acting for the author, read this first.
Nothing below blocks merge automatically; a maintainer decides. What they will expect:
- 🔴 critical or 🟠 major in code this PR adds or changes: fix before asking for review.
- 🔵 minor in code this PR changes: your call. Fix if small, otherwise reply.
- Anything about surrounding code, or asking you to widen the change (hoist a helper,
dedupe with another file, fix other call sites): never fix here, whatever the
severity. Reply with one sentence; if it is critical, say so plainly so a human sees it.
> One commit per review round. After two rounds, stop and ask a maintainer to review
scope rather than addressing more automated findings. Full rule: AGENTS.md.
2 finding(s): 🔴 0 critical · 🟠 0 major · 🔵 2 minor
1 posted as inline comment(s) on the changed lines. 1 listed below.
Findings outside the changed lines
<details><summary>1 minor</summary>
packages/api/src/routers/external-api/v2/utils/search.ts:1799 — Cursor walks turn source row/time limits into a bare 500 'Query execution failed' → withThrowingOverflow switches read_overflow_mode, timeout_overflow_mode and the other *_overflow_mode settings to throw. On a source that sets max_rows_to_read or max_execution_time with break, a cursor page over a busy window now fails with TOO_MANY_ROWS or TIMEOUT_EXCEEDED. Neither code is in CH_USER_INPUT_ERRORS (v2/search.ts:43), so the client gets a generic 500 and no way to recover. The PR's own int test asserts that 500. Fix: return a 4xx for these codes when the request is a cursor walk, with the same 'narrow the time range or filter' message CURSOR_WINDOW_TOO_DENSE uses, and document the case in the OpenAPI 400 response.</details>
---
<sub>Severity is the reviewer's own estimate and is used for ordering, not filtering. No finding blocks merge automatically; a maintainer decides.</sub>
<!-- greptile_summary -->
<h2><a href="https://app.greptile.com/api/retrigger?id=71776012"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/RetriggerDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2"><img alt="Retrigger" src="https://greptile-static-assets.s3.amazonaws.com/badges/Retrigger.svg?v=2" align="right"></picture></a><a href="https://app.greptile.com/hyperdx/github/hyperdxio/hyperdx/-/pull/3247"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptileDark.svg?v=2"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2"><img alt="View in Greptile" src="https://greptile-static-assets.s3.amazonaws.com/badges/ViewInGreptile.svg?v=2" align="right"></picture></a>Confidence Score: 4/5</h2>
[Medium impact] The change affects paging behavior for callers of external API v2 search\.
Fix the unusable final cursor before merging.
<h2><a href="https://app.greptile.com/ide/claude-code?prompt=%23%23%23%20Issue%201%0Apackages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%3A293-296%0AWith%20%60orderBy%3A%20%22Body%20ASC%22%60%2C%20exactly%2010%2C000%20matches%2C%20%60maxResults%3A%20100%60%2C%20and%20source%20%60max_result_rows%3A%20100%60%2C%20the%20last%20full%20page%20returns%20another%20cursor%20instead%20of%20finishing.%20This%20condition%20assumes%20more%20rows%20exist%20because%20the%20source%20limit%20prevents%20reading%20an%20extra%20row.%0A%0AThat%20cursor%20has%20offset%2010%2C000%2C%20so%20the%20next%20request%20returns%20400%20before%20querying%2C%20even%20though%20nothing%20remains.%20Allow%20an%20end-of-results%20check%20at%20the%20cap%20so%20the%20walk%20can%20finish%20with%20%60nextCursor%3A%20null%60.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInClaude.svg?v=7"><img alt="Fix All in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixAllInClaude.svg?v=7" align="right"></picture></a>Findings</h2>
1. <img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"> Completed searches end with errors <a href="https://github.com/hyperdxio/hyperdx/pull/3247#discussion_r4373695052">▶</a>
<h3>Summary</h3>
Adds signed cursor pagination to /api/v2/search, while preserving offset-only requests.
<details open><summary><h3>Diagram</h3></summary>
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Search request] --> B{Cursor supplied?}
B -->|No| C[Query full range with offset]
B -->|Yes| D[Start walk or verify signed cursor]
D --> E{Timestamp-led search?}
E -->|Yes| F[Query aligned time window]
E -->|No| G[Query full range with capped offset]
F --> H[Return rows and next cursor]
G --> H
C --> I[Return rows without next cursor]
</details>
<!-- greptile_confidence_score:4 -->
<sub>Reviews (16) · Last reviewed commit: "feat(api): window cursor walks led by a ..." · Reviewed by Greptile</sub>
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Offset paging misses older rows
When an existing client pages through a range longer than the first time window without sending a cursor, every request uses window 0. Its offset skips rows only within that window, so increasing the offset never reaches older matching rows. Offset-only requests need to keep searching the full requested range.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20189%0A%0AComment%3A%0AOffset%20paging%20misses%20older%20rows%0A%0AWhen%20an%20existing%20client%20pages%20through%20a%20range%20longer%20than%20the%20first%20time%20window%20without%20sending%20a%20cursor%2C%20every%20request%20uses%20window%200.%20Its%20%60offset%60%20skips%20rows%20only%20within%20that%20window%2C%20so%20increasing%20the%20offset%20never%20reaches%20older%20matching%20rows.%20Offset-only%20requests%20need%20to%20keep%20searching%20the%20full%20requested%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20189%0A%0AComment%3A%0AOffset%20paging%20misses%20older%20rows%0A%0AWhen%20an%20existing%20client%20pages%20through%20a%20range%20longer%20than%20the%20first%20time%20window%20without%20sending%20a%20cursor%2C%20every%20request%20uses%20window%200.%20Its%20%60offset%60%20skips%20rows%20only%20within%20that%20window%2C%20so%20increasing%20the%20offset%20never%20reaches%20older%20matching%20rows.%20Offset-only%20requests%20need%20to%20keep%20searching%20the%20full%20requested%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20189%0A%0AComment%3A%0AOffset%20paging%20misses%20older%20rows%0A%0AWhen%20an%20existing%20client%20pages%20through%20a%20range%20longer%20than%20the%20first%20time%20window%20without%20sending%20a%20cursor%2C%20every%20request%20uses%20window%200.%20Its%20%60offset%60%20skips%20rows%20only%20within%20that%20window%2C%20so%20increasing%20the%20offset%20never%20reaches%20older%20matching%20rows.%20Offset-only%20requests%20need%20to%20keep%20searching%20the%20full%20requested%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791335491&sig=74d070145126e896b03e9a99ed06393e4fb5a3a2471ff4b139551abaaee358c5&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20189%0A%0AComment%3A%0AOffset%20paging%20misses%20older%20rows%0A%0AWhen%20an%20existing%20client%20pages%20through%20a%20range%20longer%20than%20the%20first%20time%20window%20without%20sending%20a%20cursor%2C%20every%20request%20uses%20window%200.%20Its%20%60offset%60%20skips%20rows%20only%20within%20that%20window%2C%20so%20increasing%20the%20offset%20never%20reaches%20older%20matching%20rows.%20Offset-only%20requests%20need%20to%20keep%20searching%20the%20full%20requested%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Default times invalidate cursors
When a caller omits endTime, the endpoint resolves it to a fresh now for each request. The next page therefore has a different fingerprint even if the caller sends the same query and the returned cursor. Cursor validation responds with HTTP 400, preventing pagination with the documented default time range.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Fsearch.ts%0ALine%3A%20430-431%0A%0AComment%3A%0ADefault%20times%20invalidate%20cursors%0A%0AWhen%20a%20caller%20omits%20%60endTime%60%2C%20the%20endpoint%20resolves%20it%20to%20a%20fresh%20%60now%60%20for%20each%20request.%20The%20next%20page%20therefore%20has%20a%20different%20fingerprint%20even%20if%20the%20caller%20sends%20the%20same%20query%20and%20the%20returned%20cursor.%20Cursor%20validation%20responds%20with%20HTTP%20400%2C%20preventing%20pagination%20with%20the%20documented%20default%20time%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Fsearch.ts%0ALine%3A%20430-431%0A%0AComment%3A%0ADefault%20times%20invalidate%20cursors%0A%0AWhen%20a%20caller%20omits%20%60endTime%60%2C%20the%20endpoint%20resolves%20it%20to%20a%20fresh%20%60now%60%20for%20each%20request.%20The%20next%20page%20therefore%20has%20a%20different%20fingerprint%20even%20if%20the%20caller%20sends%20the%20same%20query%20and%20the%20returned%20cursor.%20Cursor%20validation%20responds%20with%20HTTP%20400%2C%20preventing%20pagination%20with%20the%20documented%20default%20time%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Fsearch.ts%0ALine%3A%20430-431%0A%0AComment%3A%0ADefault%20times%20invalidate%20cursors%0A%0AWhen%20a%20caller%20omits%20%60endTime%60%2C%20the%20endpoint%20resolves%20it%20to%20a%20fresh%20%60now%60%20for%20each%20request.%20The%20next%20page%20therefore%20has%20a%20different%20fingerprint%20even%20if%20the%20caller%20sends%20the%20same%20query%20and%20the%20returned%20cursor.%20Cursor%20validation%20responds%20with%20HTTP%20400%2C%20preventing%20pagination%20with%20the%20documented%20default%20time%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791335491&sig=899f5a52970995cbab23ae715a0caa1d1be2c9e2fe72802f923fe444b191328e&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Fsearch.ts%0ALine%3A%20430-431%0A%0AComment%3A%0ADefault%20times%20invalidate%20cursors%0A%0AWhen%20a%20caller%20omits%20%60endTime%60%2C%20the%20endpoint%20resolves%20it%20to%20a%20fresh%20%60now%60%20for%20each%20request.%20The%20next%20page%20therefore%20has%20a%20different%20fingerprint%20even%20if%20the%20caller%20sends%20the%20same%20query%20and%20the%20returned%20cursor.%20Cursor%20validation%20responds%20with%20HTTP%20400%2C%20preventing%20pagination%20with%20the%20documented%20default%20time%20range.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Rounded timestamps duplicate rows
For a source ordered and filtered by a rounded timestamp expression such as toStartOfHour(Timestamp), the query renderer keeps both time bounds inclusive even when this code requests a half-open window. Adjacent windows within the same hour can then return the same rows, so a cursor walk no longer returns each row exactly once.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20206-207%0A%0AComment%3A%0ARounded%20timestamps%20duplicate%20rows%0A%0AFor%20a%20source%20ordered%20and%20filtered%20by%20a%20rounded%20timestamp%20expression%20such%20as%20%60toStartOfHour%28Timestamp%29%60%2C%20the%20query%20renderer%20keeps%20both%20time%20bounds%20inclusive%20even%20when%20this%20code%20requests%20a%20half-open%20window.%20Adjacent%20windows%20within%20the%20same%20hour%20can%20then%20return%20the%20same%20rows%2C%20so%20a%20cursor%20walk%20no%20longer%20returns%20each%20row%20exactly%20once.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20206-207%0A%0AComment%3A%0ARounded%20timestamps%20duplicate%20rows%0A%0AFor%20a%20source%20ordered%20and%20filtered%20by%20a%20rounded%20timestamp%20expression%20such%20as%20%60toStartOfHour%28Timestamp%29%60%2C%20the%20query%20renderer%20keeps%20both%20time%20bounds%20inclusive%20even%20when%20this%20code%20requests%20a%20half-open%20window.%20Adjacent%20windows%20within%20the%20same%20hour%20can%20then%20return%20the%20same%20rows%2C%20so%20a%20cursor%20walk%20no%20longer%20returns%20each%20row%20exactly%20once.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20206-207%0A%0AComment%3A%0ARounded%20timestamps%20duplicate%20rows%0A%0AFor%20a%20source%20ordered%20and%20filtered%20by%20a%20rounded%20timestamp%20expression%20such%20as%20%60toStartOfHour%28Timestamp%29%60%2C%20the%20query%20renderer%20keeps%20both%20time%20bounds%20inclusive%20even%20when%20this%20code%20requests%20a%20half-open%20window.%20Adjacent%20windows%20within%20the%20same%20hour%20can%20then%20return%20the%20same%20rows%2C%20so%20a%20cursor%20walk%20no%20longer%20returns%20each%20row%20exactly%20once.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791335491&sig=7ff159925cffa88aa4a916d4d69feb498c657feb8b0330f70866ef8244e2e927&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20206-207%0A%0AComment%3A%0ARounded%20timestamps%20duplicate%20rows%0A%0AFor%20a%20source%20ordered%20and%20filtered%20by%20a%20rounded%20timestamp%20expression%20such%20as%20%60toStartOfHour%28Timestamp%29%60%2C%20the%20query%20renderer%20keeps%20both%20time%20bounds%20inclusive%20even%20when%20this%20code%20requests%20a%20half-open%20window.%20Adjacent%20windows%20within%20the%20same%20hour%20can%20then%20return%20the%20same%20rows%2C%20so%20a%20cursor%20walk%20no%20longer%20returns%20each%20row%20exactly%20once.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> <a href="#"><img alt="security" src="https://greptile-static-assets.s3.amazonaws.com/badges/Security.svg?v=2" align="top"></a> Cursor offsets bypass work limit
An API-key holder can construct a query-matching cursor with a large offset. This line uses that offset without the 10,000-row limit applied to the normal offset parameter, allowing a window query to do substantially more skip work than the endpoint permits for ordinary requests. The query fingerprint does not protect this limit because it is an unkeyed hash of request fields.
How this was verified: Any nonnegative offset in a cursor with a matching unkeyed query fingerprint reaches the ClickHouse OFFSET parameter.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20198%0A%0AComment%3A%0ACursor%20offsets%20bypass%20work%20limit%0A%0AAn%20API-key%20holder%20can%20construct%20a%20query-matching%20cursor%20with%20a%20large%20offset.%20This%20line%20uses%20that%20offset%20without%20the%2010%2C000-row%20limit%20applied%20to%20the%20normal%20%60offset%60%20parameter%2C%20allowing%20a%20window%20query%20to%20do%20substantially%20more%20skip%20work%20than%20the%20endpoint%20permits%20for%20ordinary%20requests.%20The%20query%20fingerprint%20does%20not%20protect%20this%20limit%20because%20it%20is%20an%20unkeyed%20hash%20of%20request%20fields.%0A%0AHow%20this%20was%20verified%3A%20Any%20nonnegative%20offset%20in%20a%20cursor%20with%20a%20matching%20unkeyed%20query%20fingerprint%20reaches%20the%20ClickHouse%20OFFSET%20parameter.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20198%0A%0AComment%3A%0ACursor%20offsets%20bypass%20work%20limit%0A%0AAn%20API-key%20holder%20can%20construct%20a%20query-matching%20cursor%20with%20a%20large%20offset.%20This%20line%20uses%20that%20offset%20without%20the%2010%2C000-row%20limit%20applied%20to%20the%20normal%20%60offset%60%20parameter%2C%20allowing%20a%20window%20query%20to%20do%20substantially%20more%20skip%20work%20than%20the%20endpoint%20permits%20for%20ordinary%20requests.%20The%20query%20fingerprint%20does%20not%20protect%20this%20limit%20because%20it%20is%20an%20unkeyed%20hash%20of%20request%20fields.%0A%0AHow%20this%20was%20verified%3A%20Any%20nonnegative%20offset%20in%20a%20cursor%20with%20a%20matching%20unkeyed%20query%20fingerprint%20reaches%20the%20ClickHouse%20OFFSET%20parameter.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20198%0A%0AComment%3A%0ACursor%20offsets%20bypass%20work%20limit%0A%0AAn%20API-key%20holder%20can%20construct%20a%20query-matching%20cursor%20with%20a%20large%20offset.%20This%20line%20uses%20that%20offset%20without%20the%2010%2C000-row%20limit%20applied%20to%20the%20normal%20%60offset%60%20parameter%2C%20allowing%20a%20window%20query%20to%20do%20substantially%20more%20skip%20work%20than%20the%20endpoint%20permits%20for%20ordinary%20requests.%20The%20query%20fingerprint%20does%20not%20protect%20this%20limit%20because%20it%20is%20an%20unkeyed%20hash%20of%20request%20fields.%0A%0AHow%20this%20was%20verified%3A%20Any%20nonnegative%20offset%20in%20a%20cursor%20with%20a%20matching%20unkeyed%20query%20fingerprint%20reaches%20the%20ClickHouse%20OFFSET%20parameter.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791335491&sig=f1bdf98fb237acf96ec41a0fa7ddfc2b7f6b6b3ec6ff53d7bf8a2173c5bf2fde&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20198%0A%0AComment%3A%0ACursor%20offsets%20bypass%20work%20limit%0A%0AAn%20API-key%20holder%20can%20construct%20a%20query-matching%20cursor%20with%20a%20large%20offset.%20This%20line%20uses%20that%20offset%20without%20the%2010%2C000-row%20limit%20applied%20to%20the%20normal%20%60offset%60%20parameter%2C%20allowing%20a%20window%20query%20to%20do%20substantially%20more%20skip%20work%20than%20the%20endpoint%20permits%20for%20ordinary%20requests.%20The%20query%20fingerprint%20does%20not%20protect%20this%20limit%20because%20it%20is%20an%20unkeyed%20hash%20of%20request%20fields.%0A%0AHow%20this%20was%20verified%3A%20Any%20nonnegative%20offset%20in%20a%20cursor%20with%20a%20matching%20unkeyed%20query%20fingerprint%20reaches%20the%20ClickHouse%20OFFSET%20parameter.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<!-- deep-review-state: diff=7ff5fda9a3bb27133346e8d5640995fddcde7b620698cb59c4dcb5bf844bf192; plugin=compound-engineering-v3.6.1 -->
<!-- deep-review -->
Deep Review
✅ No critical issues found. The windowing algorithm is sound, and the prior review round's P1 concerns (offset missing older rows, defaulted-time cursor invalidation, rounded-timestamp duplicate rows, dense-window rejectable cursors) are genuinely resolved and covered by tests. The exactly-once guarantee holds: compound timestamp expressions carry an exact part whose strict < bound keeps interior windows half-open even when a rounded leading key forces inclusive bounds, and the integration tests assert row identity (not just counts) across boundaries. One performance/reliability item is worth addressing before merge.
🟡 P2 — recommended
packages/api/src/routers/external-api/v2/utils/search.ts:234 — On the windowed (cut) path the MAX_SINGLE_WINDOW_OFFSET cap is applied only in the !cut branch, so a single time bucket denser than one page makes cutWindowAtBucket return offset + pageLength with no ceiling, and that growing offset is passed straight to the ClickHouse OFFSET, reintroducing the quadratic deep-paging scan cost the feature set out to remove (most easily triggered on rounded 15-minute leading keys where one bucket spans 15 minutes).- Fix: Bound the cumulative in-bucket cursor offset on the cut path and return
CURSOR_WINDOW_TOO_DENSE when a single bucket cannot be paged within the cap, so page cost stays flat or fails loudly instead of degrading silently.- <sub>security, performance, previous-comments</sub>
<details>
<summary>🔵 P3 nitpicks (7)</summary>
packages/api/src/routers/external-api/v2/search.ts:364 — The new cursor 400 conditions (invalid/expired cursor, query mismatch, window-too-dense) are not enumerated in the OpenAPI 400 docs, and the documented 200 example omits the now-always-present nextCursor field.- Fix: Enumerate the cursor error conditions in the 400 description and add
nextCursor to the 200 example in both the JSDoc and openapi.json.- <sub>api-contract</sub>
packages/api/src/routers/external-api/__tests__/search.int.test.ts:1121 — The range-change rejection only exercises the changed-startTime branch; the changed-endTime branch and the withThrowingOverflow custom-_overflow_mode regex branch are untested.- Fix: Add a case replaying a cursor with a changed explicit
endTime, and one with a source carrying a custom overflow-mode setting.- <sub>testing</sub>
packages/api/src/routers/external-api/v2/utils/search.ts:96 — resolveSearchOrderBy derivation branches (orderByExpression, displayedTimestampValueExpression, multi-part tuple) are untested; a wrong derived ORDER BY silently disengages windowing and falls back to offset paging.- Fix: Add coverage asserting the derived order leads with the timestamp so windowing engages for sources that omit request-level
orderBy.- <sub>testing</sub>
packages/api/src/routers/external-api/v2/utils/search.ts:270 — The chart config literal is both annotated and as-cast to ChartConfigWithDateRange, so the cast overrides the assignability check the annotation would perform (repo guide discourages as).- Fix: Construct
dateRange explicitly so the shape is inferred or satisfies-checked, and drop the as.- <sub>kieran-typescript, project-standards</sub>
packages/api/src/routers/external-api/v2/utils/searchWindows.ts:158 — nextSearchWindow/cumulativeWindowMs reimplement the progressive-bucketing scheme already in common-utils/core/searchWindows.ts, so API cursor paging and UI offset paging can silently diverge if the bucketing policy changes in only one place.- Fix: Add a cross-reference comment or factor the shared window-size math into one module.
- <sub>maintainability</sub>
packages/common-utils/src/core/utils.ts:7 — Foundational core/utils.ts imports stripTypeWrappers from the feature module core/eventDeltas, a layering inversion that becomes a real cycle the moment eventDeltas needs anything from utils.- Fix: Move
stripTypeWrappers into core/utils.ts and have eventDeltas import it from there.- <sub>maintainability</sub>
packages/api/src/routers/external-api/v2/utils/search.ts:1 — The file crosses the repo's 300-line guideline (~355 lines) with the new cursor/window state machine densely interleaved in runSearchConfig.- Fix: Extract the page-limit and next-cursor computation into a sibling helper module.
- <sub>project-standards, maintainability*</sub>
</details>
---
Reviewers (9): security, performance, api-contract, reliability, testing, maintainability, kieran-typescript, project-standards, previous-comments.
Testing gaps: No end-to-end test asserts bounded query cost (or a loud error) when a single bucket holds far more matching rows than a page — the dense-bucket behavior is only exercised at the pure-function level (searchWindowCut.test.ts:76), where it is asserted to grow unbounded. No test covers rounded 15-minute leading keys under high density, where the P2 offset growth is most impactful.
Coverage note: The correctness and adversarial reviewers did not return before synthesis; their lanes were partially covered by direct verification of the windowing state machine, the half-open/inclusive-bound interaction, and the dense-bucket path against the integration suite.
<sub>No finding blocks merge automatically. A maintainer will expect P0/P1 findings in code this PR changes to be fixed; P2/P3 are your call -- fix or reply. Never fix findings about surrounding code here; reply instead. Do not widen the PR. How to respond</sub>
@vinzee is attempting to deploy a commit to the HyperDX Team on Vercel.
A member of the Team first needs to authorize it.
🟠 major — Server issues cursors over its own offset cap, and the next request rejects them as malformed
offset: effectiveOffset + data.length can go past MAX_CURSOR_OFFSET (10,000). Example: a window with more than 12,000 matching rows and maxResults=2000 produces a cursor at offset 12,000, and decodeSearchCursor (utils/searchCursor.ts) rejects it with a 400 'Cursor is malformed'. A 24h window of logs easily holds that many rows, so the walk dies partway. Either split the window into smaller sub-windows when it fills up, or don't cap offsets the server generated itself (e.g. sign the cursor with an HMAC instead of bounding the offset). Add a test that walks past the cap.
<sub>Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond</sub>
<!-- hdxr:c4d1e80cea3d2b2c -->
🔵 minor — Window selection and next-page logic duplicates useOffsetPaginatedQuery
packages/app/src/hooks/useOffsetPaginatedQuery.tsx already does this: pick windows by isFirstOrderByAscending, fall back to one window when isTimestampExpressionInFirstOrderBy is false, and step to the next window on a short page. Move that logic into common-utils/src/core/searchWindows.ts next to the generators and call it from both places. The app hook would then also get the windowInclusivity fix; today it still returns rows on a window boundary twice.
<sub>Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond</sub>
<!-- hdxr:3a93a08a2e7faed9 -->
🔵 minor — The single-window fallback is written out twice
The non-cursor branch here repeats the fallback already inside resolveSearchWindows (line ~70). Add a forceSingleWindow flag to resolveSearchWindows, or pull the fallback into a shared helper, so the two copies can't drift apart.
<sub>Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond</sub>
<!-- hdxr:95612199d6ea8c6b -->
🔵 minor — Uses an as cast, which the repository conventions say to avoid
Build the argument with satisfies BuilderChartConfigWithOptTimestamp, or add a small helper that takes {orderBy, timestampValueExpression}, instead of as Parameters<typeof isTimestampExpressionInFirstOrderBy>[0]. The conventions say 'Avoid as casts'.
<sub>Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond</sub>
<!-- hdxr:39c5ffb9706f8fd6 -->
🔵 minor — INVALID_CURSOR was added to SearchErrorCode but runSearchConfig never returns it
Remove INVALID_CURSOR from SearchErrorCode and its codeToStatus case. The router answers decode errors itself before calling runSearchConfig, so that case can never run.
<sub>Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond</sub>
<!-- hdxr:3798815e913645a6 -->
🔵 minor — ?? '' / ?? 'lucene' fallbacks never apply
Remove the fallbacks for where, whereLanguage and select. searchRequestSchema already defaults them with .default('') / .default('lucene'), so none of them can be undefined here.
<sub>Advisory. Fix if it is a small defect in code this PR changes; otherwise reply in the thread. Do not widen the PR or touch files it did not already change. How to respond</sub>
<!-- hdxr:b3bf80fbf63c8d25 -->
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Dense windows invalidate cursors
A full page makes runSearchConfig add the number of returned rows to the cursor offset, with no cap. If one window has more than 10,000 matching rows, the server eventually returns a cursor with an offset above 10,000. This check rejects that server-generated cursor on the next request with HTTP 400, preventing the client from completing the walk.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2FsearchCursor.ts%0ALine%3A%2096%0A%0AComment%3A%0ADense%20windows%20invalidate%20cursors%0A%0AA%20full%20page%20makes%20%60runSearchConfig%60%20add%20the%20number%20of%20returned%20rows%20to%20the%20cursor%20offset%2C%20with%20no%20cap.%20If%20one%20window%20has%20more%20than%2010%2C000%20matching%20rows%2C%20the%20server%20eventually%20returns%20a%20cursor%20with%20an%20offset%20above%2010%2C000.%20This%20check%20rejects%20that%20server-generated%20cursor%20on%20the%20next%20request%20with%20HTTP%20400%2C%20preventing%20the%20client%20from%20completing%20the%20walk.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2FsearchCursor.ts%0ALine%3A%2096%0A%0AComment%3A%0ADense%20windows%20invalidate%20cursors%0A%0AA%20full%20page%20makes%20%60runSearchConfig%60%20add%20the%20number%20of%20returned%20rows%20to%20the%20cursor%20offset%2C%20with%20no%20cap.%20If%20one%20window%20has%20more%20than%2010%2C000%20matching%20rows%2C%20the%20server%20eventually%20returns%20a%20cursor%20with%20an%20offset%20above%2010%2C000.%20This%20check%20rejects%20that%20server-generated%20cursor%20on%20the%20next%20request%20with%20HTTP%20400%2C%20preventing%20the%20client%20from%20completing%20the%20walk.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2FsearchCursor.ts%0ALine%3A%2096%0A%0AComment%3A%0ADense%20windows%20invalidate%20cursors%0A%0AA%20full%20page%20makes%20%60runSearchConfig%60%20add%20the%20number%20of%20returned%20rows%20to%20the%20cursor%20offset%2C%20with%20no%20cap.%20If%20one%20window%20has%20more%20than%2010%2C000%20matching%20rows%2C%20the%20server%20eventually%20returns%20a%20cursor%20with%20an%20offset%20above%2010%2C000.%20This%20check%20rejects%20that%20server-generated%20cursor%20on%20the%20next%20request%20with%20HTTP%20400%2C%20preventing%20the%20client%20from%20completing%20the%20walk.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791338124&sig=18186d8a807419c629bbce3f6c57c199090ec2e9d8c46d0114508feae01c2396&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2FsearchCursor.ts%0ALine%3A%2096%0A%0AComment%3A%0ADense%20windows%20invalidate%20cursors%0A%0AA%20full%20page%20makes%20%60runSearchConfig%60%20add%20the%20number%20of%20returned%20rows%20to%20the%20cursor%20offset%2C%20with%20no%20cap.%20If%20one%20window%20has%20more%20than%2010%2C000%20matching%20rows%2C%20the%20server%20eventually%20returns%20a%20cursor%20with%20an%20offset%20above%2010%2C000.%20This%20check%20rejects%20that%20server-generated%20cursor%20on%20the%20next%20request%20with%20HTTP%20400%2C%20preventing%20the%20client%20from%20completing%20the%20walk.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> Avoid unnecessary type casts
The new legacy-window construction uses as const for both direction values, although a TimeWindow[] annotation or type inference can provide the needed type. The new window test fixture also uses a double cast. The repository's TypeScript guide requires avoiding as casts when inference or satisfies is possible; please satisfy that requirement before merging.
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-204%0A%0AComment%3A%0AAvoid%20unnecessary%20type%20casts%0A%0AThe%20new%20legacy-window%20construction%20uses%20%60as%20const%60%20for%20both%20direction%20values%2C%20although%20a%20%60TimeWindow%5B%5D%60%20annotation%20or%20type%20inference%20can%20provide%20the%20needed%20type.%20The%20new%20window%20test%20fixture%20also%20uses%20a%20double%20cast.%20The%20repository's%20TypeScript%20guide%20requires%20avoiding%20%60as%60%20casts%20when%20inference%20or%20%60satisfies%60%20is%20possible%3B%20please%20satisfy%20that%20requirement%20before%20merging.%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-204%0A%0AComment%3A%0AAvoid%20unnecessary%20type%20casts%0A%0AThe%20new%20legacy-window%20construction%20uses%20%60as%20const%60%20for%20both%20direction%20values%2C%20although%20a%20%60TimeWindow%5B%5D%60%20annotation%20or%20type%20inference%20can%20provide%20the%20needed%20type.%20The%20new%20window%20test%20fixture%20also%20uses%20a%20double%20cast.%20The%20repository's%20TypeScript%20guide%20requires%20avoiding%20%60as%60%20casts%20when%20inference%20or%20%60satisfies%60%20is%20possible%3B%20please%20satisfy%20that%20requirement%20before%20merging.%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-204%0A%0AComment%3A%0AAvoid%20unnecessary%20type%20casts%0A%0AThe%20new%20legacy-window%20construction%20uses%20%60as%20const%60%20for%20both%20direction%20values%2C%20although%20a%20%60TimeWindow%5B%5D%60%20annotation%20or%20type%20inference%20can%20provide%20the%20needed%20type.%20The%20new%20window%20test%20fixture%20also%20uses%20a%20double%20cast.%20The%20repository's%20TypeScript%20guide%20requires%20avoiding%20%60as%60%20casts%20when%20inference%20or%20%60satisfies%60%20is%20possible%3B%20please%20satisfy%20that%20requirement%20before%20merging.%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1791338124&sig=3facdde6ee9a10bd6f83bae642a16317c4d1514ee2fa78c2bd252d85ceef68ac&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-204%0A%0AComment%3A%0AAvoid%20unnecessary%20type%20casts%0A%0AThe%20new%20legacy-window%20construction%20uses%20%60as%20const%60%20for%20both%20direction%20values%2C%20although%20a%20%60TimeWindow%5B%5D%60%20annotation%20or%20type%20inference%20can%20provide%20the%20needed%20type.%20The%20new%20window%20test%20fixture%20also%20uses%20a%20double%20cast.%20The%20repository's%20TypeScript%20guide%20requires%20avoiding%20%60as%60%20casts%20when%20inference%20or%20%60satisfies%60%20is%20possible%3B%20please%20satisfy%20that%20requirement%20before%20merging.%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<!-- greptile_outside_diff -->
<h3>Comments Outside Diff</h3>
No findings outside the diff remain.
🔴 critical — The time range saved in the cursor is never used: each page recomputes its windows from that request's start/end times
cursorState.startTime/endTime are only copied into nextCursorState. resolveSearchWindows is still called with the request's startDate/endDate. When endTime is omitted, parseTimeRange (packages/api/src/mcp/tools/query/helpers.ts:257) sets it to a new now() on every call. Window N therefore moves between pages, and rows get skipped or duplicated with no error. The same happens if a client changes startTime/endTime in the middle of a walk, because the fingerprint doesn't include them. Fix: when a cursor is present, build the windows from new Date(cursorState.startTime)/new Date(cursorState.endTime). Also either reject a request whose explicit range differs from the saved one, or add the saved range to the fingerprint check.
<sub>Nothing blocks merge automatically, but a maintainer will expect this fixed if it is a real defect in code this PR changes. If it is about surrounding code, reply and say so instead of patching. Do not widen the PR. How to respond</sub>
<!-- hdxr:332504d4e28aa782 -->
Fixed in 3aa6b9e0c: a walk computes its windows from the range pinned in the cursor (walkStart/walkEnd from cursorState.startTime/endTime), not the request's. Covered by "pages with a defaulted endTime".
Fixed in ca2275962: MAX_CURSOR_OFFSET is now 1,000,000, and a walk that would exceed it moves to the next window instead of minting a cursor the decoder rejects.
Fixed in ca2275962: MAX_CURSOR_OFFSET is now 1,000,000, and a walk that would exceed it moves to the next window instead of returning a cursor the decoder rejects.
Fixed: the test now seeds its own rows ~40 minutes apart and asserts a deep offset reaches older rows outside the newest window.
Fixed in e9a6515a0: resolveSearchWindows takes { forceSingleWindow }, so the fallback lives in one place.
Fixed in e9a6515a0 for search.ts (the as const casts went with the duplicated fallback). Keeping the test fixture's as unknown as TSource; a full TSource there adds noise without coverage.
Fixed in e9a6515a0: removed INVALID_CURSOR and its codeToStatus case.
Fixed in e9a6515a0 for where/whereLanguage/select. orderBy has no schema default, so its fallback stays.
Keeping this one: isTimestampExpressionInFirstOrderBy takes a full chart config but only reads orderBy and timestampValueExpression. Dropping the cast means changing that shared helper's signature, which is outside this PR.
Out of scope here: the window generators already moved to common-utils; moving the app hook's next-page logic too would widen this PR into packages/app.
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> Pages break the requested order
resolveSearchWindows treats any first sort expression containing the timestamp name as time-ordered. For example, toHour(Timestamp) DESC passes this check, but hours restart each day. The endpoint then returns newer windows before older ones, even when older rows should sort first.
Only split the range when the first sort expression preserves timestamp order; otherwise use one window.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%2066-69%0A%0AComment%3A%0APages%20break%20the%20requested%20order%0A%0A%60resolveSearchWindows%60%20treats%20any%20first%20sort%20expression%20containing%20the%20timestamp%20name%20as%20time-ordered.%20For%20example%2C%20%60toHour%28Timestamp%29%20DESC%60%20passes%20this%20check%2C%20but%20hours%20restart%20each%20day.%20The%20endpoint%20then%20returns%20newer%20windows%20before%20older%20ones%2C%20even%20when%20older%20rows%20should%20sort%20first.%0A%0AOnly%20split%20the%20range%20when%20the%20first%20sort%20expression%20preserves%20timestamp%20order%3B%20otherwise%20use%20one%20window.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%2066-69%0A%0AComment%3A%0APages%20break%20the%20requested%20order%0A%0A%60resolveSearchWindows%60%20treats%20any%20first%20sort%20expression%20containing%20the%20timestamp%20name%20as%20time-ordered.%20For%20example%2C%20%60toHour%28Timestamp%29%20DESC%60%20passes%20this%20check%2C%20but%20hours%20restart%20each%20day.%20The%20endpoint%20then%20returns%20newer%20windows%20before%20older%20ones%2C%20even%20when%20older%20rows%20should%20sort%20first.%0A%0AOnly%20split%20the%20range%20when%20the%20first%20sort%20expression%20preserves%20timestamp%20order%3B%20otherwise%20use%20one%20window.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%2066-69%0A%0AComment%3A%0APages%20break%20the%20requested%20order%0A%0A%60resolveSearchWindows%60%20treats%20any%20first%20sort%20expression%20containing%20the%20timestamp%20name%20as%20time-ordered.%20For%20example%2C%20%60toHour%28Timestamp%29%20DESC%60%20passes%20this%20check%2C%20but%20hours%20restart%20each%20day.%20The%20endpoint%20then%20returns%20newer%20windows%20before%20older%20ones%2C%20even%20when%20older%20rows%20should%20sort%20first.%0A%0AOnly%20split%20the%20range%20when%20the%20first%20sort%20expression%20preserves%20timestamp%20order%3B%20otherwise%20use%20one%20window.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1792456524&sig=9f305d290bc28aae0b5677bcba949d3db8722151af83ef60907df05e56c47ff7&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%2066-69%0A%0AComment%3A%0APages%20break%20the%20requested%20order%0A%0A%60resolveSearchWindows%60%20treats%20any%20first%20sort%20expression%20containing%20the%20timestamp%20name%20as%20time-ordered.%20For%20example%2C%20%60toHour%28Timestamp%29%20DESC%60%20passes%20this%20check%2C%20but%20hours%20restart%20each%20day.%20The%20endpoint%20then%20returns%20newer%20windows%20before%20older%20ones%2C%20even%20when%20older%20rows%20should%20sort%20first.%0A%0AOnly%20split%20the%20range%20when%20the%20first%20sort%20expression%20preserves%20timestamp%20order%3B%20otherwise%20use%20one%20window.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P1" src="https://greptile-static-assets.s3.amazonaws.com/badges/p1.svg?v=9" align="top"></a> <a href="#"><img alt="security" src="https://greptile-static-assets.s3.amazonaws.com/badges/Security.svg?v=2" align="top"></a> Search can exhaust server memory
cursor: "start" makes the server build every time window before querying ClickHouse. parseTimeRange accepts any valid increasing dates, so an API-key holder can submit a range spanning JavaScript's supported dates and make this loop allocate roughly 200 million windows. This can exhaust server memory and block other requests before a query timeout applies.
Bound the range or calculate only the requested window.
How this was verified: Accepted request dates reach a synchronous loop that adds one window per day with no limit on the number of windows.
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-208%0A%0AComment%3A%0ASearch%20can%20exhaust%20server%20memory%0A%0A%60cursor%3A%20%22start%22%60%20makes%20the%20server%20build%20every%20time%20window%20before%20querying%20ClickHouse.%20%60parseTimeRange%60%20accepts%20any%20valid%20increasing%20dates%2C%20so%20an%20API-key%20holder%20can%20submit%20a%20range%20spanning%20JavaScript's%20supported%20dates%20and%20make%20this%20loop%20allocate%20roughly%20200%20million%20windows.%20This%20can%20exhaust%20server%20memory%20and%20block%20other%20requests%20before%20a%20query%20timeout%20applies.%0A%0ABound%20the%20range%20or%20calculate%20only%20the%20requested%20window.%0A%0AHow%20this%20was%20verified%3A%20Accepted%20request%20dates%20reach%20a%20synchronous%20loop%20that%20adds%20one%20window%20per%20day%20with%20no%20limit%20on%20the%20number%20of%20windows.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-208%0A%0AComment%3A%0ASearch%20can%20exhaust%20server%20memory%0A%0A%60cursor%3A%20%22start%22%60%20makes%20the%20server%20build%20every%20time%20window%20before%20querying%20ClickHouse.%20%60parseTimeRange%60%20accepts%20any%20valid%20increasing%20dates%2C%20so%20an%20API-key%20holder%20can%20submit%20a%20range%20spanning%20JavaScript's%20supported%20dates%20and%20make%20this%20loop%20allocate%20roughly%20200%20million%20windows.%20This%20can%20exhaust%20server%20memory%20and%20block%20other%20requests%20before%20a%20query%20timeout%20applies.%0A%0ABound%20the%20range%20or%20calculate%20only%20the%20requested%20window.%0A%0AHow%20this%20was%20verified%3A%20Accepted%20request%20dates%20reach%20a%20synchronous%20loop%20that%20adds%20one%20window%20per%20day%20with%20no%20limit%20on%20the%20number%20of%20windows.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-208%0A%0AComment%3A%0ASearch%20can%20exhaust%20server%20memory%0A%0A%60cursor%3A%20%22start%22%60%20makes%20the%20server%20build%20every%20time%20window%20before%20querying%20ClickHouse.%20%60parseTimeRange%60%20accepts%20any%20valid%20increasing%20dates%2C%20so%20an%20API-key%20holder%20can%20submit%20a%20range%20spanning%20JavaScript's%20supported%20dates%20and%20make%20this%20loop%20allocate%20roughly%20200%20million%20windows.%20This%20can%20exhaust%20server%20memory%20and%20block%20other%20requests%20before%20a%20query%20timeout%20applies.%0A%0ABound%20the%20range%20or%20calculate%20only%20the%20requested%20window.%0A%0AHow%20this%20was%20verified%3A%20Accepted%20request%20dates%20reach%20a%20synchronous%20loop%20that%20adds%20one%20window%20per%20day%20with%20no%20limit%20on%20the%20number%20of%20windows.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1792456524&sig=1f029736873998b854e655de27563dd68d6ac3c66fe3ad450295c3d139ae59e5&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2Fv2%2Futils%2Fsearch.ts%0ALine%3A%20203-208%0A%0AComment%3A%0ASearch%20can%20exhaust%20server%20memory%0A%0A%60cursor%3A%20%22start%22%60%20makes%20the%20server%20build%20every%20time%20window%20before%20querying%20ClickHouse.%20%60parseTimeRange%60%20accepts%20any%20valid%20increasing%20dates%2C%20so%20an%20API-key%20holder%20can%20submit%20a%20range%20spanning%20JavaScript's%20supported%20dates%20and%20make%20this%20loop%20allocate%20roughly%20200%20million%20windows.%20This%20can%20exhaust%20server%20memory%20and%20block%20other%20requests%20before%20a%20query%20timeout%20applies.%0A%0ABound%20the%20range%20or%20calculate%20only%20the%20requested%20window.%0A%0AHow%20this%20was%20verified%3A%20Accepted%20request%20dates%20reach%20a%20synchronous%20loop%20that%20adds%20one%20window%20per%20day%20with%20no%20limit%20on%20the%20number%20of%20windows.%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
<a href="#"><img alt="P2" src="https://greptile-static-assets.s3.amazonaws.com/badges/p2.svg?v=9" align="top"></a> Test divider breaks repository rules
The new cursor test section adds banner comments. The repository guide explicitly forbids banner comments and ASCII section dividers, including in tests. Remove these divider lines to satisfy the repository requirement before merging.
Context Used: CLAUDE.md (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
<a href="https://app.greptile.com/ide/claude-code?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2F__tests__%2Fsearch.int.test.ts%0ALine%3A%20561-563%0A%0AComment%3A%0ATest%20divider%20breaks%20repository%20rules%0A%0AThe%20new%20cursor%20test%20section%20adds%20banner%20comments.%20The%20repository%20guide%20explicitly%20forbids%20banner%20comments%20and%20ASCII%20section%20dividers%2C%20including%20in%20tests.%20Remove%20these%20divider%20lines%20to%20satisfy%20the%20repository%20requirement%20before%20merging.%0A%0A%60%60%60suggestion%0A%0A%60%60%60%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaudeDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"><img alt="Fix in Claude Code" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInClaude.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/conductor?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2F__tests__%2Fsearch.int.test.ts%0ALine%3A%20561-563%0A%0AComment%3A%0ATest%20divider%20breaks%20repository%20rules%0A%0AThe%20new%20cursor%20test%20section%20adds%20banner%20comments.%20The%20repository%20guide%20explicitly%20forbids%20banner%20comments%20and%20ASCII%20section%20dividers%2C%20including%20in%20tests.%20Remove%20these%20divider%20lines%20to%20satisfy%20the%20repository%20requirement%20before%20merging.%0A%0A%60%60%60suggestion%0A%0A%60%60%60%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"><img alt="Fix in Conductor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInConductor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/cursor?prompt=This%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2F__tests__%2Fsearch.int.test.ts%0ALine%3A%20561-563%0A%0AComment%3A%0ATest%20divider%20breaks%20repository%20rules%0A%0AThe%20new%20cursor%20test%20section%20adds%20banner%20comments.%20The%20repository%20guide%20explicitly%20forbids%20banner%20comments%20and%20ASCII%20section%20dividers%2C%20including%20in%20tests.%20Remove%20these%20divider%20lines%20to%20satisfy%20the%20repository%20requirement%20before%20merging.%0A%0A%60%60%60suggestion%0A%0A%60%60%60%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&expires=1792456524&sig=9f13888e504069289ba1c4d69f8fd35559248be398a6490700cfe610aceaa8fb&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursorDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"><img alt="Fix in Cursor" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCursor.svg?v=7"></picture></a> <a href="https://app.greptile.com/api/ide/codex?prompt=IMPORTANT%3A%20Work%20in%20the%20repository%20%22hyperdxio%2Fhyperdx%22%20on%20the%20existing%20branch%20%22va%2Fcursor-based-pagination%22.%20Checkout%20that%20branch%20%E2%80%94%20do%20NOT%20create%20a%20new%20branch%20or%20open%20a%20new%20PR.%20Push%20your%20changes%20to%20%22va%2Fcursor-based-pagination%22.%0A%0AThis%20is%20a%20comment%20left%20during%20a%20code%20review.%0APath%3A%20packages%2Fapi%2Fsrc%2Frouters%2Fexternal-api%2F__tests__%2Fsearch.int.test.ts%0ALine%3A%20561-563%0A%0AComment%3A%0ATest%20divider%20breaks%20repository%20rules%0A%0AThe%20new%20cursor%20test%20section%20adds%20banner%20comments.%20The%20repository%20guide%20explicitly%20forbids%20banner%20comments%20and%20ASCII%20section%20dividers%2C%20including%20in%20tests.%20Remove%20these%20divider%20lines%20to%20satisfy%20the%20repository%20requirement%20before%20merging.%0A%0A%60%60%60suggestion%0A%0A%60%60%60%0A%0AContext%20Used%3A%20CLAUDE.md%20%28%5Bsource%5D%28https%3A%2F%2Fgithub.com%2Fhyperdxio%2Fhyperdx%2Fblob%2Fmain%2FCLAUDE.md%29%29%0A%0A---%0A%0AFor%20each%20issue%20above%2C%20determine%20whether%20it%20is%20valid%20and%20should%20be%20fixed.%20If%20so%2C%20fix%20it%20directly.&repo=hyperdxio%2Fhyperdx&pr=3247&platform=github"><picture><source media="(prefers-color-scheme: dark)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodexDark.svg?v=7"><source media="(prefers-color-scheme: light)" srcset="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"><img alt="Fix in Codex" src="https://greptile-static-assets.s3.amazonaws.com/badges/FixInCodex.svg?v=7"></picture></a>
Fixed in af345729f: windows are used only when the first sort key is exactly the timestamp expression; a derived key like toHour(Timestamp) gets one window. Unit test added.
Fixed in af345729f: cursor walks over ranges longer than 366 days return 400, checked for decoded cursors too since the pinned range travels in the unsigned cursor. Integration test added.
Fixed in af345729f: removed the divider.
Replies to the summary findings:
OVERFLOW_MODE_SETTINGS misses other _overflow_mode settings: fixed in a4764398d. Any source setting ending in _overflow_mode (or _overflow_mode_leaf) is forced to throw, on top of the three defaults.typeof baseSelect === 'string' guard: removed in a4764398d._block_number/_block_offset), which is a follow-up.Nullable(Date) / LowCardinality(Date) now get inclusive bounds: not a change from this PR. timeFilterExpr on main already strips type wrappers before the Date check (from #3381); isRoundedTimestampPart keeps that behaviour.getColumn query per windowed page, small next to the page query itself. Carrying the column type in the cursor would grow the cursor and add a stale-schema case, so I'm leaving it as is.
vinzee
Summary
Problem
/api/v2/searchpages withLIMIT/OFFSET, so each page costs more than the last andoffsetstops at 10,000.Solution
Adds an optional
cursorrequest field and anextCursorresponse field.offsetcallers are unchanged.Cursor paging uses about 30% less ClickHouse CPU than offset paging on real pipedream-logs queries, with identical results. For queries sorted by timestamp,
OFFSETstays small however many pages you fetch, and the 10,000-row cap no longer applies.Paging through one hour of logs, 1,000 rows per page:
"start", then pass back eachnextCursoruntil it isnull. An empty or short page does not mean the end.cursoris set,offsetis ignored. The cursor is opaque.orderBybreaks the tie, as withoffset.How it works
TimestampTimedon't lose rows.OFFSETonly skips that second's rows and page cost stays flat in dense ranges.base64url(payload).base64url(HMAC), with payload:| Field | Meaning |
| :-- | :-- |
|
v| Cursor format version ||
k| Window number: which time window the next page reads from ||
ws,we| Current window's bounds; the window shrinks after each full page ||
o| Rows to skip: those already returned from the last row's second ||
f| Fingerprint ofsourceId,where,whereLanguage,select,orderBy; a mismatch is a 400 ||
s,e| The query's time range, fixed on the first page so a defaultedendTimedoesn't move |k,ws/weands/ealone: its near edge is the current window's far edge, and its far edge is the range edge offset by the total length of the firstk + 1windows. Nothing is built up front, so the range has no cap.EXPRESS_SESSION_SECRET, so they can't be edited; replicas need a shared value.toStartOfSecond/Minute/FiveMinutes/FifteenMinutes(...), use windows aligned to that key. Others (hour/day keys, non-time keys,DISTINCTselects) read the whole range with plain offsets and keep the 10,000 cap.Testing
search.int): every row returned exactly once across window boundaries, dense and burst seconds, compound timestamp expressions, both directions, tampered cursors.Screenshots or video
n/a
How to test on Vercel preview
n/a
References