home / skills / harborgrid-justin / lexiflow-premium / concurrent-safe-state-machines
/frontend/.github-skills/concurrent-safe-state-machines
This skill helps you design deterministic state machines that stay correct under concurrent rendering and re-entrancy in modern UI frameworks.
npx playbooks add skill harborgrid-justin/lexiflow-premium --skill concurrent-safe-state-machinesReview the files below or copy the command above to add this skill to your agents.
---
name: concurrent-safe-state-machines
description: Design deterministic state machines that remain correct under concurrent rendering and re-entrancy.
---
# Concurrent-Safe State Machines (React 18)
## Summary
Design deterministic state machines that remain correct under concurrent rendering and re-entrancy.
## Key Capabilities
- Apply idempotent reducers and effect cleanup patterns.
- Model state transitions as pure functions with replay tolerance.
- Prevent torn reads during interleaved renders.
## PhD-Level Challenges
- Prove invariants under double-invocation in StrictMode.
- Provide a correctness argument for side-effect isolation.
- Stress-test state transitions under randomized scheduling.
## Acceptance Criteria
- Document state invariants and transition table.
- Demonstrate correctness under StrictMode double effects.
- Provide property-based tests for state machine correctness.
This skill teaches how to design deterministic state machines that remain correct under concurrent rendering, re-entrancy, and StrictMode double-invocation. It focuses on patterns that make transitions idempotent, side effects isolatable, and reads immune to torn/interleaved renders. The result is predictable UI state, easier reasoning, and robust tests that hold under randomized scheduling.
The approach models every transition as a pure, replay-tolerant reducer and separates effects into deterministic setup/cleanup pairs. Reducers are idempotent so repeated or interleaved invocations yield the same next-state. Side effects are scheduled and cleaned up so double-invocation and cancellation don’t produce visible divergence. The skill also includes strategies to avoid torn reads by deriving UI from a single source of truth and using snapshotting where appropriate.
How do idempotent reducers handle async work?
Keep reducers pure and push async work into effects that record intent; effects should be cancel-safe and cleaned up so duplicate invocations have no additional side-effects.
What prevents torn reads during interleaved renders?
Read from a single, authoritative snapshot of state or compute derived values in pure selectors; avoid reading separate pieces of mutable state across async boundaries.