11 KiB
CLAUDE.md
This file provides guidance to Claude Code when working with the Stateful Infinite Loop variant.
Project Overview
This is a variant of the Infinite Agentic Loop pattern that adds robust state management with self-consistency validation. The system enables resumable, auditable, and deduplication-guaranteed infinite loop execution.
Key Innovation
Applies self-consistency prompting (multiple sampling + majority voting) to state validation:
- 6 independent validation checks verify state integrity
- Majority voting produces consistency score (0.0-1.0)
- High-confidence validation even if individual checks have edge cases
- Proven AI technique adapted to state management
Key Commands
Running Stateful Infinite Loops
claude
Then use these commands:
/infinite-stateful
Main orchestration command with state management.
# Basic usage
/infinite-stateful <spec_path> <output_dir> <count> [url_strategy] [run_id]
# Examples:
/infinite-stateful specs/example_spec.md outputs 5
/infinite-stateful specs/example_spec.md outputs 10 specs/urls.json
/infinite-stateful specs/example_spec.md outputs infinite specs/urls.json
/infinite-stateful specs/example_spec.md outputs infinite specs/urls.json run_20250310_143022
Features:
- Persistent state in
.claude/state/run_*.json - Automatic URL deduplication
- Self-consistency validation
- Graceful interruption handling
- Resume capability
/status
View run status and validate consistency.
# List all runs
/status
# Detailed status for specific run
/status run_20250310_143022
Output:
- Run information (spec, progress, timing)
- Self-consistency validation (6 checks + score)
- Recent iterations
- Resumability status
- URL usage summary
/resume
Resume interrupted run.
/resume <run_id>
Process:
- Validates state consistency
- Loads original parameters
- Continues from last completed iteration
- No duplicate URLs or iterations
/reset-state
State management utilities.
# Verify state integrity
/reset-state <run_id>
# Rebuild state from files
/reset-state <run_id> --rebuild
# Delete state (with backup)
/reset-state <run_id> --delete
Architecture & Structure
Command System
.claude/commands/:
infinite-stateful.md- Main orchestration with state managementresume.md- Resume interrupted runsstatus.md- View and validate run statusreset-state.md- State management utilities
.claude/settings.json:
- Permissions: Write, Edit, Read, Bash, Task, WebFetch, WebSearch
- Custom instructions for state management
State Management
.claude/state/:
run_*.json- Individual run state filesREADME.md- State system documentation
State File Structure:
{
"run_id": "run_YYYYMMDD_HHMMSS",
"spec_path": "specs/example_spec.md",
"output_dir": "outputs",
"total_count": 10,
"status": "in_progress",
"completed_iterations": 3,
"iterations": [...],
"used_urls": [...],
"validation": {
"consistency_score": 1.0,
"issues": []
}
}
Specification System
specs/:
example_spec.md- Example specification with state integration- Custom specs can be added
Spec Requirements:
- Output file naming pattern
- Content structure
- Quality standards
- Web learning integration
- Metadata embedding (required for state tracking)
Utility Files
state_manager.py- Python utilities for state operationsvalidators/check_state_consistency.sh- Bash validation scripttemplates/- State, URL tracker, and iteration templatesdocs/state_management_guide.md- Complete usage guide
State Management Principles
1. Single Source of Truth
State file is authoritative:
- File system is secondary
- State determines what exists
- Validation checks consistency
2. Self-Consistency Validation
6 independent checks with majority voting:
- Schema validation
- File count matching
- Iteration record consistency
- URL uniqueness
- File existence
- Timestamp validity
Consistency Score = (passed checks) / 6
- ≥0.8: State is reliable
- 0.5-0.79: Warnings
- <0.5: Corrupted
3. Deduplication Guarantee
- All URLs tracked in
used_urls - URL strategy filtered against used URLs
- Fallback to web search for new URLs
- Iteration numbers never collide
4. Resumability
- Any run can resume by
run_id - State tracks exact progress point
- Graceful handling of interruptions
- No re-generation of completed iterations
5. Atomicity
- State updates are atomic (temp + rename)
- Prevents corruption from partial writes
- Safe concurrent reads
- Single writer at a time
6. Auditability
- Complete iteration history
- Timestamped state changes
- URL tracking
- Validation results
Implementation Details
State Updates
After each batch completion:
- Load current state
- Add iteration records
- Update completion counters
- Add URLs to used list
- Update timestamp
- Save atomically (temp + rename)
Self-Consistency Validation
Between batches and at resume:
- Run 6 independent checks
- Each check returns pass/fail
- Compute consistency score
- If score < 0.8, pause and report
- User decides: continue, fix, or abort
URL Deduplication
For each iteration:
- Load URL strategy
- Filter out URLs in
state.used_urls - Select next available URL
- If exhausted, fallback to web search
- Add used URL to state
Resume Process
- Locate state file by
run_id - Validate state consistency (pre-check)
- Extract original parameters
- Determine next iteration number
- Continue generation
- Validate state consistency (post-check)
Rebuild Process
When state corrupted:
- Scan output directory
- Extract iteration numbers from filenames
- Read metadata from files (if embedded)
- Reconstruct iteration records
- Compute file hashes
- Deduplicate URLs
- Create new state file
- Validate rebuilt state
Usage Patterns
Pattern 1: Long-Running Generation
# Start
/infinite-stateful specs/viz.md outputs 100
# ... interrupted after 47 ...
# State auto-saved
# Resume
/resume run_20250310_143022
# Continues from 48
Pattern 2: Infinite Mode
# Start infinite
/infinite-stateful specs/viz.md outputs infinite specs/urls.json
# ... hits context limit ...
# State saved
# New session
/resume run_20250310_143022
# Continues indefinitely
Pattern 3: State Recovery
# Check
/status run_20250310_143022
# Score: 0.67 (WARNING)
# Rebuild
/reset-state run_20250310_143022 --rebuild
# Verify
/status run_20250310_143022
# Score: 1.00 (CONSISTENT)
Best Practices
For Claude Code Users
State Management:
- Let system manage state automatically
- Use
/statusto check consistency regularly - Resume interrupted runs rather than restart
- Trust self-consistency validation
Resumability:
- Use consistent spec and output_dir
- Preserve output directory between sessions
- Check state before resuming
- Let system determine next iteration
URL Deduplication:
- Use URL strategy files
- Let state track URLs automatically
- Trust deduplication system
- Provide fallback search terms
Validation:
- Run validation before critical operations
- Investigate low consistency scores
- Use multiple validation approaches
- Trust majority voting results
For Spec Authors
Required Metadata:
Specs MUST require embedded metadata in outputs:
<div id="metadata" style="display:none;">
{
"iteration": 1,
"web_source": "https://example.com/tutorial",
"techniques_learned": ["technique1", "technique2"],
"created": "2025-03-10T14:30:00Z"
}
</div>
This enables:
- State reconstruction from files
- URL deduplication
- Quality validation
- Learning tracking
For Command Authors
State-Aware Commands:
New commands should:
- Load state from
.claude/state/ - Validate consistency before operations
- Update state atomically
- Document state changes
Example:
from state_manager import StateManager
sm = StateManager()
state = sm.load_state(run_id)
# Validate
score, checks = sm.validate_consistency(state)
if score < 0.8:
# Handle low consistency
# Operate on state
# ...
# Save atomically
sm.save_state(state)
Troubleshooting
Common Issues
Low Consistency Score:
- Run
/status <run_id>for details - Use
/reset-state <run_id> --rebuildto fix - Investigate specific failed checks
Cannot Resume:
- Verify state file exists:
ls .claude/state/ - Use
/statusto list available runs - Check state consistency before resume
Duplicate URLs:
- Run
/status <run_id>to check uniqueness - Rebuild state:
/reset-state <run_id> --rebuild - Verify URL strategy has unique URLs
Missing Files:
- Files manually deleted after generation
- Rebuild state:
/reset-state <run_id> --rebuild - Or accept loss and continue
Validation Tools
Bash Validator:
./validators/check_state_consistency.sh run_20250310_143022
Python Utilities:
python state_manager.py list
python state_manager.py validate run_20250310_143022
python state_manager.py info run_20250310_143022
Web Learning Integration
This variant demonstrates learning from self-consistency prompting research:
Source Concept: Self-consistency improves AI reliability by:
- Generating multiple independent reasoning paths
- Analyzing consistency across paths
- Using majority voting to select consensus answer
Applied Here: State validation uses:
- Multiple independent validation checks (6 different approaches)
- Consistency analysis (pass/fail for each check)
- Majority voting (consistency score from passed/total)
Result: High-confidence state validation even if individual checks have edge cases or limitations.
Extension Points
Add Custom Commands
Create new commands in .claude/commands/:
- Analysis commands
- Export commands
- Reporting commands
- Integration commands
Add Custom Validation
Extend state_manager.py:
def validate_custom(state):
# Domain-specific checks
pass
Add State Metadata
Extend state schema:
def add_metadata(state, key, value):
if 'metadata' not in state:
state['metadata'] = {}
state['metadata'][key] = value
Integrate External Systems
Export state:
from state_manager import StateManager
sm = StateManager()
state = sm.load_state('run_20250310_143022')
# Export to external system
send_to_external(state)
Success Criteria
A successful stateful run:
- Completes Reliably: Handles interruptions gracefully
- Maintains Consistency: Validation score ≥0.8
- Deduplicates URLs: No duplicate web sources
- Resumes Correctly: Exact continuation from interrupt point
- Provides Audit Trail: Complete iteration history
- Recovers from Errors: Rebuild capability for corruption
Documentation
- README.md: Overview and quick start
- docs/state_management_guide.md: Complete usage guide
- .claude/state/README.md: State system reference
- This file (CLAUDE.md): Project instructions
Notes
- State files are JSON for human readability
- Atomic writes prevent corruption
- Multiple validation approaches ensure reliability
- Self-consistency principle proven in AI research
- System designed for production use cases
When working with this project, prioritize state integrity and trust the self-consistency validation system.