state_management

Implements JSON state persistence with atomic writes, file locking, and crash recovery patterns.

Updated Jan 14, 2026
One-click install
npx skills add https://github.com/jvsandhu/agentic-skills --skill state-management-jvsandhu
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: state_management
Source: https://github.com/jvsandhu/agentic-skills/tree/main/skills/state_management
Command: npx skills add https://github.com/jvsandhu/agentic-skills --skill state-management-jvsandhu

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Stateful libraries and long-running batch operations lose progress or corrupt data when processes crash, restart, or run concurrently. This Skill provides standardized patterns for persisting state reliably across restarts and system failures. ## Core Features & Use Cases - Atomic JSON Persistence: Write state to temporary files and rename atomically to prevent corruption on crash. - File Locking & Concurrency: Use fcntl-based exclusive locks to protect state files from concurrent modification. - Crash Recovery & Versioning: Design resumable state with checkpoints, progress tracking, and schema migration paths. - Use Case: When building a batch processing tool that handles dozens of features, use the BatchStateManager pattern to track completed and failed items, then resume from the last checkpoint after an interruption with a --resume flag. ## Quick Start Ask the agent to implement state persistence for a batch processing library using atomic writes and crash recovery so it can resume after interruptions.

Frequently Asked Questions about state_management

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I persist state to JSON without corruption on crash?

Use atomic writes: write the JSON to a temporary file in the same directory, then call os.replace to rename it over the target. The rename is atomic, so a crash mid-write never leaves a partially written state file.

How to prevent concurrent access to a state file in Python?

Use fcntl.flock to acquire an exclusive lock on the open file handle before reading or writing. Wrap it in a context manager that releases the lock in a finally block so the lock is always freed.

How do I resume a batch process after a crash?

Design state with a batch ID, full feature list, current index, and completed/failed lists saved after each step. On restart, load the state file by batch ID and skip already-completed items to continue from the last checkpoint.

How do I handle state schema changes across versions?

Store a version field in the state and write a migrate function that chains upgrades step by step, such as 1.0.0 to 1.1.0 to 2.0.0. Apply migrations on load so old state files remain usable.

Does fcntl file locking work on Windows?

No, fcntl is Unix-only and raises ImportError on Windows. For cross-platform locking you need alternatives like msvcrt.locking or a portalocker-style library, which this pattern does not cover.