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

principle-separate-before-serializing-shared-state

它能做什么

为智能体提供一条处理可能同时写入同一文件、分支或状态的并发工作的规则。适合同时运行多个工作者或智能体的工程师。

适用场景

  • ✓当多个智能体或进程可能写入同一文件或状态对象的时候。
  • ✓当两个分支或工作者会推送到同一目标的时候。
  • ✓当你想为修复间歇性的竞争条件而添加锁的时候。

技能链接

https://github.com/cursor/plugins/blob/main/harness/pstack/claude-code/skills/principle-separate-before-serializing-shared-state/SKILL.md

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

使用方法

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

可直接粘贴的消息

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

可以这样问 AI

“三个智能体写入同一个共享状态文件,它不断被损坏。请修复这个设计。”

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

工作原理

智能体会先问工作者是否真的需要共享同一个对象。如果不需要,它会为每个工作者分配各自的文件或键,只在报告步骤中合并。只有在确实需要单一共享写入者时,它才会添加结构性的串行化,例如锁。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Separate Before Serializing Shared State

When concurrent actors might share mutable state, first ask whether they need the same mutable object. If not, eliminate the sharing. When sharing is real, enforce serialization structurally: lockfiles, sequential phases, exclusive ownership. Instructions and conventions are not concurrency control.

Why: Concurrent writes to shared state create race conditions that are intermittent, hard to reproduce, and expensive to debug.

Pattern:

  1. Identify shared mutable state (files both read and write, branches both push to, APIs both define and consume).
  2. Default: eliminate the shared write target. Ask: do these actors need one canonical object, or are they publishing independent facts? Give each actor its own owned file, key, branch, or state directory, and merge only at the read/reporting boundary. Two workers writing their own lastX field into one state.json is still shared mutation. indexer-state.json + metrics-state.json is not.
  3. Only when one shared write target is a real invariant, serialize access structurally (lockfiles, sequential phases, single-writer actor, or atomic compare-and-swap). Treat "we need a lock" as a design smell to check, not as the default answer.
常见问题

仅靠说明能防止冲突吗?

不能。该技能指出,说明和约定并不是并发控制。

加锁是默认的修复方法吗?

不是。该技能把需要加锁视为应先检查的设计上的坏味道。

什么是好的拆分方式?

让每个参与者拥有各自的状态文件,而不是让多个工作者写入同一个共享文件。

详情

作者的原始描述

Apply when concurrent actors might write to the same file, branch, key, or state object. Eliminate the sharing first; serialize structurally only when one shared writer is a real invariant.

repo: cursor/plugins

热度

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

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能