Principle Separate Before Serializing Shared State

作者 cursorccb5507cec15无许可证10K 个星标收录于 2026年10月8日更新于 2026年10月8日仓库今天更新

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.

AI 生成的概览

指导智能体在采用结构化串行化之前,先消除并发参与者之间的共享可变状态。

功能
这是一个仅含说明的技能,提供处理并发写入共享状态的设计模式。它引导识别共享可变状态,默认做法是消除共享写入目标,让每个参与者拥有自己的文件、键、分支或状态目录,只在读取或报告边界进行合并。它建议仅当单一共享写入目标是真实不变量时,才通过锁文件、顺序阶段、单一写入者或原子比较并交换等方式在结构上串行化访问。
适用场景
当并发参与者可能写入同一文件、分支、键或状态对象时使用。它适用于正在考虑加锁或串行化方案的设计或评审场景,以便先检查能否移除共享。
运行要求
不需要任何工具、软件包、运行时、凭据或网络访问。它不附带脚本,仅为说明性内容。

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.

来源与署名

来源:cursor/plugins位于pstack/skills/principle-separate-before-serializing-shared-state提交ccb5507

许可证: 无许可证

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架