Block It Out

TODO: Formal Verification & State Machine for Shadow Sync

Problem Statement

The shadow sync system has had multiple critical bugs due to complex state management:

  1. Duplicate shadows being created
  2. Orphan detection incorrectly deleting valid shadows
  3. Store keys vs shadow note mismatches (timestamp handling)
  4. Phase transitions between different sync strategies

Required: TLA+ Formal Model

State Machine Definition

Create a formal TLA+ specification that defines:

States

State Transitions

NONE -> CREATED: First sync creates shadow
CREATED -> SYNCED: Store record saved successfully
SYNCED -> SYNCED: Inputs unchanged (skip)
SYNCED -> STALE: Source event inputs changed
STALE -> SYNCED: Shadow updated with new inputs
SYNCED -> ORPHANED: Source event deleted/removed
ORPHANED -> DELETED: Orphan cleanup runs
DELETED -> NONE: Cleanup complete

Invariants to Verify

  1. No Duplicates: For each (ruleID, sourceEventID, destCalendarID), at most ONE shadow exists
  2. Store Consistency: If shadow exists, store MUST have matching record
  3. Orphan Correctness: Shadow is orphan IFF source event no longer in sync range
  4. Key Format Consistency: Store keys and shadow note IDs must use same format (with or without timestamp)
  5. Idempotency: Running sync twice with no changes should result in 0 insertions, 0 deletions

Implementation Requirements

1. Formal Specification

2. Code Structure

3. Testing

4. Key Format Standardization

Current Bugs Fixed

References

Priority: HIGH

This must be addressed comprehensively to prevent continued regression bugs in the sync system.