Skip to content

[desktop/macos] Global state: 107 singletons, 476 raw UserDefaults accesses, 62 inline string keys #8845

Open

Description

@Git-on-my-level

Area: Architecture — state management

Problem

App state flows through implicit globals rather than injected dependencies:

  • 107 files declare static let shared singletons (111 total). View models reach directly into them — e.g. Providers/ChatProvider.swift touches 16 distinct singletons (APIClient.shared, AuthService.shared, RewindDatabase.shared, MemoryStorage.shared, …), which is why it's effectively untestable and why the test suite has evolved into testing extracted policy structs instead.
  • 476 UserDefaults.standard call sites with 62+ distinct inline string keys. The same literal is retyped across files: "auth_userId" ×18, "playwrightExtensionToken" ×11, "hasCompletedFileIndexing" ×10, "chatBridgeMode" ×6. A typo compiles fine and silently reads nil/false — in auth and onboarding paths.
  • 123 NotificationCenter.default.post sites across 57 notification names used as a cross-module event bus (names are at least defined once each — good).

Why it matters

At ~1190 commits/month with agent authors, conventions held only by grep are probabilistically failing: nothing stops the 63rd key or the 108th singleton, and no compiler error catches "auth_userid".

Suggested fix (mechanical)

  1. Create one DefaultsKeys namespace (enum of static let keys, or a typed @UserDefault property-wrapper layer) and migrate call sites file-by-file.
  2. Add a CI grep-gate: forKey: " string literals are only allowed in the keys file(s); new violations fail lint. This freezes the problem immediately even before migration finishes.
  3. For testability, don't attempt global DI — just give the worst offenders (ChatProvider, TasksStore) initializer-injected protocol seams for the 2–3 dependencies their logic actually branches on, defaulting to .shared.

Positive pattern worth protecting

The "extract pure policy struct + unit-test it" pattern (ConversationReconciliationPolicy, DashboardTaskRefreshPolicy, PushToTalkStateMachine…) is the right countermeasure and should be named in AGENTS.md as the standard for new logic.

Activity

  1. added
    docs-tooling
    p3
    on Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docs-toolingp3refactor

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions