Skip to content

Clipboard hardening: extract domain from main.ts + fix image write race #35

@pablopunk

Description

@pablopunk

Goal

Extract the clipboard-history domain out of main.ts using the DI pattern with tests, and fix the fire-and-forget clipboard image write race (merged from #34).

Merged from

Why this matters

src/electron/main.ts is ~5,369 lines and the single highest-churn file in the repo. The clipboard domain is a well-bounded cluster of functions with module-level mutable state. Extracting it follows the established createX(deps) pattern used by app-icon-cache, app-index-service, etc. Additionally, persistClipboardImage returns a path synchronously while the write runs un-awaited — a correctness hazard that the extraction naturally fixes.

To do

Phase 1: Fix the image write race (was #34)

  • Make persistClipboardImage async — await mkdir + writeFile
  • Return null on write failure (instead of a path to a missing file)
  • Update callers to await and handle null

Phase 2: Extract clipboard domain (was #35)

  • Inventory all clipboard functions and their dependencies
  • Create src/electron/clipboard-history.tscreateClipboardHistory(deps) mirroring app-icon-cache.ts
  • Move function bodies verbatim, swapping only module-state reads → deps.getHistory() / deps.setHistory()
  • Wire instance in main.ts, delete inline copies
  • Create src/electron/clipboard-history.test.ts — characterization tests with fakes
  • Verify: pnpm typecheck + pnpm test exit 0, clipboard behavior unchanged via pnpm palette:debug

Out of scope

  • Other domains in main.ts (calculator, extensions, app index, AI)
  • IPC handler names/signatures — behavior must be identical
  • src/electron/clipboard-utils.ts (already extracted)

Verification

  • mise exec -- pnpm typecheck → exit 0
  • mise exec -- pnpm test → exit 0
  • mise exec -- pnpm palette:debug → clipboard items still resolve
  • main.ts line count decreased (wc -l src/electron/main.ts)

Metadata

Metadata

Assignees

No one assigned

    Labels

    improveSurfaced by the improve skilltech-debtTech debt / refactor

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions