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。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 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:
- Identify shared mutable state (files both read and write, branches both push to, APIs both define and consume).
- 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
lastXfield into onestate.jsonis still shared mutation.indexer-state.json+metrics-state.jsonis not. - 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
评论
- …
同一仓库的更多技能
advisor
15k如果你开启该功能,让你的智能体在关键节点咨询一个更强的第二模型,例如在重大决定之前,或在错误上卡住时。
check-agent-compatibility
15k全面检查 AI 编程智能体能否启动、验证和记录某个代码仓库,并返回一个兼容性评分以及主要问题。
cli-for-agents
15k教你的智能体设计或审查命令行工具,使 AI 智能体无需提示即可运行,并提供清晰的帮助文字和示例。
continual-learning
15k通过把回顾过往对话的工作交给专门的记忆更新程序,使你的 AGENTS.md 笔记保持最新。