360 Secure SkillHub
360 安全技能中心

principle-make-operations-idempotent

这是一份纯设计原则说明,针对命令、生命周期步骤和处理循环,给出收敛启动、按内容清理、自愈锁与幂等调度等模式,并用三个问题检验重复执行与中途崩溃后的收敛性,不含代码或工具。

mdsmithaustin
热度 0.00
最近更新 2026-10-19 07:37:07
分类:开发依赖:纯提示词应用场景:DevOps / 代码审查

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add mdsmithaustin/pstack/principle-make-operations-idempotent
Metadata
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.

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.

The pattern:

  • Convergent startup: scan for existing state, clean stale artifacts, adopt live sessions
  • Content-based cleanup: compare by content equivalence, not creation order
  • Self-healing locks: use PID-based stale lock detection
  • Idempotent scheduling: failed work respawns cleanly, fresh input regenerated after each cycle

For generated reports, indexes, and calibration files, rebuild from attributed source records and replace the generated output. Preserve source facts, append-only history, and human annotations. Report missing inputs rather than inventing samples or carrying stale derived values forward.

The test:

  1. What happens if this runs twice in a row?
  2. What happens if the previous run crashed at every possible point?
  3. 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.