Skip to content

fix(tasks): adaptive visibility-aware polling for cloud task list#2336

Draft
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/adaptive-task-list-polling
Draft

fix(tasks): adaptive visibility-aware polling for cloud task list#2336
posthog[bot] wants to merge 1 commit into
mainfrom
posthog-code/adaptive-task-list-polling

Conversation

@posthog
Copy link
Copy Markdown
Contributor

@posthog posthog Bot commented May 24, 2026

Problem

Users who step away from the desktop and create cloud tasks elsewhere (e.g. on mobile while on a walk) return to a stale task list — the new tasks don't appear until a manual reload, undermining the cross-device promise of cloud tasks.

Two compounding issues in useTasks:

  • Fixed 30s polling regardless of visibility. refetchInterval: 30_000 keeps hammering the API even when the window is hidden, and never adapts when the user is clearly idle on the task list.
  • The on-return refetch was silently swallowed. The global staleTime is 5 minutes and refetchOnWindowFocus: true means "refetch on focus if the query is stale". Returning within 5 minutes left the query "fresh", so no refetch fired — and 5 minutes is exactly the window where this hurts. The previous PR description's claim that refetchOnWindowFocus was working in practice was incorrect: it never loaded the new tasks in practice.

Refs #2335 — alternative implementation that adds adaptive backoff and unit-test coverage.

Changes

apps/code/src/renderer/features/tasks/hooks/useTasks.ts

  • New useAdaptiveTaskListPolling(queryKey) helper used by useTasks, useTaskSummaries, and useSlackTasks.
  • Polling starts at 30s while the window is focused, exponentially backs off (30s → 60s → 120s → 180s) and caps at a 3-minute ceiling.
  • Pauses entirely when the window is blurred or the document is hidden (returns false from refetchInterval, plus refetchIntervalInBackground: false).
  • On the blur→focus transition, resets the backoff and explicitly invalidateQueries({ queryKey }) so the list refreshes immediately — independent of TanStack's staleness gate.

apps/code/src/renderer/features/tasks/hooks/useTasks.test.tsx (new)

  • Asserts the minimum-interval start, the exponential backoff with ceiling, the pause-on-blur behavior, the focus-return invalidate + backoff reset, and that the initial focused mount does not over-invalidate.
  • Drives the real focus pathway by mocking document.hasFocus() and dispatching focus / blur events, exercising the same useRendererWindowFocusStore wiring production uses.

How did you test this?

  • pnpm --filter code test useTasks.test — 6 tests pass.
  • pnpm --filter code test queryClient — existing focus-manager tests still pass.
  • pnpm lint clean on the touched files.
  • pnpm --filter code typecheck — the new code introduces no new errors (pre-existing failures in unrelated @posthog/platform/* and @posthog/git/* modules persist on main).
  • I could not run the Electron app in this sandbox, so the on-return refetch hasn't been observed in a real window. Worth a smoke test before un-drafting: blur the window, create a cloud task from another client, return to the desktop, confirm the list refreshes within a beat (from the explicit invalidate) rather than waiting up to 3 minutes for the next poll.

Publish to changelog?

no


Created with PostHog Code

The cloud task list polled every 30s on a fixed timer regardless of
window visibility, and the global 5-minute staleTime caused the
implicit refetchOnWindowFocus path to silently skip the refetch when
returning within that window — so tasks created elsewhere (e.g. on
mobile while away from desktop) never appeared until the user
manually reloaded.

Add a visibility-aware adaptive poll in useTasks / useTaskSummaries /
useSlackTasks: start at 30s while the window is focused, exponentially
back off to a 3-minute ceiling, pause entirely while blurred or hidden,
and invalidate the list immediately on focus return so the refresh
happens explicitly rather than going through TanStack's staleness
gate.

Add useTasks.test.tsx covering the polling cadence, pause-on-hidden,
the focus-return invalidate, and that the initial mount does not
trigger an extra invalidate.

Refs #2335.

Generated-By: PostHog Code
Task-Id: 0707617f-64d9-4a1b-b45a-b2e22e5cfc25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants