Skip to content

feat(ourlogs): Switch needle in haystack to time based #111946

Closed
k-fish wants to merge 2 commits intomasterfrom
fix/logs/fix-continue-scanning-improve-depth
Closed

feat(ourlogs): Switch needle in haystack to time based #111946
k-fish wants to merge 2 commits intomasterfrom
fix/logs/fix-continue-scanning-improve-depth

Conversation

@k-fish
Copy link
Copy Markdown
Member

@k-fish k-fish commented Mar 31, 2026

We were doing a static number of fetches before. Depending on cacheing etc. this can be really fast or slow, but it resulted in you clicking 'continue scanning' a LOT.

This should bring down the number of times you have to click continue. The initial page is 10 seconds to show you the button faster and to correct in case the user accidentally made a huge query. Subsequent presses are longer (20 seconds) since you are assumed to be fine with waiting / have the correct conditions.

k-fish added 2 commits March 31, 2026 16:40
We were doing a static number of fetches before. Depending on cacheing
etc. this can be really fast or slow, but it resulted in you clicking
'continue scanning' a LOT. This should bring down the number of times
you have to click to ever 20 seconds, aside from the initial faster to
return 5 second result.
@k-fish k-fish requested a review from a team as a code owner March 31, 2026 20:49
@k-fish k-fish changed the title Fix/logs/fix continue scanning improve depth feat(ourlogs): Switch needle in haystack to time based Mar 31, 2026
@github-actions github-actions bot added the Scope: Frontend Automatically applied to PRs that change frontend components label Mar 31, 2026
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 3 potential issues.

Fix All in Cursor

Bugbot Autofix prepared fixes for all 3 issues found in the latest run.

  • ✅ Fixed: Autofetch timer never resets per new query
    • Added useEffect to reset autoFetchStartTime, autoFetchDuration, and autoFetchRequestCount when queryKeyWithInfinite changes.
  • ✅ Fixed: Resume duration leaks into later searches
    • The useEffect that triggers on queryKeyWithInfinite changes now resets autoFetchDuration to FLEX_TIME_INITIAL_SEARCH_DURATION_MS for each new query.
  • ✅ Fixed: Time-only autofetch can flood API requests
    • Added autoFetchRequestCount state with maxAutoFetchRequests limit (50) to prevent unbounded request loops during fast-response scenarios.

Create PR

Or push these changes by commenting:

@cursor push 69f20acc5c
Preview (69f20acc5c)
diff --git a/static/app/views/explore/logs/useLogsQuery.tsx b/static/app/views/explore/logs/useLogsQuery.tsx
--- a/static/app/views/explore/logs/useLogsQuery.tsx
+++ b/static/app/views/explore/logs/useLogsQuery.tsx
@@ -694,13 +694,24 @@
   const [autoFetchDuration, setAutoFetchDuration] = useState(
     FLEX_TIME_INITIAL_SEARCH_DURATION_MS
   );
+  const [autoFetchRequestCount, setAutoFetchRequestCount] = useState(0);
+  const maxAutoFetchRequests = 50;
+
+  useEffect(() => {
+    setAutoFetchStartTime(Date.now());
+    setAutoFetchDuration(FLEX_TIME_INITIAL_SEARCH_DURATION_MS);
+    setAutoFetchRequestCount(0);
+  }, [queryKeyWithInfinite]);
+
   const canAutoFetchNextPage =
     !!highFidelity &&
     hasNextPage &&
     nextPageHasData &&
     (lastPageLength === 0 || _data.length < limit);
   const shouldAutoFetchNextPage =
-    canAutoFetchNextPage && Date.now() - autoFetchStartTime < autoFetchDuration;
+    canAutoFetchNextPage &&
+    Date.now() - autoFetchStartTime < autoFetchDuration &&
+    autoFetchRequestCount < maxAutoFetchRequests;
 
   const autoFetchPageCount = useRef(0);
   const prevShouldAutoFetch = useRef(false);
@@ -735,6 +746,7 @@
     }
 
     autoFetchPageCount.current += 1;
+    setAutoFetchRequestCount(prev => prev + 1);
     _fetchNextPage();
   }, [shouldAutoFetchNextPage, isFetchingNextPage, _fetchNextPage, nextPageCursor]);
 
@@ -770,6 +782,7 @@
     resumeAutoFetch: () => {
       setAutoFetchStartTime(Date.now());
       setAutoFetchDuration(FLEX_TIME_RESUME_SEARCH_DURATION_MS);
+      setAutoFetchRequestCount(0);
     },
     dataScanned,
     bytesScanned: totalBytesScanned,

This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

const [autoFetchStartTime, setAutoFetchStartTime] = useState<number>(() => Date.now());
const [autoFetchDuration, setAutoFetchDuration] = useState(
FLEX_TIME_INITIAL_SEARCH_DURATION_MS
);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Autofetch timer never resets per new query

Medium Severity

autoFetchStartTime is initialized once and only refreshed by resumeAutoFetch, so the 10-second autofetch window is measured from hook mount instead of each new search. After the page has been open long enough, new queries in useLogsQuery.tsx stop auto-fetching immediately and require manual Continue Scanning clicks.

Additional Locations (1)
Fix in Cursor Fix in Web

resumeAutoFetch: () => {
setAutoFetchStartTime(Date.now());
setAutoFetchDuration(FLEX_TIME_RESUME_SEARCH_DURATION_MS);
},
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Resume duration leaks into later searches

Low Severity

resumeAutoFetch sets autoFetchDuration to FLEX_TIME_RESUME_SEARCH_DURATION_MS, but that value is never restored for later queries. After one resume, subsequent “initial” searches in useLogsQuery.tsx inherit the longer 20s window, so the shorter first-pass behavior no longer applies.

Additional Locations (1)
Fix in Cursor Fix in Web

(lastPageLength === 0 || _data.length < limit);
const shouldAutoFetchNextPage = canAutoFetchNextPage && autoFetchesRemaining > 0;
const shouldAutoFetchNextPage =
canAutoFetchNextPage && Date.now() - autoFetchStartTime < autoFetchDuration;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Time-only autofetch can flood API requests

Medium Severity

Replacing maxAutoFetches with only a wall-clock check in useLogsQuery.tsx removes the request-count guard. When empty pages return quickly, shouldAutoFetchNextPage stays true and the loop can issue many sequential _fetchNextPage calls within 10–20 seconds, creating significantly higher backend load than before.

Additional Locations (1)
Fix in Cursor Fix in Web

// the original state starts at -1 because we have to count
// the 1 query made by default outside of the auto fetches
const [autoFetchesRemaining, setAutoFetchesRemaining] = useState(maxAutoFetches - 1);
const [autoFetchStartTime, setAutoFetchStartTime] = useState<number>(() => Date.now());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Bug: The auto-fetch timer state (autoFetchStartTime, autoFetchDuration) is not reset when the user changes query parameters, preventing or delaying auto-fetching on subsequent searches.
Severity: HIGH

Suggested Fix

Use a useEffect hook that triggers when the query key (queryKeyWithInfinite) changes. Inside this effect, reset the autoFetchStartTime state to Date.now() and the autoFetchDuration state to the initial value (FLEX_TIME_INITIAL_SEARCH_DURATION_MS). This will ensure every new, distinct query starts with a fresh auto-fetch timer.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: static/app/views/explore/logs/useLogsQuery.tsx#L693

Potential issue: The state variables `autoFetchStartTime` and `autoFetchDuration` are
initialized when the `useInfiniteLogsQuery` hook mounts but are not reset when query
parameters change. If a user waits more than 10 seconds after the page loads and then
modifies their query (e.g., changes a filter or search term), the new query will not
auto-fetch new logs because the condition `Date.now() - autoFetchStartTime <
autoFetchDuration` will be false. Additionally, if a user clicks "Continue Scanning"
(which sets the duration to 20s) and then runs a new query, the initial auto-fetch for
that new query will incorrectly use a 20-second duration instead of the intended 10
seconds.

Did we get this right? 👍 / 👎 to inform future reviews.

@JoshuaKGoldberg
Copy link
Copy Markdown
Member

I'm continuing this in #113219.

@linear-code
Copy link
Copy Markdown

linear-code bot commented Apr 16, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Scope: Frontend Automatically applied to PRs that change frontend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants