Skip to content

Desktop: Add auth error backoff to all polling operations - #5786

Closed
beastoin wants to merge 7 commits into
mainfrom
fix/desktop-error-backoff
Closed

beastoin wants to merge 7 commits into
mainfrom
fix/desktop-error-backoff

Conversation

@beastoin

Copy link
Copy Markdown
Collaborator

Summary

  • Adds AuthBackoffTracker — a shared singleton with exponential backoff (5s → 15s → 30s → 60s cap) that tracks consecutive 401/Unauthorized errors
  • Integrates backoff into all 5 polling call sites: ChatProvider (15s), AppState conversations (30s), TasksStore (30s), MemoriesPage (30s), CrispManager (120s)
  • Each poller skips its cycle when in backoff period, reports auth failures on 401, and resets on success
  • Eliminates repetitive Unauthorized error log spam that makes local dev logs unusable for real debugging

Fixes #5784 (friction #10)

How it works

When any poller gets a 401, AuthBackoffTracker.shared.reportAuthFailure() is called. All subsequent pollers check shouldSkipRequest() at entry — if we're in a backoff window, they return early without hitting the API. On any successful API call, reportSuccess() resets the counter. This means one 401 immediately suppresses all polling, and recovery is instant on the first successful call.

Test plan

  • Build succeeds on macOS (verified locally on Mac Mini)
  • Sign in → verify all pollers work normally (no backoff)
  • Sign out or use expired token → verify log spam stops after first 401
  • Sign back in → verify pollers resume immediately

by AI for @beastoin

🤖 Generated with Claude Code

beastoin and others added 7 commits March 18, 2026 07:57
Shared singleton that tracks consecutive 401/Unauthorized errors
across all polling operations. Backoff intervals: 5s, 15s, 30s,
60s cap. Resets on any successful auth call.

Fixes #5784 (friction #10: error log spam)

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Skip poll when in backoff period, report 401 as auth failure,
report success on clean fetch. Reduces Unauthorized log spam
from every 15s to exponentially-backed-off intervals.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Skip refresh when in backoff period, track 401 failures and
successes. Prevents repeated Unauthorized errors every 30s.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Skip task refresh when in backoff period, track auth failures
and successes on the shared backoff tracker.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Skip memory refresh when in backoff period, track auth failures
and successes on the shared backoff tracker.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
Skip Crisp message poll when in backoff period, track auth
failures and successes on the shared backoff tracker.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@greptile-apps

greptile-apps Bot commented Mar 18, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR introduces AuthBackoffTracker — a @MainActor singleton that applies exponential backoff (5 s → 15 s → 30 s → 60 s) across all five desktop polling call sites to suppress repetitive 401 log spam. The approach is well-structured and the integration pattern (shouldSkipRequest / reportAuthFailure / reportSuccess) is consistent across most call sites.

Key concerns:

  • Concurrent pollers inflate the failure counter — when the backoff window expires all pollers check shouldSkipRequest() simultaneously, all receive false, and if all receive 401 they each call reportAuthFailure(). This increments consecutiveFailures by N (number of pollers) in a single real failure event, jumping straight to the 60 s cap on what the user experiences as the first failure. The documented 5 s → 15 s → 30 s → 60 s escalation is not guaranteed.
  • TasksStore secondary fetches are missing auth-failure reporting — after reportSuccess() is called for the incomplete-tasks fetch, the two subsequent do-catch blocks (completed tasks, deleted tasks) silently swallow 401 errors without calling reportAuthFailure(), inconsistent with every other integration in this PR.
  • Backoff escalation is not logged — only the first failure emits a log message; transitions between backoff tiers are silent, making auth diagnostics harder.

Confidence Score: 3/5

  • Safe to merge for the log-spam goal, but the concurrent-poller inflation issue means the documented backoff escalation is not guaranteed in practice.
  • The feature achieves its primary objective (eliminating 401 log spam) and the @MainActor design prevents data races. However, the concurrent-poller inflation bug can cause the backoff to skip directly to 60 s on the first real auth failure, and the secondary-fetch gap in TasksStore leaves some 401s untracked — both are fixable before merge.
  • Pay close attention to desktop/Desktop/Sources/AuthBackoffTracker.swift (concurrent-inflation logic) and desktop/Desktop/Sources/Stores/TasksStore.swift (missing auth-failure reporting in secondary fetches).

Important Files Changed

Filename Overview
desktop/Desktop/Sources/AuthBackoffTracker.swift New singleton implementing exponential backoff. Has a logic issue: when the backoff window expires multiple pollers can all escape simultaneously, each calling reportAuthFailure() and inflating consecutiveFailures from 0 to N in one event — bypassing the intended 5 s → 15 s → 30 s → 60 s escalation and jumping directly to 60 s. Also only logs on the first failure; backoff escalation transitions are silent.
desktop/Desktop/Sources/Stores/TasksStore.swift Backoff guard added to the primary incomplete-tasks fetch, but the two subsequent do-catch blocks (completed tasks, deleted tasks) do not call reportAuthFailure() on 401 — inconsistent with the rest of the PR.
desktop/Desktop/Sources/AppState.swift Backoff guard and success/failure reporting correctly integrated around the primary getConversations call; the secondary getConversationsCount catch silently swallows errors, but that call is post-success so auth failures there are extremely unlikely.
desktop/Desktop/Sources/MainWindow/CrispManager.swift Backoff guard and success/failure reporting cleanly integrated; only covers the single fetch path so no secondary-call inconsistency.
desktop/Desktop/Sources/MainWindow/Pages/MemoriesPage.swift Backoff guard and success/failure reporting correctly integrated with no secondary call gaps.
desktop/Desktop/Sources/Providers/ChatProvider.swift Backoff guard and success/failure reporting correctly integrated; single fetch path with no secondary-call inconsistency.
desktop/CHANGELOG.json Changelog entry added for the auth backoff feature.

Sequence Diagram

sequenceDiagram
    participant P1 as ChatProvider (15s)
    participant P2 as AppState (30s)
    participant P3 as TasksStore (30s)
    participant P4 as MemoriesPage (30s)
    participant P5 as CrispManager (120s)
    participant T as AuthBackoffTracker
    participant API as API Server

    Note over P1,P5: Normal polling — consecutiveFailures = 0
    P1->>T: shouldSkipRequest() → false
    P1->>API: GET /messages → 200 OK
    P1->>T: reportSuccess()

    Note over P1,P5: Auth fails — all pollers in-flight simultaneously
    P1->>T: shouldSkipRequest() → false
    P2->>T: shouldSkipRequest() → false
    P3->>T: shouldSkipRequest() → false
    P1->>API: GET /messages → 401
    P2->>API: GET /conversations → 401
    P3->>API: GET /tasks → 401
    P1->>T: reportAuthFailure() → failures=1, backoff=5s
    P2->>T: reportAuthFailure() → failures=2, backoff=15s
    P3->>T: reportAuthFailure() → failures=3, backoff=30s
    Note over T: ⚠️ Jumps to 30s instead of intended 5s

    Note over P1,P5: Backoff active — pollers skipped
    P1->>T: shouldSkipRequest() → true (skip)
    P2->>T: shouldSkipRequest() → true (skip)
    P4->>T: shouldSkipRequest() → true (skip)

    Note over P1,P5: Backoff expires — user signs back in
    P1->>T: shouldSkipRequest() → false
    P1->>API: GET /messages → 200 OK
    P1->>T: reportSuccess() → failures=0 (reset)
    Note over P1,P5: All pollers resume normally
Loading

Comments Outside Diff (1)

  1. desktop/Desktop/Sources/Stores/TasksStore.swift, line 255-282 (link)

    P2 Secondary fetches silently swallow 401s without reporting auth failure

    After reportSuccess() is called for the incomplete-tasks fetch at line 246, refreshTasksIfNeeded continues to make two more API calls — for completed tasks (lines 258–282) and deleted tasks (~lines 286+). Both of those catch blocks log the error but never call AuthBackoffTracker.shared.reportAuthFailure() on a 401.

    While a 401 on a secondary fetch right after a successful primary fetch is unlikely, it is possible (e.g., token expires mid-function), and it would leave the tracker in a "success" state even though auth just failed. For consistency with every other call site in this PR, both secondary catches should mirror the pattern used in the primary block:

    } catch {
        if case APIError.unauthorized = error {
            AuthBackoffTracker.shared.reportAuthFailure()
        }
        logError("TasksStore: Auto-refresh completed tasks failed", error: error)
    }

    The same gap exists in the deleted-tasks catch block further down.

Last reviewed commit: "Add changelog entry ..."

Comment on lines +31 to +36
func reportAuthFailure() {
consecutiveFailures += 1
lastFailureTime = Date()
if consecutiveFailures == 1 {
log("AuthBackoff: first auth failure, backing off")
}

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.

P2 Concurrent pollers inflate failure count on first error

When all pollers are at consecutiveFailures == 0, they all call shouldSkipRequest() and all receive false, so every in-flight poll will proceed to the API simultaneously. If they all receive a 401 (e.g., on app startup with an expired token), each one calls reportAuthFailure() serially on the main actor. Five pollers will increment the counter from 0 → 5 in a single failure event, so index = min(4, 3) = 3 and the backoff jumps directly to 60 s, bypassing the intended 5 s → 15 s → 30 s → 60 s escalation.

A simple guard — only increment on the first such report per backoff cycle — would preserve the intended escalation:

func reportAuthFailure() {
    // If we're already in a backoff window, don't reset the clock or bump the counter again —
    // other pollers that slipped through before the window was established shouldn't re-trigger.
    if shouldSkipRequest() {
        return
    }
    consecutiveFailures += 1
    lastFailureTime = Date()
    if consecutiveFailures == 1 {
        log("AuthBackoff: first auth failure, backing off")
    } else {
        log("AuthBackoff: failure #\(consecutiveFailures), backing off \(backoffIntervals[min(consecutiveFailures - 1, backoffIntervals.count - 1)])s")
    }
}

This way the counter only increments when a new API call escapes the backoff window and fails, which matches the documented 5 s → 15 s → 30 s → 60 s progression.

Comment on lines +31 to +37
func reportAuthFailure() {
consecutiveFailures += 1
lastFailureTime = Date()
if consecutiveFailures == 1 {
log("AuthBackoff: first auth failure, backing off")
}
}

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.

P2 Backoff escalation transitions are not logged

Only the first failure (consecutiveFailures == 1) produces a log message. When the counter advances through the backoff ladder (5 s → 15 s → 30 s → 60 s), none of those transitions are surfaced. During local debugging it can be hard to tell which backoff tier is active without adding a breakpoint.

Consider logging each new tier:

func reportAuthFailure() {
    consecutiveFailures += 1
    lastFailureTime = Date()
    let backoff = backoffIntervals[min(consecutiveFailures - 1, backoffIntervals.count - 1)]
    log("AuthBackoff: failure #\(consecutiveFailures), next retry in \(backoff)s")
}

@github-actions

Copy link
Copy Markdown
Contributor

Hey @beastoin 👋

Thank you so much for taking the time to contribute to Omi! We truly appreciate you putting in the effort to submit this pull request.

After careful review, we've decided not to merge this particular PR. Please don't take this personally — we genuinely try to merge as many contributions as possible, but sometimes we have to make tough calls based on:

  • Project standards — Ensuring consistency across the codebase
  • User needs — Making sure changes align with what our users need
  • Code best practices — Maintaining code quality and maintainability
  • Project direction — Keeping aligned with our roadmap and vision

Your contribution is still valuable to us, and we'd love to see you contribute again in the future! If you'd like feedback on how to improve this PR or want to discuss alternative approaches, please don't hesitate to reach out.

Thank you for being part of the Omi community! 💜

@undivisible
undivisible deleted the fix/desktop-error-backoff branch August 20, 2026 11:34
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.

Desktop app: onboarding and local dev friction points

1 participant