Skip to content

fix: foreground-aware derive-debounce so results are ready when the app opens - #46

Merged
abdulsaheel merged 1 commit into
mainfrom
fix/foreground-aware-derive-debounce
Jul 8, 2026
Merged

fix: foreground-aware derive-debounce so results are ready when the app opens#46
abdulsaheel merged 1 commit into
mainfrom
fix/foreground-aware-derive-debounce

Conversation

@abdulsaheel

Copy link
Copy Markdown
Collaborator

Summary

DeriveDebouncer picked its wait window purely from data staleness — a
catch-up sync's incoming records approaching "now" (exactly the moment the
user is watching for) flipped the debounce into its SLOWEST tier (60s
quiet / 5min floor), since neither this layer nor DeriveScheduler's own
8s settle knew whether a human was actively looking at the screen. The
"Analyze now" button felt instant only because it calls
DerivationEngine.run() directly, skipping both debounce layers.

Adds a third, foreground-aware tier (5s quiet / 15s floor) that takes
priority over the staleness-based tiers whenever the app is in the
foreground — reusing the _background flag AppState already tracks for
the derive-scheduler's own gate, no new state introduced. Background
behavior is unchanged.

Test plan

  • 5 new DeriveDebouncer tests (foreground tier priority, its own
    floor, overriding stale mode, and a regression test proving existing
    callers with no isForeground arg are unaffected)
  • flutter test --concurrency=1 — all green
  • flutter analyze — no new issues

…pp opens

DeriveDebouncer previously picked its wait window purely from dataStaleness
(how far the last decoded record is from wall-clock now): fresh mode (data
staleness < 30min) waits up to 60s quiet / 5min floor, stale/catch-up mode
waits 12s/90s. The paradox: as a catch-up sync's incoming records approach
"now" — exactly the moment the user is waiting on — staleness drops below
the threshold and the debounce flips into its SLOWEST tier. Neither this
layer nor DeriveScheduler's own 8s settle knew whether a human was actively
watching the screen; the "Analyze now" button felt fast only because it
calls DerivationEngine.run() directly, skipping both.

Adds a third tier that takes priority whenever the app is in the
foreground (5s quiet / 15s floor, reusing the existing `_background` flag
AppState already tracks for the derive-scheduler's own foreground gate) —
the fresh-mode rationale (avoid paying compute cost for every trickle of
ambient live data) doesn't apply when the screen is already on. Background
behavior is completely unchanged.

5 new tests covering the foreground tier, including a regression test that
existing callers with no isForeground arg behave exactly as before.
@abdulsaheel
abdulsaheel merged commit 4a769fc into main Jul 8, 2026
@abdulsaheel abdulsaheel mentioned this pull request Jul 8, 2026
@abdulsaheel
abdulsaheel deleted the fix/foreground-aware-derive-debounce branch July 12, 2026 05:53
abdulsaheel added a commit that referenced this pull request Aug 19, 2026
The pin was still sitting on the PR branch head. PR #46 merged it to
main as bfea5e5, two commits ahead of the old pin — both test-only
deprecation-ignore annotations, lib/ untouched, so no kAlgoVersion
bump needed for this move.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant