Skill v1.0.1
currentAutomated scan100/100+1 new
name: principle-make-operations-idempotent description: "Apply when designing commands, lifecycle steps, or processing loops that run amid crashes, restarts, and retries. Converge to the same end state regardless of partial prior runs." disable-model-invocation: true metadata: version: "1.0.1"
Make Operations Idempotent
Design operations so they converge to the correct state regardless of how many times they run or where they start from. Every state-mutating operation should answer: "What happens if this runs twice? What happens if the previous run crashed halfway?"
Why: Commands, lifecycle operations, and processing loops run where crashes, restarts, and retries are normal. If partial state changes the next run's outcome, every restart becomes a debugging session.
Derive State Deltas
Compute state deltas instead of executing imperative mutation sequences.
- Inspect actual and desired state. Given the current actual state and the target desired state, compute
delta = desired - actual. - Apply only the delta. Execute mutations only for the missing or diverged elements. Do not re-run actions for state that already matches the target.
- Safe no-ops on converged state. Re-running the operation on an already-converged state results in
delta = empty. The operation exits immediately with zero side effects. - Crash recovery by design. An operation that crashes midway leaves a partially converged state. On retry, the reconciliation step computes the remaining delta and finishes the transition without duplicating work.
Patterns
- Convergent startup. Scan for existing state, clean stale artifacts, and adopt live sessions.
- Content-based cleanup. Compare by content equivalence rather than creation order or timestamps.
- Self-healing locks. Use PID-based or lease-based stale lock detection.
- Idempotent scheduling. Ensure failed work respawns cleanly and fresh input is regenerated after each cycle.
The Test
- What happens if this operation runs twice in a row?
- What happens if the previous run crashed at every possible point?
- Does re-execution converge to the same end state?
If any answer is "it depends on what state was left behind," the operation needs a reconciliation step.