state_management

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

State Management Patterns Skill

状态管理模式技能

Standardized state management and persistence patterns for the autonomous-dev plugin ecosystem. Ensures reliable, crash-resistant state persistence across Claude restarts and system failures.
为autonomous-dev插件生态系统提供标准化的状态管理与持久化模式,确保在Claude重启或系统故障时实现可靠、抗崩溃的状态持久化。

When This Skill Activates

本技能的激活场景

  • Implementing state persistence
  • Managing crash recovery
  • Handling concurrent state access
  • Versioning state schemas
  • Tracking batch operations
  • Managing user preferences
  • Keywords: "state", "persistence", "JSON", "atomic", "crash recovery", "checkpoint"

  • 实现状态持久化时
  • 管理崩溃恢复时
  • 处理并发状态访问时
  • 版本化状态schema时
  • 跟踪批量操作时
  • 管理用户偏好时
  • 关键词:"state"、"persistence"、"JSON"、"atomic"、"crash recovery"、"checkpoint"

Core Patterns

核心模式

1. JSON Persistence with Atomic Writes

1. 基于原子写入的JSON持久化

Definition: Store state in JSON files with atomic writes to prevent corruption on crash.
Pattern:
python
import json
from pathlib import Path
from typing import Dict, Any
import tempfile
import os

def save_state_atomic(state: Dict[str, Any], state_file: Path) -> None:
    """Save state with atomic write to prevent corruption.

    Args:
        state: State dictionary to persist
        state_file: Target state file path

    Security:
        - Atomic Write: Prevents partial writes on crash
        - Temp File: Write to temp, then rename (atomic operation)
        - Permissions: Preserves file permissions
    """
    # Write to temporary file first
    temp_fd, temp_path = tempfile.mkstemp(
        dir=state_file.parent,
        prefix=f".{state_file.name}.",
        suffix=".tmp"
    )

    try:
        # Write JSON to temp file
        with os.fdopen(temp_fd, 'w') as f:
            json.dump(state, f, indent=2)

        # Atomic rename (overwrites target)
        os.replace(temp_path, state_file)

    except Exception:
        # Clean up temp file on failure
        if Path(temp_path).exists():
            Path(temp_path).unlink()
        raise
See:
docs/json-persistence.md
,
examples/batch-state-example.py

定义:将状态存储在JSON文件中,通过原子写入防止崩溃时的数据损坏。
实现代码:
python
import json
from pathlib import Path
from typing import Dict, Any
import tempfile
import os

def save_state_atomic(state: Dict[str, Any], state_file: Path) -> None:
    """通过原子写入保存状态,防止数据损坏。

    参数:
        state: 要持久化的状态字典
        state_file: 目标状态文件路径

    安全特性:
        - 原子写入:防止崩溃时出现部分写入
        - 临时文件:先写入临时文件,再重命名(原子操作)
        - 权限保留:维持文件原有权限
    """
    # 先写入临时文件
    temp_fd, temp_path = tempfile.mkstemp(
        dir=state_file.parent,
        prefix=f".{state_file.name}.",
        suffix=".tmp"
    )

    try:
        # 将JSON写入临时文件
        with os.fdopen(temp_fd, 'w') as f:
            json.dump(state, f, indent=2)

        # 原子重命名(覆盖目标文件)
        os.replace(temp_path, state_file)

    except Exception:
        # 失败时清理临时文件
        if Path(temp_path).exists():
            Path(temp_path).unlink()
        raise
参考文档
docs/json-persistence.md
,
examples/batch-state-example.py

2. File Locking for Concurrent Access

2. 用于并发访问的文件锁

Definition: Use file locks to prevent concurrent modification of state files.
Pattern:
python
import fcntl
import json
from pathlib import Path
from contextlib import contextmanager

@contextmanager
def file_lock(filepath: Path):
    """Acquire exclusive file lock for state file.

    Args:
        filepath: Path to file to lock

    Yields:
        Open file handle with exclusive lock

    Example:
        >>> with file_lock(state_file) as f:
        ...     state = json.load(f)
        ...     state['count'] += 1
        ...     f.seek(0)
        ...     f.truncate()
        ...     json.dump(state, f)
    """
    with filepath.open('r+') as f:
        fcntl.flock(f.fileno(), fcntl.LOCK_EX)
        try:
            yield f
        finally:
            fcntl.flock(f.fileno(), fcntl.LOCK_UN)
See:
docs/file-locking.md
,
templates/file-lock-template.py

定义:使用文件锁防止状态文件被并发修改。
实现代码:
python
import fcntl
import json
from pathlib import Path
from contextlib import contextmanager

@contextmanager
def file_lock(filepath: Path):
    """为状态文件获取排他性文件锁。

    参数:
        filepath: 要锁定的文件路径

    返回:
        带有排他锁的打开文件句柄

    示例:
        >>> with file_lock(state_file) as f:
        ...     state = json.load(f)
        ...     state['count'] += 1
        ...     f.seek(0)
        ...     f.truncate()
        ...     json.dump(state, f)
    """
    with filepath.open('r+') as f:
        fcntl.flock(f.fileno(), fcntl.LOCK_EX)
        try:
            yield f
        finally:
            fcntl.flock(f.fileno(), fcntl.LOCK_UN)
参考文档
docs/file-locking.md
,
templates/file-lock-template.py

3. Crash Recovery Pattern

3. 崩溃恢复模式

Definition: Design state to enable recovery after crashes or interruptions.
Principles:
  • State includes enough context to resume operations
  • Progress tracking enables "resume from last checkpoint"
  • State validation detects corruption
  • Migration paths handle schema changes
Example:
python
@dataclass
class BatchState:
    """Batch processing state with crash recovery support.

    Attributes:
        batch_id: Unique batch identifier
        features: List of all features to process
        current_index: Index of current feature
        completed: List of completed feature names
        failed: List of failed feature names
        created_at: State creation timestamp
        last_updated: Last update timestamp
    """
    batch_id: str
    features: List[str]
    current_index: int = 0
    completed: List[str] = None
    failed: List[str] = None
    created_at: str = None
    last_updated: str = None

    def __post_init__(self):
        if self.completed is None:
            self.completed = []
        if self.failed is None:
            self.failed = []
        if self.created_at is None:
            self.created_at = datetime.now().isoformat()
        self.last_updated = datetime.now().isoformat()
See:
docs/crash-recovery.md
,
examples/crash-recovery-example.py

定义:设计可在崩溃或中断后恢复的状态机制。
设计原则:
  • 状态包含足够上下文以恢复操作
  • 进度跟踪支持“从最后检查点恢复”
  • 状态验证可检测数据损坏
  • 提供迁移路径以处理schema变更
示例代码:
python
@dataclass
class BatchState:
    """支持崩溃恢复的批量处理状态。

    属性:
        batch_id: 唯一的批次标识符
        features: 所有待处理的功能列表
        current_index: 当前处理的功能索引
        completed: 已完成的功能名称列表
        failed: 处理失败的功能名称列表
        created_at: 状态创建时间戳
        last_updated: 最后更新时间戳
    """
    batch_id: str
    features: List[str]
    current_index: int = 0
    completed: List[str] = None
    failed: List[str] = None
    created_at: str = None
    last_updated: str = None

    def __post_init__(self):
        if self.completed is None:
            self.completed = []
        if self.failed is None:
            self.failed = []
        if self.created_at is None:
            self.created_at = datetime.now().isoformat()
        self.last_updated = datetime.now().isoformat()
参考文档
docs/crash-recovery.md
,
examples/crash-recovery-example.py

4. State Versioning and Migration

4. 状态版本化与迁移

Definition: Version state schemas to enable graceful upgrades.
Pattern:
python
STATE_VERSION = "2.0.0"

def migrate_state(state: Dict[str, Any]) -> Dict[str, Any]:
    """Migrate state from old version to current.

    Args:
        state: State dictionary (any version)

    Returns:
        Migrated state (current version)
    """
    version = state.get("version", "1.0.0")

    if version == "1.0.0":
        # Migrate 1.0.0 → 1.1.0
        state = _migrate_1_0_to_1_1(state)
        version = "1.1.0"

    if version == "1.1.0":
        # Migrate 1.1.0 → 2.0.0
        state = _migrate_1_1_to_2_0(state)
        version = "2.0.0"

    state["version"] = STATE_VERSION
    return state
See:
docs/state-versioning.md
,
templates/state-manager-template.py

定义:为状态schema添加版本,实现平滑升级。
实现代码:
python
STATE_VERSION = "2.0.0"

def migrate_state(state: Dict[str, Any]) -> Dict[str, Any]:
    """将旧版本状态迁移至当前版本。

    参数:
        state: 任意版本的状态字典

    返回:
        迁移后的当前版本状态
    """
    version = state.get("version", "1.0.0")

    if version == "1.0.0":
        # 从1.0.0迁移至1.1.0
        state = _migrate_1_0_to_1_1(state)
        version = "1.1.0"

    if version == "1.1.0":
        # 从1.1.0迁移至2.0.0
        state = _migrate_1_1_to_2_0(state)
        version = "2.0.0"

    state["version"] = STATE_VERSION
    return state
参考文档
docs/state-versioning.md
,
templates/state-manager-template.py

Real-World Examples

实际应用示例

BatchStateManager Pattern

BatchStateManager模式

From
plugins/autonomous-dev/lib/batch_state_manager.py
:
Features:
  • JSON persistence with atomic writes
  • Crash recovery via --resume flag
  • Progress tracking (completed/failed features)
  • Automatic context clearing at 150K tokens
  • State versioning for schema upgrades
Usage:
python
undefined
来自
plugins/autonomous-dev/lib/batch_state_manager.py
:
功能特性:
  • 基于原子写入的JSON持久化
  • 通过--resume标志实现崩溃恢复
  • 进度跟踪(已完成/失败功能)
  • 达到150K tokens时自动清理上下文
  • 支持状态版本化以实现schema升级
使用示例:
python
undefined

Create batch state

创建批次状态

manager = BatchStateManager.create(["feat1", "feat2", "feat3"]) manager.batch_id # "batch-20251116-123456"
manager = BatchStateManager.create(["feat1", "feat2", "feat3"]) manager.batch_id # "batch-20251116-123456"

Process features

处理功能

for feature in manager.features: if manager.should_clear_context(): # Clear context at 150K tokens manager.record_context_clear()
try:
    # Process feature
    result = process_feature(feature)
    manager.mark_completed(feature)
except Exception as e:
    manager.mark_failed(feature, str(e))

manager.save()  # Atomic write
for feature in manager.features: if manager.should_clear_context(): # 达到150K tokens时清理上下文 manager.record_context_clear()
try:
    # 处理功能
    result = process_feature(feature)
    manager.mark_completed(feature)
except Exception as e:
    manager.mark_failed(feature, str(e))

manager.save()  # 原子写入

Resume after crash

崩溃后恢复

manager = BatchStateManager.load("batch-20251116-123456") next_feature = manager.get_next_feature() # Skips completed
undefined
manager = BatchStateManager.load("batch-20251116-123456") next_feature = manager.get_next_feature() # 跳过已完成的功能
undefined

Checkpoint Integration (Issue #79)

检查点集成(Issue #79)

Agents save checkpoints using the portable pattern:
Agent使用可移植模式保存检查点:

Portable Pattern (Works Anywhere)

可移植模式(全场景适用)

python
from pathlib import Path
import sys
python
from pathlib import Path
import sys

Portable path detection

可移植路径检测

current = Path.cwd() while current != current.parent: if (current / ".git").exists(): project_root = current break current = current.parent
current = Path.cwd() while current != current.parent: if (current / ".git").exists(): project_root = current break current = current.parent

Add lib to path

将lib目录添加到路径

lib_path = project_root / "plugins/autonomous-dev/lib" if lib_path.exists(): sys.path.insert(0, str(lib_path))
try:
    from agent_tracker import AgentTracker
    success = AgentTracker.save_agent_checkpoint(
        agent_name='my-agent',
        message='Task completed - found 5 patterns',
        tools_used=['Read', 'Grep', 'WebSearch']
    )
    print(f"Checkpoint: {'saved' if success else 'skipped'}")
except ImportError:
    print("ℹ️ Checkpoint skipped (user project)")
undefined
lib_path = project_root / "plugins/autonomous-dev/lib" if lib_path.exists(): sys.path.insert(0, str(lib_path))
try:
    from agent_tracker import AgentTracker
    success = AgentTracker.save_agent_checkpoint(
        agent_name='my-agent',
        message='Task completed - found 5 patterns',
        tools_used=['Read', 'Grep', 'WebSearch']
    )
    print(f"Checkpoint: {'saved' if success else 'skipped'}")
except ImportError:
    print("ℹ️ Checkpoint skipped (user project)")
undefined

Features

功能特性

  • Portable: Works from any directory (user projects, subdirectories, fresh installs)
  • No hardcoded paths: Uses dynamic project root detection
  • Graceful degradation: Returns False, doesn't block workflow
  • Security validated: Path validation (CWE-22), no subprocess (CWE-78)
  • 可移植性: 可在任意目录(用户项目、子目录、全新安装环境)运行
  • 无硬编码路径: 使用动态项目根目录检测
  • 优雅降级: 保存失败时返回False,不阻塞工作流
  • 安全验证: 路径验证(CWE-22),无子进程调用(CWE-78)

Design Patterns

设计模式

  • Progressive Enhancement: Works with or without tracking infrastructure
  • Non-blocking: Never raises exceptions
  • Two-tier: Library imports instead of subprocess calls
See: LIBRARIES.md Section 24 (agent_tracker.py), DEVELOPMENT.md Scenario 2.5, docs/LIBRARIES.md for API

  • 渐进增强: 无论是否有跟踪基础设施均可工作
  • 非阻塞: 永远不会抛出异常
  • 双层架构: 通过库导入而非子进程调用实现
参考文档:LIBRARIES.md第24节(agent_tracker.py),DEVELOPMENT.md场景2.5,docs/LIBRARIES.md获取API详情

Usage Guidelines

使用指南

For Library Authors

针对库开发者

When implementing stateful features:
  1. Use JSON persistence with atomic writes
  2. Add file locking for concurrent access protection
  3. Design for crash recovery with resumable state
  4. Version your state for schema evolution
  5. Validate on load to detect corruption
实现有状态功能时:
  1. 使用JSON持久化并结合原子写入
  2. 添加文件锁以保护并发访问
  3. 为崩溃恢复设计可恢复的状态
  4. 为状态添加版本以支持schema演进
  5. 加载时验证以检测数据损坏

For Claude

针对Claude

When creating or analyzing stateful libraries:
  1. Load this skill when keywords match ("state", "persistence", etc.)
  2. Follow persistence patterns for reliability
  3. Implement crash recovery for long-running operations
  4. Use atomic operations to prevent corruption
  5. Reference templates in
    templates/
    directory
创建或分析有状态库时:
  1. 匹配关键词时加载本技能(如"state"、"persistence"等)
  2. 遵循持久化模式以确保可靠性
  3. 实现崩溃恢复以支持长时间运行的操作
  4. 使用原子操作以防止数据损坏
  5. 参考
    templates/
    目录中的模板

Token Savings

Token节省

By centralizing state management patterns in this skill:
  • Before: ~50 tokens per library for inline state management docs
  • After: ~10 tokens for skill reference comment
  • Savings: ~40 tokens per library
  • Total: ~400 tokens across 10 libraries (4-5% reduction)

通过将状态管理模式集中在本技能中:
  • 之前: 每个库的内联状态管理文档约占50 tokens
  • 之后: 仅需约10 tokens的技能引用注释
  • 节省: 每个库约节省40 tokens
  • 总计: 10个库共节省约400 tokens(减少4-5%)

Progressive Disclosure

渐进式披露

This skill uses Claude Code 2.0+ progressive disclosure architecture:
  • Metadata (frontmatter): Always loaded (~180 tokens)
  • Full content: Loaded only when keywords match
  • Result: Efficient context usage, scales to 100+ skills
When you use terms like "state management", "persistence", "crash recovery", or "atomic writes", Claude Code automatically loads the full skill content.

本技能采用Claude Code 2.0+的渐进式披露架构:
  • 元数据(前置内容): 始终加载(约180 tokens)
  • 完整内容: 仅在匹配关键词时加载
  • 效果: 高效利用上下文,可扩展至100+技能
当你使用"state management"、"persistence"、"crash recovery"或"atomic writes"等术语时,Claude Code会自动加载本技能的完整内容。

Templates and Examples

模板与示例

Templates (reusable code structures)

模板(可复用代码结构)

  • templates/state-manager-template.py
    : Complete state manager class
  • templates/atomic-write-template.py
    : Atomic write implementation
  • templates/file-lock-template.py
    : File locking utilities
  • templates/state-manager-template.py
    : 完整的状态管理器类
  • templates/atomic-write-template.py
    : 原子写入实现
  • templates/file-lock-template.py
    : 文件锁工具

Examples (real implementations)

示例(实际实现)

  • examples/batch-state-example.py
    : BatchStateManager pattern
  • examples/user-state-example.py
    : UserStateManager pattern
  • examples/crash-recovery-example.py
    : Crash recovery demonstration
  • examples/batch-state-example.py
    : BatchStateManager模式
  • examples/user-state-example.py
    : UserStateManager模式
  • examples/crash-recovery-example.py
    : 崩溃恢复演示

Documentation (detailed guides)

文档(详细指南)

  • docs/json-persistence.md
    : JSON storage patterns
  • docs/atomic-writes.md
    : Atomic write implementation
  • docs/file-locking.md
    : Concurrent access protection
  • docs/crash-recovery.md
    : Recovery strategies

  • docs/json-persistence.md
    : JSON存储模式
  • docs/atomic-writes.md
    : 原子写入实现
  • docs/file-locking.md
    : 并发访问保护
  • docs/crash-recovery.md
    : 恢复策略

Cross-References

交叉引用

This skill integrates with other autonomous-dev skills:
  • library-design-patterns: Two-tier design, progressive enhancement
  • error-handling-patterns: Exception handling and recovery
  • security-patterns: File permissions and path validation
See:
skills/library-design-patterns/
,
skills/error-handling-patterns/

本技能与其他autonomous-dev技能集成:
  • library-design-patterns: 双层设计、渐进增强
  • error-handling-patterns: 异常处理与恢复
  • security-patterns: 文件权限与路径验证
参考文档
skills/library-design-patterns/
,
skills/error-handling-patterns/

Maintenance

维护说明

This skill should be updated when:
  • New state management patterns emerge
  • State schema versioning needs change
  • Concurrency patterns evolve
  • Performance optimizations discovered
出现以下情况时应更新本技能:
  • 出现新的状态管理模式
  • 状态schema版本化需求变更
  • 并发模式演进
  • 发现性能优化方案

🔄 Workflow

🔄 工作流

Aşama 1: State Scoping & Selection

步骤1:状态范围界定与选择

  • Identify State Type: State'in tipini belirle (Server State, UI State, Form State, Global State).
  • Tool Selection: Karmaşıklığa göre
    useState
    ,
    Context
    ,
    Zustand
    veya
    Redux Toolkit
    arasından doğru aracı seç.
  • Single Source of Truth: Aynı verinin birden fazla yerde state olarak tutulmadığını (Derivable state) doğrula.
  • 识别状态类型:确定状态类型(Server State、UI State、Form State、Global State)。
  • 工具选择:根据复杂度从
    useState
    Context
    Zustand
    Redux Toolkit
    中选择合适工具。
  • 单一数据源:确保同一数据不会在多个地方作为状态存储(衍生状态)。

Aşama 2: Implementation & Sync

步骤2:实现与同步

  • Action/Mutation Design: State güncellemeleri için net fonksiyonlar veya reducer'lar tanımla.
  • Effect Syncing: State değişikliklerinin yan etkilerini (API çağrıları, LocalStorage) kontrollü bir şekilde yönet.
  • Persistence Strategy: Gerekli state'leri (örn: User session)
    persist
    middleware veya custom logic ile kalıcı hale getir.
  • 操作/变更设计:为状态更新定义清晰的函数或reducer。
  • 副作用同步:可控地管理状态变更的副作用(API调用、LocalStorage)。
  • 持久化策略:通过
    persist
    中间件或自定义逻辑将必要状态(如用户会话)持久化。

Aşama 3: Optimization & Debugging

步骤3:优化与调试

  • Selector Usage: Zustand/Redux kullanılıyorsa, sadece gerekli state parçalarını (
    selectors
    ) seçerek re-render'ı önle.
  • DevTools Audit: State değişimlerini Redux DevTools veya benzeri araçlarla adım adım izle.
  • Testing: State değişimlerinin beklenen sonuçları verip vermediğini unit testlerle doğrula.
  • 选择器使用:使用Zustand/Redux时,仅选择所需的状态片段(
    selectors
    )以避免不必要的重渲染。
  • 开发工具审计:通过Redux DevTools或类似工具逐步跟踪状态变更。
  • 测试验证:通过单元测试验证状态变更是否符合预期结果。

Kontrol Noktaları

检查点

AşamaDoğrulama
1State objesi doğrudan mutasyona uğratılıyor mu? (Immutability kontrolü)
2State hiyerarşisi "Prop Drilling" problemine yol açıyor mu?
3Loading ve Error state'leri kullanıcıya doğru şekilde yansıtılıyor mu?

State Management v1.5 - With Workflow
步骤验证内容
1状态对象是否被直接修改?(不可变性检查)
2状态层级是否导致“属性透传”问题?
3Loading与Error状态是否正确呈现给用户?

State Management v1.5 - 包含工作流