Use Cases
- Track last processed item — Remember the last tag, commit, or issue you handled
- Store counters and metrics — Keep running totals across automation runs
- Avoid duplicate notifications — Don’t re-post about the same PR twice
- Resume from failures — Pick up where you left off if something goes wrong
- Cache expensive computations — Store results that don’t change often
Basic Example
Track the last processed git tag for release notes:lastProcessedTag is null, so all tags are processed. On subsequent runs, only new tags are handled.
Common Patterns
Tracking Processed Items
Store IDs of items you’ve already handled:Timestamp-Based Tracking
Use timestamps instead of IDs for time-based queries:Counters and Metrics
Track statistics across runs:Deduplication with TTL
Prevent duplicate notifications with expiring state:Best Practices
Initialize with Defaults
Always handle the case where state doesn’t exist yet:Document Your State Variables
Add comments explaining what each variable means:Clean Up Old State
Prevent state from growing unbounded:Handle State Corruption
Be defensive about invalid state:Atomic Updates
Update state only after successful completion:Debugging State Issues
Log State Changes
Include state in your logs for debugging:Add a Reset Mechanism
Allow manual state reset for recovery:Related
- Error Handling — Handle failures gracefully
- Performance Tips — Optimize automation runs
- Product Changelog — Example using state for release tracking