Skip to content

refactor(architecture): decouple main state bridges and extract store transform helpers - #1084

Merged
vastsa merged 2 commits into
vastsa:mainfrom
Totopo27:refactor/main-and-store-hotspots
Sep 26, 2026
Merged

vastsa merged 2 commits into
vastsa:mainfrom
Totopo27:refactor/main-and-store-hotspots

Conversation

@Totopo27

Copy link
Copy Markdown
Contributor

Note: This PR builds upon #1083 (security boundaries). Once #1083 lands, this PR will automatically update to show only the architectural refactoring diff.

Summary

Decomposes apps/desktop/electron/main/index.ts by extracting scattered mutable lifecycle bridges into a dedicated MainProcessState container (apps/desktop/electron/main/bootstrap/main-state.ts), reducing the main entrypoint by over 450 lines of repetitive accessor boilerplate while strictly preserving contract semantics. In addition, pure data transforms are decoupled from the central Zustand store (apps/desktop/src/stores/app-store.ts).

Key Changes

  1. State Container Pattern (bootstrap/main-state.ts):

    • Encapsulates mutable runtime references (mainWindow, host, sidecar, quitting, closeBehavior, tray, etc.) into a cohesive MainProcessState class.
    • Houses the bridge interfaces consumed by subsystem factories:
      • launcherState: LauncherState
      • windowLifecycleState: WindowLifecycleState
      • runtimeState: RuntimeState
      • applicationLifecycleState: ApplicationLifecycleState
      • applicationAppearanceState: ApplicationAppearanceState
      • planRuntimeState: PlanRuntimeState
      • startupState: StartupState
      • shutdownState: ShutdownState
  2. Entrypoint Simplification (index.ts):

    • Replaced more than 350 lines of standalone getter/setter boilerplate across index.ts with unified destructuring from mainState.
    • Entrypoint line count decreased from 1,484 lines down to 1,029 lines (~31% reduction), bringing it closer to repository architectural ratchet bounds (AGENTS.md §7).
  3. Workspace Git Resolution Extraction (workspace-git.ts):

    • Extracted withGitBranch into a dedicated workspace utility module.
  4. Error Formatting Centralization (main-process-errors.ts):

    • Consolidated describeError into the error-handling module alongside installMainProcessErrorHandlers.
  5. Store Decoupling (stores/helpers/store-helpers.ts & stores/app-store.ts):

    • Extracted 13 pure workspace and session data transformations into store-helpers.ts, dropping app-store.ts from 736 to 617 lines (~16% reduction), complying with AGENTS.md §8 ("Pure transformation → Reducer / helper").

Verification

  • Executed full test runner across Electron Main, BrowserView, and UI Store suites with 54/54 passing tests.

…bootstrap isolation

Harden filesystem boundary enforcement, release checksum validation, and bootstrap scripting across Electron Main, BrowserView, and deployment scripts.

Previously, browser-view.ts path containment checks did not resolve symlinks, which could allow workspace symlinks targeting files outside the workspace root to be loaded into the preview browser pane. In addition, attachment blob references and release checksum parsing relied on unconstrained segment lookups or loose falsy gates rather than strict hexadecimal SHA-256 schema validation.

This change canonicalizes workspace paths with realpathSync, mandates exact 64-character hexadecimal SHA-256 formatting for attachments, restricts local bootstrap directories and tarballs to mode 700/600, eliminates TOCTOU checks in boot verification scripts, and updates git hygiene rules for certificates.
… transform helpers

Decompose monolithic entrypoint and central store hotspots to comply with architectural line count and responsibility boundaries (AGENTS.md §7 and §8).

In apps/desktop/electron/main/index.ts, more than 350 lines were dedicated to manual getter/setter bridges across 8 mutable state interfaces. In apps/desktop/src/stores/app-store.ts, pure workspace and session data transforms were mixed into the Zustand store definition.

This change introduces MainProcessState in bootstrap/main-state.ts to encapsulate main runtime mutable state, extracts workspace git resolution into workspace-git.ts, centralizes error formatting in main-process-errors.ts, and extracts pure session and workspace transformations into stores/helpers/store-helpers.ts. This reduces index.ts from 1,484 to 1,029 lines and app-store.ts from 736 to 617 lines.
vastsa added a commit that referenced this pull request Sep 26, 2026
Integrate PRs #1083, #1084, and #1085 with the reviewed fixes and validated task/integration E2E candidates.
@vastsa
vastsa merged commit bc650f4 into vastsa:main Sep 26, 2026
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.

2 participants