# 政策檔復原 / Policy recovery

先在管理頁確認顯示的儲存路徑，以及目前操作的是哪個 DSH profile。不要刪除 skill 文件、整個 DSH home 或其他 profile 的資料來排除本 plugin 的錯誤。

## 有有效備份

停止目標 profile 的 DSH process，先將損壞 policy 另存一份不覆寫既有檔案的備份，再還原已知有效的 policy。重新啟動後，確認管理頁是 Ready，並核對兩個載入入口的有效政策。執行中的 Last good 只表示記憶體仍保有最後有效政策，不等於磁碟上一定存在備份。

## 沒有有效備份：明確重設

這會清除全域與所有 workspace 覆寫，回到來源原本規則；先前停用的 skills 可能再次可用。**這是人工明確重設，不是 plugin 自動修復。**

停止目標 profile，保留原損壞檔案與必要診斷資訊後，可在管理頁指出的 policy 路徑建立以下新內容：

```json
{
  "version": 1,
  "global": {},
  "workspaces": {}
}
```

重新啟動目標 profile，確認管理頁 Ready，再重新套用你需要的限制。這不會刪除已載入的歷史指令；需要乾淨上下文時另開新對話。不要從不可信來源取得所謂的修復設定。

## Lock 衝突

版本衝突表示 policy revision 已改變；重新整理後檢查最新狀態再套用。鎖定錯誤表示 policy 檔路徑後加上 `.lock` 的檔案已存在，但這不能證明它是殘留鎖：另一個管理頁或 host 可能仍在寫入。請稍候再試。若鎖定檔持續存在，停止目標 DSH host，確認沒有其他 writer，然後只移除錯誤訊息顯示的那個 lock。儲存空間錯誤請檢查 policy 路徑、目錄權限及可用空間。不要刪 policy 檔或使用廣泛清理命令。

手動編輯 policy 前必須先停止 DSH host。外部 writer 若遵守相同的 exclusive lock 協定，可與 plugin 協作；忽略 lock 的 writer 仍可能在最後一次 revision 讀取與 atomic rename 之間競爭。若 plugin rename 覆蓋該外部編輯，事後讀回無法還原已遺失的內容。

---

Before recovery, verify the storage path and target profile. Preserve invalid input, stop the intended host, and restore a known-valid policy. Last-good memory is not a disk backup.

If no backup exists, the JSON above is an **explicit reset**: it removes all global/workspace overrides and restores source defaults, potentially re-enabling skills. Preserve the corrupt document first, then deliberately replace it while the target host is stopped. Restart, verify Ready and reapply restrictions. This does not erase loaded history.

For a stale revision, refresh and review the latest state before applying again. A lock error means a file named by appending `.lock` to the policy path exists; it does not prove that the lock is stale. Wait and retry. If it remains, stop the target DSH host, confirm that no writer is active, then remove only the lock path shown in the error. For storage errors, check the policy path, directory permissions and available space. Never delete the policy, skills or an entire DSH home as a shortcut.

Stop DSH before manually editing the policy. An external writer can cooperate by following the same exclusive-lock protocol. A writer that ignores the lock can still race between the final revision read and atomic rename. If the plugin rename overwrites that external edit, reading the file afterward cannot recover the lost contents.
