cursor-guard 2.1.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.zh-CN.md CHANGED
@@ -1,293 +1,345 @@
1
- # Cursor Guard
2
-
3
- [![npm version](https://img.shields.io/npm/v/cursor-guard)](https://www.npmjs.com/package/cursor-guard)
4
- [![license](https://img.shields.io/github/license/zhangqiang8vipp/cursor-guard)](LICENSE)
5
-
6
- 保护你的代码免受 [Cursor](https://cursor.com) AI 代理意外覆写或删除。
7
-
8
- **[English](README.md)**
9
-
10
- ---
11
-
12
- ## 功能介绍
13
-
14
- 当 Cursor 的 AI 代理编辑你的文件时,可能会意外覆盖、删除或丢失代码。**Cursor Guard** 强制执行一套安全协议:
15
-
16
- - **强制写前快照** — 在任何破坏性操作前自动 Git 提交或影子拷贝
17
- - **先读后写** — 代理必须先读取文件内容,才能覆写
18
- - **预览再执行** — 危险操作前展示 diff 预览并要求确认
19
- - **确定性恢复** — 按优先级的恢复路径(Git → 影子拷贝 → 对话上下文 → 编辑器历史)
20
- - **可配置保护范围** — 通过 `.cursor-guard.json` 配置文件只保护你关心的文件
21
- - **敏感文件过滤** — `.env`、密钥、证书等敏感文件自动排除备份
22
- - **自动备份脚本** — 跨平台 (Node.js) 定期快照到独立 Git 分支,不干扰工作区
23
-
24
- ---
25
-
26
- ## 安装
27
-
28
- ### 方式一:npm 安装
29
-
30
- ```bash
31
- npm install cursor-guard
32
- ```
33
-
34
- 安装后,将技能文件复制到 Cursor 技能目录:
35
-
36
- **Windows (PowerShell):**
37
-
38
- ```powershell
39
- # 全局安装(所有项目生效)
40
- Copy-Item -Recurse node_modules/cursor-guard "$env:USERPROFILE/.cursor/skills/cursor-guard"
41
-
42
- # 项目级安装(仅当前项目生效)
43
- Copy-Item -Recurse node_modules/cursor-guard .cursor/skills/cursor-guard
44
- ```
45
-
46
- **macOS / Linux:**
47
-
48
- ```bash
49
- # 全局安装
50
- cp -r node_modules/cursor-guard ~/.cursor/skills/cursor-guard
51
-
52
- # 项目级安装
53
- cp -r node_modules/cursor-guard .cursor/skills/cursor-guard
54
- ```
55
-
56
- 复制完成后,如果不需要保留在 `node_modules` 中,可以卸载:
57
-
58
- ```bash
59
- npm uninstall cursor-guard
60
- ```
61
-
62
- ### 方式二:Git 克隆
63
-
64
- ```bash
65
- # 全局安装
66
- git clone https://github.com/zhangqiang8vipp/cursor-guard.git ~/.cursor/skills/cursor-guard
67
-
68
- # 项目级安装
69
- git clone https://github.com/zhangqiang8vipp/cursor-guard.git .cursor/skills/cursor-guard
70
- ```
71
-
72
- ### 方式三:手动下载
73
-
74
- [GitHub Releases](https://github.com/zhangqiang8vipp/cursor-guard/releases) 下载并解压到:
75
-
76
- ```
77
- ~/.cursor/skills/cursor-guard/ # 全局
78
- <项目根目录>/.cursor/skills/cursor-guard/ # 项目级
79
- ```
80
-
81
- ### 验证安装
82
-
83
- 安装后目录结构应如下所示:
84
-
85
- ```
86
- .cursor/skills/cursor-guard/
87
- ├── SKILL.md # AI 代理指令
88
- ├── README.md
89
- ├── README.zh-CN.md
90
- ├── LICENSE
91
- ├── package.json
92
- └── references/
93
- ├── lib/
94
- ├── auto-backup.js # 备份核心 (Node.js)
95
- │ ├── guard-doctor.js # 健康检查核心
96
- │ └── utils.js # 共享工具库
97
- ├── bin/
98
- │ ├── cursor-guard-backup.js # CLI 入口:npx cursor-guard-backup
99
- └── cursor-guard-doctor.js # CLI 入口:npx cursor-guard-doctor
100
- ├── auto-backup.ps1 / .sh # 薄封装
101
- ├── guard-doctor.ps1 / .sh
102
- ├── recovery.md # 恢复命令模板
103
- ├── cursor-guard.example.json # 示例配置
104
- ├── cursor-guard.schema.json # 配置 Schema
105
- ├── config-reference.md # 配置说明(英文)
106
- └── config-reference.zh-CN.md # 配置说明(中文)
107
- ```
108
-
109
- 技能会在 AI 代理检测到高风险操作(文件编辑、删除、重命名)或你提到恢复相关词汇时自动激活。无需其他设置,安装即生效。
110
-
111
- ---
112
-
113
- ## 快速上手
114
-
115
- 1. **安装技能** — 用以上任意方式安装
116
-
117
- 2. **打开 Cursor** — 开始一个 Agent 对话
118
-
119
- 3. **技能自动生效** — 当 AI 代理尝试编辑文件时,会自动:
120
- - 写入前创建 Git 快照
121
- - 覆写前先读取文件
122
- - 危险操作前展示 diff 预览
123
- - 每次受保护操作后报告状态
124
-
125
- 4. **(可选)添加项目配置** — 自定义保护范围:
126
-
127
- ```bash
128
- cp .cursor/skills/cursor-guard/references/cursor-guard.example.json .cursor-guard.json
129
- ```
130
-
131
- 5. **(可选)运行自动备份** — 在独立终端运行:
132
-
133
- ```bash
134
- npx cursor-guard-backup --path /my/project
135
- ```
136
-
137
- ### 项目配置
138
-
139
- 编辑 `.cursor-guard.json` 定义保护哪些文件:
140
-
141
- ```json
142
- {
143
- "protect": ["src/**", "lib/**", "package.json"],
144
- "ignore": ["node_modules/**", "dist/**"],
145
- "auto_backup_interval_seconds": 60,
146
- "secrets_patterns": [".env", ".env.*", "*.key", "*.pem"],
147
- "pre_restore_backup": "always",
148
- "retention": { "mode": "days", "days": 30 }
149
- }
150
- ```
151
-
152
- #### `pre_restore_backup` — 恢复前保留行为控制
153
-
154
- | 值 | 行为 |
155
- |----|------|
156
- | `"always"`(默认) | 每次恢复前自动保留当前版本,无需确认。 |
157
- | `"ask"` | 每次恢复前询问你:"恢复前是否保留当前版本?(Y/n)"——由你逐次决定。 |
158
- | `"never"` | 恢复前不保留当前版本(不推荐)。 |
159
-
160
- 无论配置如何,你始终可以在单次请求中覆盖:
161
- - 说"不保留当前版本"可跳过保留(即使配置为 `"always"`)
162
- - 说"先保留当前版本"可强制保留(即使配置为 `"never"`)
163
-
164
- ---
165
-
166
- ## 自动备份脚本
167
-
168
- 在使用 Cursor 时,在**单独的终端窗口**中运行。跨平台——需要 Node.js >= 18。
169
-
170
- ```bash
171
- # 通过 npx(npm 安装后)
172
- npx cursor-guard-backup --path /my/project
173
- npx cursor-guard-backup --path /my/project --interval 30
174
-
175
- # Windows PowerShell
176
- .\references\auto-backup.ps1 -Path "D:\MyProject"
177
-
178
- # macOS / Linux
179
- ./references/auto-backup.sh /my/project
180
- ```
181
-
182
- 脚本使用 Git 底层命令快照到 `refs/guard/auto-backup`——不会切换分支,也不会影响你的工作索引。该引用位于 `refs/heads/` 之外,`git push --all` 不会推送它。支持 `shadow` 模式用于非 Git 目录。
183
-
184
- ### 健康检查
185
-
186
- ```bash
187
- npx cursor-guard-doctor --path /my/project
188
-
189
- # Windows: .\references\guard-doctor.ps1 -Path "D:\MyProject"
190
- # macOS/Linux: ./references/guard-doctor.sh /my/project
191
- ```
192
-
193
- > **注意**:请在独立终端窗口中运行备份/检查脚本,不要在 Cursor 集成终端中运行。
194
-
195
- ---
196
-
197
- ## 恢复
198
-
199
- 出问题时,直接用自然语言告诉 AI 代理即可。
200
-
201
- **默认行为**:执行任何恢复操作前,代理会自动保留你的当前版本,方便恢复后反悔。无需额外请求,这是默认行为。如需跳过,请明确说"不保留当前版本"或"直接覆盖恢复"。
202
-
203
- ### 按时间恢复
204
-
205
- > "帮我恢复到5分钟前"
206
- > "恢复到今天下午3点的状态"
207
- > "回到昨天的版本"
208
-
209
- ### 按版本恢复
210
-
211
- > "恢复到上一个版本"
212
- > "回到前3个版本"
213
- > "撤销最近两次修改"
214
-
215
- ### 指定文件恢复
216
-
217
- > "把 src/app.py 恢复到10分钟前"
218
- > "把 src/app.py 恢复到上一个版本"
219
-
220
- 代理会:
221
- 1. **先保留你的当前版本**(除非你明确选择跳过)
222
- 2. 搜索 Git 历史和自动备份快照
223
- 3. 列出匹配版本供你选择
224
- 4. 确认后执行恢复
225
- 5. 报告恢复前备份引用和恢复结果
226
-
227
- 如果保留当前版本失败,代理**不会**继续恢复——会等你明确确认后才会在没有安全网的情况下恢复。
228
-
229
- ### 恢复优先级
230
-
231
- 1. **Git** — `git restore`, `git reset`, `git reflog`
232
- 2. **自动备份引用** — `refs/guard/auto-backup`
233
- 3. **影子拷贝** — `.cursor-guard-backup/<时间戳>/`
234
- 4. **对话上下文** — 代理 Read 调用捕获的原始文件内容
235
- 5. **编辑器历史** VS Code/Cursor Timeline(辅助)
236
-
237
- 详细恢复命令见 [references/recovery.md](references/recovery.md)。
238
-
239
- ---
240
-
241
- ## 触发关键词
242
-
243
- 技能在以下信号时激活:
244
-
245
- - AI 代理的文件编辑、删除、重命名
246
- - 恢复请求:"回滚"、"误删"、"丢版本"、"改不回来"
247
- - 按时间恢复:"恢复到N分钟前""恢复到下午3点"、"回到昨天"
248
- - 按版本恢复:"恢复到上一个版本"、"前N个版本"、"撤销最近N次修改"
249
- - 历史问题:Checkpoint 丢失、Timeline 不工作、保存失败
250
-
251
- ---
252
-
253
- ## 文件说明
254
-
255
- | 文件 | 用途 |
256
- |------|------|
257
- | `SKILL.md` | AI 代理的主要技能指令 |
258
- | `references/lib/auto-backup.js` | 自动备份核心逻辑 (Node.js) |
259
- | `references/lib/guard-doctor.js` | 健康检查核心逻辑 (Node.js) |
260
- | `references/lib/utils.js` | 共享工具库(配置、glob、git、manifest) |
261
- | `references/bin/cursor-guard-backup.js` | CLI 入口:`npx cursor-guard-backup` |
262
- | `references/bin/cursor-guard-doctor.js` | CLI 入口:`npx cursor-guard-doctor` |
263
- | `references/auto-backup.ps1` / `.sh` | 薄封装(Windows / macOS+Linux) |
264
- | `references/guard-doctor.ps1` / `.sh` | 薄封装(Windows / macOS+Linux) |
265
- | `references/recovery.md` | 恢复命令模板 |
266
- | `references/cursor-guard.example.json` | 示例项目配置 |
267
- | `references/cursor-guard.schema.json` | 配置文件的 JSON Schema |
268
- | `references/config-reference.md` | 配置字段说明(英文) |
269
- | `references/config-reference.zh-CN.md` | 配置字段说明(中文) |
270
-
271
- ---
272
-
273
- ## 已知限制
274
-
275
- - **二进制文件**:Git 快照可以存储二进制文件(图片、编译产物),但无法进行有意义的 diff 或部分恢复。
276
- - **未跟踪文件**:从未提交到 Git 的文件无法从 Git 历史恢复。影子拷贝(`backup_strategy: "shadow"` 或 `"both"`)是未跟踪文件的唯一安全网。
277
- - **并发 Agent**:如果多个 AI 代理线程同时写入同一文件,快照无法防止竞态条件。请避免并行编辑同一文件。
278
- - **外部工具修改索引**:在自动备份运行期间,其他修改 Git 索引的工具(如 Git GUI、IDE Git 集成)可能冲突。脚本使用临时索引来最小化风险,但边缘情况仍存在。
279
- - **Git worktree**:自动备份脚本支持 worktree 布局(`git rev-parse --git-dir`),但未在所有特殊配置下测试(如 `--separate-git-dir`)。
280
- - **Cursor 终端干扰**:Cursor 集成终端会向 `git commit` 命令注入 `--trailer` 标志,导致 `commit-tree` 等底层命令异常。请始终在**独立的终端窗口**中运行自动备份脚本。
281
- - **大型仓库**:对于非常大的仓库,备份循环中的 `git add -A` 可能较慢。使用 `.cursor-guard.json` 中的 `protect` 模式缩小范围。
282
-
283
- ## 环境要求
284
-
285
- - **Node.js >= 18** — 备份与健康检查脚本的核心运行时
286
- - **Git** — 主要备份策略(仅影子拷贝模式不需要)
287
- - **Cursor IDE** — 需启用 Agent 模式
288
-
289
- ---
290
-
291
- ## 许可证
292
-
293
- MIT
1
+ # Cursor Guard
2
+
3
+ [![npm version](https://img.shields.io/npm/v/cursor-guard)](https://www.npmjs.com/package/cursor-guard)
4
+ [![license](https://img.shields.io/github/license/zhangqiang8vipp/cursor-guard)](LICENSE)
5
+
6
+ 保护你的代码免受 [Cursor](https://cursor.com) AI 代理意外覆写或删除。
7
+
8
+ **[English](README.md)**
9
+
10
+ ---
11
+
12
+ ## 功能介绍
13
+
14
+ 当 Cursor 的 AI 代理编辑你的文件时,可能会意外覆盖、删除或丢失代码。**Cursor Guard** 强制执行一套安全协议:
15
+
16
+ - **强制写前快照** — 在任何破坏性操作前自动 Git 提交或影子拷贝
17
+ - **先读后写** — 代理必须先读取文件内容,才能覆写
18
+ - **预览再执行** — 危险操作前展示 diff 预览并要求确认
19
+ - **确定性恢复** — 按优先级的恢复路径(Git → 影子拷贝 → 对话上下文 → 编辑器历史)
20
+ - **可配置保护范围** — 通过 `.cursor-guard.json` 配置文件只保护你关心的文件
21
+ - **敏感文件过滤** — `.env`、密钥、证书等敏感文件自动排除备份
22
+ - **自动备份脚本** — 跨平台 (Node.js) 定期快照到独立 Git 分支,不干扰工作区
23
+ - **MCP 工具调用(可选)** — 7 个标准化工具(诊断、快照、恢复、状态等),结构化 JSON 返回,低 token 消耗
24
+ - **自动诊断修复** — `doctor_fix` 一键修补缺失配置、未初始化 Git、gitignore 遗漏等常见问题
25
+
26
+ ---
27
+
28
+ ## 安装
29
+
30
+ ### 方式一:npm 安装
31
+
32
+ ```bash
33
+ npm install cursor-guard
34
+ ```
35
+
36
+ 安装后,将技能文件复制到 Cursor 技能目录:
37
+
38
+ **Windows (PowerShell):**
39
+
40
+ ```powershell
41
+ # 全局安装(所有项目生效)
42
+ Copy-Item -Recurse node_modules/cursor-guard "$env:USERPROFILE/.cursor/skills/cursor-guard"
43
+
44
+ # 项目级安装(仅当前项目生效)
45
+ Copy-Item -Recurse node_modules/cursor-guard .cursor/skills/cursor-guard
46
+ ```
47
+
48
+ **macOS / Linux:**
49
+
50
+ ```bash
51
+ # 全局安装
52
+ cp -r node_modules/cursor-guard ~/.cursor/skills/cursor-guard
53
+
54
+ # 项目级安装
55
+ cp -r node_modules/cursor-guard .cursor/skills/cursor-guard
56
+ ```
57
+
58
+ 复制完成后,如果不需要保留在 `node_modules` 中,可以卸载:
59
+
60
+ ```bash
61
+ npm uninstall cursor-guard
62
+ ```
63
+
64
+ ### 方式二:Git 克隆
65
+
66
+ ```bash
67
+ # 全局安装
68
+ git clone https://github.com/zhangqiang8vipp/cursor-guard.git ~/.cursor/skills/cursor-guard
69
+
70
+ # 项目级安装
71
+ git clone https://github.com/zhangqiang8vipp/cursor-guard.git .cursor/skills/cursor-guard
72
+ ```
73
+
74
+ ### 方式三:手动下载
75
+
76
+ 从 [GitHub Releases](https://github.com/zhangqiang8vipp/cursor-guard/releases) 下载并解压到:
77
+
78
+ ```
79
+ ~/.cursor/skills/cursor-guard/ # 全局
80
+ <项目根目录>/.cursor/skills/cursor-guard/ # 项目级
81
+ ```
82
+
83
+ ### 验证安装
84
+
85
+ 安装后目录结构应如下所示:
86
+
87
+ ```
88
+ .cursor/skills/cursor-guard/
89
+ ├── SKILL.md # AI 代理指令(含 MCP 双路径逻辑)
90
+ ├── ROADMAP.md # 版本演进规划书
91
+ ├── README.md
92
+ ├── README.zh-CN.md
93
+ ├── LICENSE
94
+ ├── package.json
95
+ └── references/
96
+ ├── lib/
97
+ ├── auto-backup.js # 备份 watcher(调用 Core)
98
+ │ ├── guard-doctor.js # 健康检查 CLI(调用 Core)
99
+ ├── utils.js # 共享工具库
100
+ │ └── core/ # V3 Core 层(纯逻辑)
101
+ ├── doctor.js # 诊断检查(含 MCP 自检)
102
+ ├── doctor-fix.js # 自动修复常见问题
103
+ ├── snapshot.js # Git 快照 + 影子拷贝
104
+ ├── backups.js # 备份列表 + 留存清理
105
+ ├── restore.js # 单文件/全项目恢复
106
+ └── status.js # 备份系统状态
107
+ ├── mcp/
108
+ │ └── server.js # MCP Server(7 个工具)
109
+ ├── bin/
110
+ │ ├── cursor-guard-backup.js # CLI:npx cursor-guard-backup
111
+ │ ├── cursor-guard-doctor.js # CLI:npx cursor-guard-doctor
112
+ │ └── cursor-guard-mcp (server.js)# CLI:npx cursor-guard-mcp
113
+ ├── auto-backup.ps1 / .sh # 薄封装
114
+ ├── guard-doctor.ps1 / .sh
115
+ ├── recovery.md # 恢复命令模板
116
+ ├── cursor-guard.example.json # 示例配置
117
+ ├── cursor-guard.schema.json # 配置 Schema
118
+ ├── config-reference.md # 配置说明(英文)
119
+ └── config-reference.zh-CN.md # 配置说明(中文)
120
+ ```
121
+
122
+ 技能会在 AI 代理检测到高风险操作(文件编辑、删除、重命名)或你提到恢复相关词汇时自动激活。无需其他设置,安装即生效。
123
+
124
+ ---
125
+
126
+ ## 快速上手
127
+
128
+ 1. **安装技能** — 用以上任意方式安装
129
+
130
+ 2. **打开 Cursor** — 开始一个 Agent 对话
131
+
132
+ 3. **技能自动生效** — 当 AI 代理尝试编辑文件时,会自动:
133
+ - 写入前创建 Git 快照
134
+ - 覆写前先读取文件
135
+ - 危险操作前展示 diff 预览
136
+ - 每次受保护操作后报告状态
137
+
138
+ 4. **(可选)添加项目配置** — 自定义保护范围:
139
+
140
+ ```bash
141
+ cp .cursor/skills/cursor-guard/references/cursor-guard.example.json .cursor-guard.json
142
+ ```
143
+
144
+ 5. **(可选)启用 MCP 工具调用** — 在 `.cursor/mcp.json` 中添加:
145
+
146
+ ```jsonc
147
+ {
148
+ "mcpServers": {
149
+ "cursor-guard": {
150
+ "command": "node",
151
+ "args": ["<skill-path>/references/mcp/server.js"]
152
+ }
153
+ }
154
+ }
155
+ ```
156
+
157
+ 启用后 AI 代理可直接调用 7 个结构化工具(诊断、快照、恢复等),无需拼接 shell 命令,更快更省 token。不启用也完全不影响使用。
158
+
159
+ 6. **(可选)运行自动备份** — 在独立终端运行:
160
+
161
+ ```bash
162
+ npx cursor-guard-backup --path /my/project
163
+ ```
164
+
165
+ ### 项目配置
166
+
167
+ 编辑 `.cursor-guard.json` 定义保护哪些文件:
168
+
169
+ ```json
170
+ {
171
+ "protect": ["src/**", "lib/**", "package.json"],
172
+ "ignore": ["node_modules/**", "dist/**"],
173
+ "auto_backup_interval_seconds": 60,
174
+ "secrets_patterns": [".env", ".env.*", "*.key", "*.pem"],
175
+ "pre_restore_backup": "always",
176
+ "retention": { "mode": "days", "days": 30 }
177
+ }
178
+ ```
179
+
180
+ #### `pre_restore_backup` — 恢复前保留行为控制
181
+
182
+ | | 行为 |
183
+ |----|------|
184
+ | `"always"`(默认) | 每次恢复前自动保留当前版本,无需确认。 |
185
+ | `"ask"` | 每次恢复前询问你:"恢复前是否保留当前版本?(Y/n)"——由你逐次决定。 |
186
+ | `"never"` | 恢复前不保留当前版本(不推荐)。 |
187
+
188
+ 无论配置如何,你始终可以在单次请求中覆盖:
189
+ - 说"不保留当前版本"可跳过保留(即使配置为 `"always"`)
190
+ - 说"先保留当前版本"可强制保留(即使配置为 `"never"`)
191
+
192
+ ---
193
+
194
+ ## 自动备份脚本
195
+
196
+ 在使用 Cursor 时,在**单独的终端窗口**中运行。跨平台——需要 Node.js >= 18。
197
+
198
+ 这点很重要:
199
+
200
+ - 命令可以在任何目录执行
201
+ - 但 `--path` 必须指向你要保护的项目根目录
202
+ - 如果你当前已经在项目根目录,可以写 `--path .`
203
+ - 如果你当前不在项目根目录,就不要写 `--path .`,要写完整路径
204
+
205
+ ```bash
206
+ # 如果当前就在项目根目录
207
+ npx cursor-guard-backup --path .
208
+
209
+ # 如果当前不在项目根目录
210
+ npx cursor-guard-backup --path /my/project
211
+ npx cursor-guard-backup --path /my/project --interval 30
212
+
213
+ # Windows PowerShell
214
+ .\references\auto-backup.ps1 -Path "D:\MyProject"
215
+
216
+ # macOS / Linux
217
+ ./references/auto-backup.sh /my/project
218
+ ```
219
+
220
+ 错误示例:
221
+
222
+ ```bash
223
+ # 你当前在别的目录
224
+ # 这时 --path . 保护的是当前目录,不是你真正想保护的项目
225
+ npx cursor-guard-backup --path .
226
+ ```
227
+
228
+ 脚本使用 Git 底层命令快照到 `refs/guard/auto-backup`——不会切换分支,也不会影响你的工作索引。该引用位于 `refs/heads/` 之外,`git push --all` 不会推送它。支持 `shadow` 模式用于非 Git 目录。
229
+
230
+ ### 健康检查
231
+
232
+ ```bash
233
+ npx cursor-guard-doctor --path /my/project
234
+
235
+ # Windows: .\references\guard-doctor.ps1 -Path "D:\MyProject"
236
+ # macOS/Linux: ./references/guard-doctor.sh /my/project
237
+ ```
238
+
239
+ > **注意**:请在独立终端窗口中运行备份/检查脚本,不要在 Cursor 集成终端中运行。
240
+
241
+ ---
242
+
243
+ ## 恢复
244
+
245
+ 出问题时,直接用自然语言告诉 AI 代理即可。
246
+
247
+ **默认行为**:执行任何恢复操作前,代理会自动保留你的当前版本,方便恢复后反悔。无需额外请求,这是默认行为。如需跳过,请明确说"不保留当前版本""直接覆盖恢复"
248
+
249
+ ### 按时间恢复
250
+
251
+ > "帮我恢复到5分钟前"
252
+ > "恢复到今天下午3点的状态"
253
+ > "回到昨天的版本"
254
+
255
+ ### 按版本恢复
256
+
257
+ > "恢复到上一个版本"
258
+ > "回到前3个版本"
259
+ > "撤销最近两次修改"
260
+
261
+ ### 指定文件恢复
262
+
263
+ > "把 src/app.py 恢复到10分钟前"
264
+ > "把 src/app.py 恢复到上一个版本"
265
+
266
+ 代理会:
267
+ 1. **先保留你的当前版本**(除非你明确选择跳过)
268
+ 2. 搜索 Git 历史和自动备份快照
269
+ 3. 列出匹配版本供你选择
270
+ 4. 确认后执行恢复
271
+ 5. 报告恢复前备份引用和恢复结果
272
+
273
+ 如果保留当前版本失败,代理**不会**继续恢复——会等你明确确认后才会在没有安全网的情况下恢复。
274
+
275
+ ### 恢复优先级
276
+
277
+ 1. **Git** `git restore`, `git reset`, `git reflog`
278
+ 2. **自动备份引用** `refs/guard/auto-backup`
279
+ 3. **影子拷贝** `.cursor-guard-backup/<时间戳>/`
280
+ 4. **对话上下文** 代理 Read 调用捕获的原始文件内容
281
+ 5. **编辑器历史** VS Code/Cursor Timeline(辅助)
282
+
283
+ 详细恢复命令见 [references/recovery.md](references/recovery.md)。
284
+
285
+ ---
286
+
287
+ ## 触发关键词
288
+
289
+ 技能在以下信号时激活:
290
+
291
+ - AI 代理的文件编辑、删除、重命名
292
+ - 恢复请求:"回滚"、"误删"、"丢版本"、"改不回来"
293
+ - 按时间恢复:"恢复到N分钟前"、"恢复到下午3点"、"回到昨天"
294
+ - 按版本恢复:"恢复到上一个版本"、"前N个版本"、"撤销最近N次修改"
295
+ - 历史问题:Checkpoint 丢失、Timeline 不工作、保存失败
296
+ - 健康检查:"guard doctor"、"自检"、"诊断guard"、"MCP 能用吗"
297
+ - 自动修复:"guard fix"、"修复配置"、"自动修复"
298
+ - 备份状态:"备份状态"、"guard status"、"watcher 在跑吗"
299
+
300
+ ---
301
+
302
+ ## 文件说明
303
+
304
+ | 文件 | 用途 |
305
+ |------|------|
306
+ | `SKILL.md` | AI 代理的主要技能指令(含 MCP 双路径逻辑) |
307
+ | `ROADMAP.md` | 版本演进规划书(V2-V7) |
308
+ | `references/lib/core/` | Core 层:6 个纯逻辑模块(doctor / doctor-fix / snapshot / backups / restore / status) |
309
+ | `references/mcp/server.js` | MCP Server:7 个标准化工具(可选) |
310
+ | `references/lib/auto-backup.js` | 自动备份 watcher(调用 Core) |
311
+ | `references/lib/guard-doctor.js` | 健康检查 CLI 壳(调用 Core) |
312
+ | `references/lib/utils.js` | 共享工具库(配置、glob、git、manifest) |
313
+ | `references/bin/cursor-guard-backup.js` | CLI 入口:`npx cursor-guard-backup` |
314
+ | `references/bin/cursor-guard-doctor.js` | CLI 入口:`npx cursor-guard-doctor` |
315
+ | `references/auto-backup.ps1` / `.sh` | 薄封装(Windows / macOS+Linux) |
316
+ | `references/guard-doctor.ps1` / `.sh` | 薄封装(Windows / macOS+Linux) |
317
+ | `references/recovery.md` | 恢复命令模板 |
318
+ | `references/cursor-guard.example.json` | 示例项目配置 |
319
+ | `references/cursor-guard.schema.json` | 配置文件的 JSON Schema |
320
+ | `references/config-reference.md` | 配置字段说明(英文) |
321
+ | `references/config-reference.zh-CN.md` | 配置字段说明(中文) |
322
+
323
+ ---
324
+
325
+ ## 已知限制
326
+
327
+ - **二进制文件**:Git 快照可以存储二进制文件(图片、编译产物),但无法进行有意义的 diff 或部分恢复。
328
+ - **未跟踪文件**:从未提交到 Git 的文件无法从 Git 历史恢复。影子拷贝(`backup_strategy: "shadow"` 或 `"both"`)是未跟踪文件的唯一安全网。
329
+ - **并发 Agent**:如果多个 AI 代理线程同时写入同一文件,快照无法防止竞态条件。请避免并行编辑同一文件。
330
+ - **外部工具修改索引**:在自动备份运行期间,其他修改 Git 索引的工具(如 Git GUI、IDE Git 集成)可能冲突。脚本使用临时索引来最小化风险,但边缘情况仍存在。
331
+ - **Git worktree**:自动备份脚本支持 worktree 布局(`git rev-parse --git-dir`),但未在所有特殊配置下测试(如 `--separate-git-dir`)。
332
+ - **Cursor 终端干扰**:Cursor 集成终端会向 `git commit` 命令注入 `--trailer` 标志,导致 `commit-tree` 等底层命令异常。请始终在**独立的终端窗口**中运行自动备份脚本。
333
+ - **大型仓库**:对于非常大的仓库,备份循环中的 `git add -A` 可能较慢。使用 `.cursor-guard.json` 中的 `protect` 模式缩小范围。
334
+
335
+ ## 环境要求
336
+
337
+ - **Node.js >= 18** — 备份与健康检查脚本的核心运行时
338
+ - **Git** — 主要备份策略(仅影子拷贝模式不需要)
339
+ - **Cursor IDE** — 需启用 Agent 模式
340
+
341
+ ---
342
+
343
+ ## 许可证
344
+
345
+ MIT