跳到正文
Skillpedia
API 与服务器(后端)15k · #10979

principle-make-operations-idempotent

它能做什么

为智能体提供一条规则,用于构建崩溃后可以安全重试的操作。适合编写命令、任务或处理循环的工程师。

适用场景

  • ✓当你设计的命令或任务可能在失败后再次运行的时候。
  • ✓当重启、重试或崩溃可能留下部分状态的时候。
  • ✓当你需要一个能够安全恢复的处理循环的时候。

技能链接

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-make-operations-idempotent/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-make-operations-idempotent/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请让我们的数据导入任务在崩溃后可以安全地重新运行,且不会重复创建记录。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会根据当前状态推导每一步,只应用缺失的差异。它会让锁或临时文件等遗留标记能被识别为陈旧的,然后检查任务在每个节点运行两次或中途崩溃时会发生什么。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Make Operations Idempotent

Design operations so they converge to the correct state regardless of how many times they run or where the previous run stopped. 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 idea: derive each step from the state that exists, not from what the previous run meant to do. Read the current state, compare it to the goal, and apply only the difference. A step written this way is safe to repeat, because a repeat finds nothing left to do.

Anything a run leaves behind, such as a lock, a temporary file, a half-written record, or a claimed job, must be recognizable as stale by the next run. Judge staleness by evidence that stays true wherever the previous run could have stopped, not only in the common case. A marker that the next run cannot judge turns every crash into manual cleanup. For example, write to a temporary path and rename it into place, or compare artifacts by content instead of by creation order.

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.

常见问题

关键的测试是什么?

要问操作连续运行两次会发生什么,以及上一次运行在每个可能的节点崩溃时会发生什么。

遗留的锁如何处理?

必须依据在上一次运行可能停止的任何位置都依然成立的证据,来判定它已经陈旧。

写入临时文件有帮助吗?

有。先写入临时路径,再重命名到目标位置,是避免写入一半结果的一种方法。

详情

作者的原始描述

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.

repo: cursor/plugins

热度

GitHub 星标
15k
排名
#10979
许可证
—
更新时间
2026/10/31

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能