Skip to content

[codex] Normalize Windows Google sync state#7897

Open
tianmind-studio wants to merge 5 commits into
BasedHardware:mainfrom
tianmind-studio:codex/windows-google-sync-state-normalize
Open

[codex] Normalize Windows Google sync state#7897
tianmind-studio wants to merge 5 commits into
BasedHardware:mainfrom
tianmind-studio:codex/windows-google-sync-state-normalize

Conversation

@tianmind-studio

@tianmind-studio tianmind-studio commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add a pure normalizeSourceState helper for persisted Google sync source state.
  • Use it when reading google-sync.json from Electron userData so partial, legacy, or malformed per-source entries fall back safely.
  • Drop invalid processed IDs, dedupe while preserving the latest occurrence, stop scanning once MAX_PROCESSED newest unique IDs are found, and keep the existing bound during normalization.
  • Format the touched sync-state files with the current Windows app dev dependencies.

Why

The Windows app stores Google sync progress in a local JSON file. A partially written, manually edited, or older-shaped file could currently pass non-array processedIds or invalid timestamps into sync logic. That can break filtering or produce bad lastSyncAt values even though the file read itself succeeded.

Refresh

  • Rebased on main at 1a5824403b.
  • Current head: ad12f482c1.
  • Preserved the Greptile-requested early break in the backwards dedupe loop; the review thread is already resolved.

Validation

  • vitest run src/main/integrations/syncStateLogic.test.ts -> 1 file passed, 8 tests passed
  • npm run typecheck -> passed
  • prettier --check src/main/integrations/syncState.ts src/main/integrations/syncStateLogic.ts src/main/integrations/syncStateLogic.test.ts -> passed
  • git diff --check origin/main...HEAD
  • scripts/pre-commit with the backend Windows venv and local Dart SDK on PATH

@greptile-apps

greptile-apps Bot commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a normalizeSourceState helper that sanitizes the SourceState read from google-sync.json on Windows, replacing the previous ?? emptySourceState() fallback that only handled a missing top-level key. The helper validates field types, drops invalid/empty IDs, deduplicates while preserving the latest occurrence, and bounds the result to MAX_PROCESSED entries.

  • syncStateLogic.ts gains the pure normalizeSourceState function; syncState.ts uses it for both gmail and calendar when reading the file.
  • Three new unit tests cover null/malformed input, mixed-validity ID lists with deduplication, and MAX_PROCESSED bounding.

Confidence Score: 4/5

Safe to merge — the change is additive, well-tested, and purely defensive; the only concern is a minor efficiency gap in the dedup loop.

The normalization logic is correct across all covered cases and the tests faithfully verify them. The one gap is that the backward dedup loop does not exit once MAX_PROCESSED unique IDs are found, so a severely oversized processedIds array in a corrupted file would still be iterated in full. In practice this file is written by the app and is itself bounded to MAX_PROCESSED, so the impact is negligible, but it is a latent inefficiency worth addressing.

desktop/windows/src/main/integrations/syncStateLogic.ts — the dedup loop early-exit

Important Files Changed

Filename Overview
desktop/windows/src/main/integrations/syncStateLogic.ts Adds normalizeSourceState to sanitize persisted JSON data — validates types, deduplicates IDs keeping the latest occurrence, and bounds to MAX_PROCESSED. The loop collects all unique valid IDs before slicing, so it does not short-circuit once MAX_PROCESSED entries are found.
desktop/windows/src/main/integrations/syncState.ts Swaps ?? emptySourceState() for normalizeSourceState() when reading both Gmail and calendar state from the JSON file. Minimal, correct change.
desktop/windows/src/main/integrations/syncStateLogic.test.ts Adds three well-targeted tests for normalizeSourceState: null/malformed input, mixed-validity IDs with dedup, and MAX_PROCESSED bounding. All test expectations are correct.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["read() in syncState.ts"] --> B["JSON.parse(readFileSync(...))"]
    B --> C["normalizeSourceState(raw.gmail)"]
    B --> D["normalizeSourceState(raw.calendar)"]
    C --> E{value is object?}
    D --> E
    E -- No --> F["emptySourceState()"]
    E -- Yes --> G{lastSyncAt valid number > 0?}
    G -- No --> H["lastSyncAt = 0"]
    G -- Yes --> I["keep lastSyncAt"]
    H --> J{processedIds is Array?}
    I --> J
    J -- No --> K["processedIds = []"]
    J -- Yes --> L["Iterate backwards, filter non-string/empty, dedupe keeping last occurrence"]
    L --> M{len > MAX_PROCESSED?}
    M -- Yes --> N["slice to keep newest MAX_PROCESSED"]
    M -- No --> O["keep as-is"]
    K --> P["Return SourceState"]
    N --> P
    O --> P
    F --> P
Loading

Reviews (1): Last reviewed commit: "fix(windows): normalize Google sync stat..." | Re-trigger Greptile

Comment thread desktop/windows/src/main/integrations/syncStateLogic.ts
@tianmind-studio tianmind-studio force-pushed the codex/windows-google-sync-state-normalize branch from 4523fd1 to bce5cdd Compare June 16, 2026 23:47
@tianmind-studio tianmind-studio force-pushed the codex/windows-google-sync-state-normalize branch from bce5cdd to ad12f48 Compare June 17, 2026 10:41
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