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 從公開儲存庫中收錄這些內容。

檢舉或申請下架