@uncarved/jade 0.3.0 → 0.4.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.md +1 -0
- package/dist/api.d.ts +1 -1
- package/dist/api.js +1 -1
- package/dist/host.d.ts +2 -0
- package/dist/host.js +39 -5
- package/dist/host.js.map +1 -1
- package/dist/remote.js +5 -0
- package/dist/remote.js.map +1 -1
- package/dist/session.d.ts +7 -0
- package/dist/session.js +482 -27
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +49 -24
- package/plugins/approval/policy.ts +48 -25
- package/plugins/approval/review.ts +9 -5
- package/plugins/checks/checks.ts +10 -7
- package/plugins/checks/index.ts +13 -13
- package/plugins/checks/plugin.json +1 -1
- package/plugins/checks/project.ts +37 -38
- package/plugins/commands/index.ts +27 -3
- package/plugins/compact/compact.ts +13 -5
- package/plugins/compact/index.ts +20 -3
- package/plugins/compact/style.ts +13 -14
- package/plugins/completions/index.ts +35 -0
- package/plugins/completions/plugin.json +7 -0
- package/plugins/copy/index.ts +46 -0
- package/plugins/copy/plugin.json +7 -0
- package/plugins/editor/index.ts +10 -4
- package/plugins/fs/diff.ts +7 -6
- package/plugins/fs/fs.ts +8 -4
- package/plugins/fs/grep.ts +55 -12
- package/plugins/fs/index.ts +2 -7
- package/plugins/fs/plugin.json +1 -1
- package/plugins/fs/renderers.ts +16 -15
- package/plugins/fs/style.ts +16 -15
- package/plugins/hooks/index.ts +3 -2
- package/plugins/keys/index.ts +5 -1
- package/plugins/mcp/index.ts +197 -78
- package/plugins/mcp/install.ts +5 -3
- package/plugins/mcp/mcp.ts +163 -11
- package/plugins/mcp/plugin.json +2 -1
- package/plugins/mcp/project.ts +47 -50
- package/plugins/memory/index.ts +4 -4
- package/plugins/memory/style.ts +13 -14
- package/plugins/mentions/candidates.ts +67 -0
- package/plugins/mentions/index.ts +12 -5
- package/plugins/mentions/plugin.json +1 -1
- package/plugins/mentions/style.ts +13 -14
- package/plugins/models/configure.ts +10 -9
- package/plugins/models/index.ts +31 -4
- package/plugins/models/provider.ts +12 -1
- package/plugins/modes/index.ts +13 -2
- package/plugins/print/index.ts +7 -4
- package/plugins/prompts/index.ts +0 -0
- package/plugins/renderers/index.ts +10 -5
- package/plugins/repeats/index.ts +39 -9
- package/plugins/schedule/index.ts +2 -0
- package/plugins/screenshot/browser.ts +231 -0
- package/plugins/screenshot/index.ts +3 -2
- package/plugins/screenshot/tool.ts +27 -51
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +75 -22
- package/plugins/shell/plugin.json +1 -2
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +93 -34
- package/plugins/skills/index.ts +28 -19
- package/plugins/skills/project.ts +12 -7
- package/plugins/steer/index.ts +5 -4
- package/plugins/task/index.ts +81 -81
- package/plugins/task/plugin.json +5 -2
- package/plugins/task/style.ts +13 -14
- package/plugins/task/subagent.ts +67 -184
- package/plugins/todo/index.ts +39 -9
- package/plugins/tools/index.ts +57 -0
- package/plugins/tools/plugin.json +7 -0
- package/plugins/tui/frontend.ts +161 -87
- package/plugins/tui/index.ts +2 -3
- package/plugins/tui/mascot.ts +8 -7
- package/plugins/tui/mentions.ts +11 -80
- package/plugins/tui/plugin.json +1 -1
- package/plugins/tui/screen.ts +55 -5
- package/plugins/tui/transcript.ts +17 -7
- package/plugins/tui/tui.ts +356 -104
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +5 -3
- package/plugins/undo/index.ts +43 -14
- package/plugins/undo/style.ts +13 -14
- package/plugins/web/index.ts +7 -18
- package/plugins/web/plugin.json +1 -1
- package/plugins-dist/approval/index.js +363 -414
- package/plugins-dist/approval/policy.js +395 -497
- package/plugins-dist/approval/review.js +132 -158
- package/plugins-dist/approval/schema.js +50 -55
- package/plugins-dist/ask/index.js +115 -129
- package/plugins-dist/budget/index.js +27 -31
- package/plugins-dist/checks/checks.js +293 -329
- package/plugins-dist/checks/index.js +119 -139
- package/plugins-dist/checks/plugin.json +2 -1
- package/plugins-dist/checks/project.js +57 -65
- package/plugins-dist/commands/index.js +47 -29
- package/plugins-dist/compact/compact.js +233 -249
- package/plugins-dist/compact/index.js +98 -95
- package/plugins-dist/compact/style.js +9 -14
- package/plugins-dist/completions/index.js +30 -0
- package/plugins-dist/completions/plugin.json +9 -0
- package/plugins-dist/continue/index.js +40 -52
- package/plugins-dist/copy/index.js +40 -0
- package/plugins-dist/copy/plugin.json +9 -0
- package/plugins-dist/cost/index.js +98 -115
- package/plugins-dist/custom-commands/index.js +55 -72
- package/plugins-dist/editor/index.js +64 -72
- package/plugins-dist/fs/diff.js +111 -124
- package/plugins-dist/fs/fs.js +426 -469
- package/plugins-dist/fs/grep.js +262 -244
- package/plugins-dist/fs/index.js +20 -34
- package/plugins-dist/fs/plugin.json +1 -2
- package/plugins-dist/fs/renderers.js +100 -112
- package/plugins-dist/fs/style.js +11 -15
- package/plugins-dist/fs/sys.js +31 -43
- package/plugins-dist/hooks/index.js +107 -127
- package/plugins-dist/keys/index.js +27 -31
- package/plugins-dist/mcp/index.js +271 -199
- package/plugins-dist/mcp/install.js +123 -137
- package/plugins-dist/mcp/mcp.js +638 -577
- package/plugins-dist/mcp/plugin.json +3 -1
- package/plugins-dist/mcp/project.js +98 -115
- package/plugins-dist/memory/index.js +83 -88
- package/plugins-dist/memory/memory.js +111 -128
- package/plugins-dist/memory/style.js +9 -14
- package/plugins-dist/mentions/attach.js +91 -99
- package/plugins-dist/mentions/candidates.js +49 -0
- package/plugins-dist/mentions/index.js +71 -81
- package/plugins-dist/mentions/plugin.json +2 -1
- package/plugins-dist/mentions/style.js +9 -14
- package/plugins-dist/models/config.js +29 -43
- package/plugins-dist/models/configure.js +392 -473
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -188
- package/plugins-dist/models/provider.js +243 -295
- package/plugins-dist/models/saved.js +59 -71
- package/plugins-dist/modes/index.js +138 -165
- package/plugins-dist/plugins/index.js +34 -34
- package/plugins-dist/print/index.js +192 -175
- package/plugins-dist/prompts/index.js +81 -89
- package/plugins-dist/prompts/notes.js +31 -37
- package/plugins-dist/prompts/prompts.js +155 -193
- package/plugins-dist/prompts/results.js +40 -53
- package/plugins-dist/prompts/sys.js +39 -50
- package/plugins-dist/renderers/index.js +32 -32
- package/plugins-dist/repeats/index.js +134 -151
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +229 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +169 -0
- package/plugins-dist/screenshot/index.js +37 -45
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +101 -151
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -74
- package/plugins-dist/shell/plugin.json +1 -2
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +317 -320
- package/plugins-dist/shell/sys.js +64 -79
- package/plugins-dist/skills/index.js +193 -211
- package/plugins-dist/skills/install.js +122 -149
- package/plugins-dist/skills/project.js +65 -73
- package/plugins-dist/skills/skills.js +135 -171
- package/plugins-dist/steer/index.js +108 -125
- package/plugins-dist/suggest/index.js +235 -259
- package/plugins-dist/task/index.js +130 -148
- package/plugins-dist/task/plugin.json +5 -4
- package/plugins-dist/task/style.js +9 -14
- package/plugins-dist/task/subagent.js +156 -279
- package/plugins-dist/todo/index.js +79 -63
- package/plugins-dist/tools/index.js +43 -0
- package/plugins-dist/tools/plugin.json +9 -0
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +641 -655
- package/plugins-dist/tui/history.js +34 -37
- package/plugins-dist/tui/index.js +16 -18
- package/plugins-dist/tui/markdown.js +203 -247
- package/plugins-dist/tui/mascot.js +266 -353
- package/plugins-dist/tui/mentions.js +67 -144
- package/plugins-dist/tui/plugin.json +1 -2
- package/plugins-dist/tui/render.js +35 -45
- package/plugins-dist/tui/screen.js +197 -180
- package/plugins-dist/tui/transcript.js +436 -468
- package/plugins-dist/tui/tui.js +1554 -1645
- package/plugins-dist/tui/vendor.js +1388 -1088
- package/plugins-dist/undo/checkpoint.js +95 -118
- package/plugins-dist/undo/index.js +185 -192
- package/plugins-dist/undo/snapshot.js +83 -99
- package/plugins-dist/undo/style.js +9 -14
- package/plugins-dist/update/index.js +26 -28
- package/plugins-dist/web/env.js +55 -68
- package/plugins-dist/web/index.js +80 -110
- package/plugins-dist/web/plugin.json +1 -2
- package/plugins-dist/web/renderers.js +4 -5
- package/plugins-dist/web/web.js +514 -593
- package/sdk/python/__pycache__/jade_plugin.cpython-312.pyc +0 -0
- package/sdk/python/jade_plugin.py +9 -2
- package/src/api.ts +1 -1
- package/src/host.ts +39 -4
- package/src/remote.ts +6 -1
- package/src/session.ts +489 -34
|
@@ -1,210 +1,203 @@
|
|
|
1
|
-
// jade/undo:/undo 把模型上一轮改过的文件恢复原样(再执行一次撤销更早的一轮)。
|
|
2
|
-
//
|
|
3
|
-
// jade/fs 不再保存检查点,这里自己观察:tool.before 在 write_file / edit_file / multi_edit 执行前记下文件原来的内容,
|
|
4
|
-
// tool.after 在执行成功后记下新内容的哈希,存成快照(checkpoint.ts)。tool.before 在审批之前执行,
|
|
5
|
-
// 所以等用户批准期间别人对文件的改动会算进"原来的内容"。
|
|
6
|
-
// shell 命令改的文件:在 git 仓库里,每轮第一次 bash 之前给工作区拍一张快照(snapshot.ts),撤销时把之后变了的文件恢复回去。
|
|
7
1
|
import { readFile } from "node:fs/promises";
|
|
8
2
|
import { realpathSync } from "node:fs";
|
|
9
3
|
import path from "node:path";
|
|
10
4
|
import { definePlugin } from "@uncarved/jade-api";
|
|
11
5
|
import { Checkpoints, lastTurnChanges, safe, sha256, turnToolNames } from "./checkpoint.js";
|
|
12
6
|
import { Snapshots } from "./snapshot.js";
|
|
13
|
-
import { dim,
|
|
14
|
-
/** 会改文件、参数里有 path 的工具 */
|
|
15
|
-
export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
16
|
-
/** 没有工作区快照(不在 git 仓库里、配置关掉了)时,shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
|
|
7
|
+
import { dim, ok } from "./style.js";
|
|
17
8
|
const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
|
|
18
|
-
|
|
19
|
-
const SNAPSHOT_TIMEOUT_MS = 60_000;
|
|
20
|
-
/** 对话框和结果里最多列出这么多个文件 */
|
|
9
|
+
const SNAPSHOT_TIMEOUT_MS = 60000;
|
|
21
10
|
const MAX_LISTED = 30;
|
|
22
|
-
/** 比较两个路径是不是同一个文件用的写法:git 给的和工具参数里的大小写、斜杠、短文件名可能不同 */
|
|
23
11
|
function same(p) {
|
|
24
|
-
|
|
25
|
-
|
|
12
|
+
const full = path.join(realPath(path.dirname(p)), path.basename(p));
|
|
13
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
26
14
|
}
|
|
27
|
-
/** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
|
|
28
15
|
function display(cwd, full) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
16
|
+
const rel = path.relative(cwd, full);
|
|
17
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
18
|
+
return shown.split(path.sep).join("/");
|
|
32
19
|
}
|
|
33
20
|
function realPath(p) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
21
|
+
try {
|
|
22
|
+
return realpathSync.native(p);
|
|
23
|
+
} catch {
|
|
24
|
+
return path.resolve(p);
|
|
25
|
+
}
|
|
40
26
|
}
|
|
41
|
-
|
|
27
|
+
const decidedBy = new Map;
|
|
28
|
+
const links = new Map;
|
|
42
29
|
async function contentOf(file) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
30
|
+
try {
|
|
31
|
+
return await readFile(file, "utf8");
|
|
32
|
+
} catch (e) {
|
|
33
|
+
if (e.code === "ENOENT")
|
|
34
|
+
return null;
|
|
35
|
+
throw e;
|
|
36
|
+
}
|
|
51
37
|
}
|
|
52
38
|
export default definePlugin({
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
39
|
+
process(ctx) {
|
|
40
|
+
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
41
|
+
const cwd = realPath(ctx.cwd);
|
|
42
|
+
ctx.service("commands@1")?.register({
|
|
43
|
+
name: "undo",
|
|
44
|
+
description: "restore the files the agent changed in its last turn; in a git repository that includes what its shell commands changed (run again to go further back)",
|
|
45
|
+
async run(_args, { sessions, ui, signal }) {
|
|
46
|
+
const s = sessions.current;
|
|
47
|
+
if (!s)
|
|
48
|
+
return "No session is open.";
|
|
49
|
+
const snapshots = new Snapshots(path.join(ctx.home, "checkpoints", safe(s.id)), ctx.cwd);
|
|
50
|
+
const undone = await checkpoints.undone(s.id);
|
|
51
|
+
const snaps = await snapshots.list();
|
|
52
|
+
const entries = [...await checkpoints.list(s.id), ...snaps].sort((x, y) => y.at - x.at);
|
|
53
|
+
let cps;
|
|
54
|
+
let snap;
|
|
55
|
+
let tree;
|
|
56
|
+
for (;; ) {
|
|
57
|
+
const hits = lastTurnChanges(s.state.messages, entries, undone);
|
|
58
|
+
cps = hits.filter((h) => ("path" in h));
|
|
59
|
+
snap = hits.find((h) => ("tree" in h));
|
|
60
|
+
tree = snap ? await snapshots.changed(snap.tree) : [];
|
|
61
|
+
if (!snap || cps.length > 0 || tree.length > 0)
|
|
62
|
+
break;
|
|
63
|
+
undone.add(snap.toolUseId);
|
|
64
|
+
await checkpoints.markUndone(s.id, [snap.toolUseId]);
|
|
65
|
+
}
|
|
66
|
+
const ranShell = (names) => [...names].some((n) => s.tool(n)?.meta?.shell !== undefined);
|
|
67
|
+
if (cps.length === 0 && !snap)
|
|
68
|
+
return dim(snaps.length === 0 && ranShell(turnToolNames(s.state.messages)) ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
|
|
69
|
+
const shellToo = !snap && ranShell(turnToolNames(s.state.messages, cps[0].toolUseId));
|
|
70
|
+
const inTree = new Set(tree.map((c) => same(c.path)));
|
|
71
|
+
const fileCps = cps.filter((cp) => !(snap && cp.at > snap.at && inTree.has(same(cp.path))));
|
|
72
|
+
const files = new Map;
|
|
73
|
+
for (const cp of fileCps)
|
|
74
|
+
files.set(cp.path, cp);
|
|
75
|
+
const plan = new Map;
|
|
76
|
+
for (const c of tree)
|
|
77
|
+
plan.set(same(c.path), { path: c.path, gone: c.added });
|
|
78
|
+
for (const cp of files.values())
|
|
79
|
+
plan.set(same(cp.path), { path: cp.path, gone: cp.before === null });
|
|
80
|
+
const shown = new Map([...plan.values()].map((f) => [f.path, display(cwd, path.join(realPath(path.dirname(f.path)), path.basename(f.path)))]));
|
|
81
|
+
const show = (p) => shown.get(p) ?? display(cwd, p);
|
|
82
|
+
const latest = new Map;
|
|
83
|
+
for (const cp of fileCps)
|
|
84
|
+
if (!latest.has(cp.path))
|
|
85
|
+
latest.set(cp.path, cp);
|
|
86
|
+
const changed = [];
|
|
87
|
+
for (const cp of latest.values())
|
|
88
|
+
if (!inTree.has(same(cp.path)) && !await checkpoints.unchangedSince(cp))
|
|
89
|
+
changed.push(show(cp.path));
|
|
90
|
+
const rows = [...plan.values()].map((f) => ` ${f.gone ? "delete " : "restore"} ${show(f.path)}`);
|
|
91
|
+
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join(`
|
|
92
|
+
`) : rows.join(`
|
|
93
|
+
`);
|
|
94
|
+
if (plan.size > 1 || changed.length > 0 || tree.length > 0) {
|
|
95
|
+
const ok = await ui.select({
|
|
96
|
+
title: changed.length > 0 ? `${changed.join(", ")} changed since the agent wrote ${changed.length > 1 ? "them" : "it"}. Undo anyway and discard those changes too?` : tree.length > 0 ? `Undo the agent's last turn (${plan.size} file${plan.size > 1 ? "s" : ""})? It ran shell commands: these files go back to how they were before the first command, and edits you made to them since then are lost too.` : `Undo the agent's last turn of changes (${plan.size} files)?`,
|
|
97
|
+
detail: list,
|
|
98
|
+
options: [
|
|
99
|
+
{ label: "No", value: "no" },
|
|
100
|
+
{ label: changed.length > 0 ? "Yes, undo anyway" : "Yes, undo", value: "yes" }
|
|
101
|
+
]
|
|
102
|
+
}, signal);
|
|
103
|
+
if (ok !== "yes")
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
if (snap)
|
|
107
|
+
await snapshots.restore(snap.tree, tree);
|
|
108
|
+
for (const cp of fileCps)
|
|
109
|
+
await checkpoints.restore(cp);
|
|
110
|
+
await checkpoints.markUndone(s.id, [...cps.map((c) => c.toolUseId), ...snap ? [snap.toolUseId] : []]);
|
|
111
|
+
const what = [...plan.values()].slice(0, MAX_LISTED).map((f) => f.gone ? `deleted ${show(f.path)} (you had created it)` : `restored ${show(f.path)} to its content before your changes`);
|
|
112
|
+
if (plan.size > MAX_LISTED)
|
|
113
|
+
what.push(`and ${plan.size - MAX_LISTED} more files`);
|
|
114
|
+
ctx.service("prompts@1")?.note(s.id, `The user undid your changes from your last turn: ${what.join("; ")}. Read these files again before changing them.`);
|
|
115
|
+
const title = snap ? `Undid the last turn (${plan.size} file${plan.size > 1 ? "s" : ""}):` : `Undid ${cps.length} change${cps.length > 1 ? "s" : ""} from the last turn:`;
|
|
116
|
+
return [ok(title), `
|
|
117
|
+
`, dim(list), ...shellToo ? [`
|
|
118
|
+
`, dim(SHELL_NOTE)] : []];
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
},
|
|
122
|
+
session(ctx) {
|
|
123
|
+
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
124
|
+
const sessionId = ctx.session.id;
|
|
125
|
+
const decided = new Map;
|
|
126
|
+
decidedBy.set(sessionId, decided);
|
|
127
|
+
const link = () => {
|
|
128
|
+
const p = ctx.session.parent;
|
|
129
|
+
if (!p)
|
|
130
|
+
return { root: sessionId };
|
|
131
|
+
const up = links.get(p.sessionId)?.();
|
|
132
|
+
if (up?.via)
|
|
133
|
+
return up;
|
|
134
|
+
const via = decidedBy.get(p.sessionId)?.get(p.eventId);
|
|
135
|
+
return { root: up?.root ?? p.sessionId, ...via ? { via } : {} };
|
|
136
|
+
};
|
|
137
|
+
links.set(sessionId, link);
|
|
138
|
+
ctx.effect(() => () => {
|
|
139
|
+
if (decidedBy.get(sessionId) === decided)
|
|
140
|
+
decidedBy.delete(sessionId);
|
|
141
|
+
if (links.get(sessionId) === link)
|
|
142
|
+
links.delete(sessionId);
|
|
143
|
+
});
|
|
144
|
+
const before = new Map;
|
|
145
|
+
const snapshots = new Snapshots(path.join(ctx.home, "checkpoints", safe(sessionId)), ctx.cwd);
|
|
146
|
+
let snapped = false;
|
|
147
|
+
ctx.kernel.hook({
|
|
148
|
+
kind: "transform",
|
|
149
|
+
point: "tool.before",
|
|
150
|
+
order: 50,
|
|
151
|
+
optional: true,
|
|
152
|
+
timeoutMs: SNAPSHOT_TIMEOUT_MS,
|
|
153
|
+
fn: async (call) => {
|
|
154
|
+
const meta = ctx.session.tool(call.name)?.meta;
|
|
155
|
+
if (meta?.shell !== undefined && ctx.config.shell !== false && !snapped && !ctx.session.parent) {
|
|
156
|
+
snapped = true;
|
|
157
|
+
const tree = await snapshots.take();
|
|
158
|
+
if (tree)
|
|
159
|
+
await snapshots.save({ toolUseId: call.toolUseId, tree, at: performance.timeOrigin + performance.now() });
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
if (meta?.writes === undefined)
|
|
163
|
+
return;
|
|
164
|
+
const p = call.input?.[meta.writes];
|
|
165
|
+
if (typeof p !== "string")
|
|
166
|
+
return;
|
|
167
|
+
const full = path.resolve(ctx.cwd, p);
|
|
168
|
+
before.set(call.toolUseId, { tool: call.name, path: full, before: await contentOf(full) });
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
ctx.kernel.hook({
|
|
172
|
+
kind: "transform",
|
|
173
|
+
point: "tool.after",
|
|
174
|
+
optional: true,
|
|
175
|
+
fn: async (out) => {
|
|
176
|
+
const snap = before.get(out.call.toolUseId);
|
|
177
|
+
if (!snap)
|
|
178
|
+
return;
|
|
179
|
+
before.delete(out.call.toolUseId);
|
|
180
|
+
if (out.isError)
|
|
181
|
+
return;
|
|
182
|
+
const after = await contentOf(snap.path);
|
|
183
|
+
if (after === snap.before)
|
|
184
|
+
return;
|
|
185
|
+
const { root, via } = link();
|
|
186
|
+
await checkpoints.save(root, { toolUseId: out.call.toolUseId, ...snap, afterHash: sha256(after ?? ""), ...via ? { via } : {} });
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
ctx.kernel.hook({
|
|
190
|
+
kind: "observe",
|
|
191
|
+
point: "event",
|
|
192
|
+
fn: (e) => {
|
|
193
|
+
if (e.type === "tool.decided")
|
|
194
|
+
decided.set(e.id, e.data.toolUseId);
|
|
195
|
+
if (e.type !== "turn.ended")
|
|
196
|
+
return;
|
|
197
|
+
before.clear();
|
|
198
|
+
decided.clear();
|
|
199
|
+
snapped = false;
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
}
|
|
210
203
|
});
|
|
@@ -1,111 +1,95 @@
|
|
|
1
|
-
// 工作区快照:shell 命令改了什么无从得知,所以每轮第一次跑命令之前给整个工作区拍一张快照,/undo 时把之后变了的文件恢复回去。
|
|
2
|
-
//
|
|
3
|
-
// 快照存在 $JADE_HOME 里的一个影子 git 仓库(<dir>/shadow/.git),工作区指向项目的仓库根目录:
|
|
4
|
-
// 项目自己的 .git(暂存区、提交、stash)一点不碰。只在 git 仓库里做:要靠 .gitignore 跳过 node_modules 这类目录,
|
|
5
|
-
// 对象库借用项目的(alternates),已经提交过的内容不再存一份。被忽略的文件不在快照里,撤销不了。
|
|
6
|
-
// 快照是 tree 对象,不建提交;哪一轮对应哪个 tree 记在 snapshots.json。
|
|
7
1
|
import { execFile } from "node:child_process";
|
|
8
2
|
import { mkdir, readFile, rm, writeFile } from "node:fs/promises";
|
|
9
3
|
import path from "node:path";
|
|
10
4
|
const FILE = "snapshots.json";
|
|
11
|
-
/** 一次 git 命令最多带这么多路径:Windows 的命令行长度有限 */
|
|
12
5
|
const CHUNK = 100;
|
|
6
|
+
|
|
13
7
|
export class Snapshots {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
await writeFile(path.join(gitDir, "objects", "info", "alternates"), `${objects.split(path.sep).join("/")}\n`);
|
|
48
|
-
for (const [k, v] of [["core.autocrlf", "false"], ["core.safecrlf", "false"], ["core.longpaths", "true"], ["gc.auto", "0"]]) {
|
|
49
|
-
await git(["config", k, v], { cwd: root, env });
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
return { env, root };
|
|
53
|
-
})());
|
|
54
|
-
}
|
|
55
|
-
/** 给工作区拍一张快照,返回 tree;不在 git 仓库里时 undefined */
|
|
56
|
-
async take() {
|
|
57
|
-
const r = await this.#prepare();
|
|
58
|
-
if (!r)
|
|
59
|
-
return undefined;
|
|
60
|
-
await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {
|
|
61
|
-
// 有的文件加不进去(被别的程序锁着、嵌套的仓库):其余的照样拍
|
|
62
|
-
});
|
|
63
|
-
return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
|
|
64
|
-
}
|
|
65
|
-
/** 工作区现在和快照 tree 相比变了的文件 */
|
|
66
|
-
async changed(tree) {
|
|
67
|
-
const r = await this.#prepare();
|
|
68
|
-
const now = await this.take();
|
|
69
|
-
if (!r || !now || now === tree)
|
|
70
|
-
return [];
|
|
71
|
-
const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", tree, now], { cwd: r.root, env: r.env });
|
|
72
|
-
const parts = out.split("\0");
|
|
73
|
-
const changes = [];
|
|
74
|
-
for (let i = 0; i + 1 < parts.length; i += 2)
|
|
75
|
-
changes.push({ path: path.join(r.root, parts[i + 1]), added: parts[i] === "A" });
|
|
76
|
-
return changes;
|
|
77
|
-
}
|
|
78
|
-
/** 把这些文件恢复成快照 tree 里的样子:之后新出现的删掉,其余的取回 */
|
|
79
|
-
async restore(tree, changes) {
|
|
80
|
-
const r = await this.#prepare();
|
|
81
|
-
if (!r)
|
|
82
|
-
return;
|
|
83
|
-
for (const c of changes)
|
|
84
|
-
if (c.added)
|
|
85
|
-
await rm(c.path, { force: true });
|
|
86
|
-
const back = changes.filter((c) => !c.added).map((c) => path.relative(r.root, c.path).split(path.sep).join("/"));
|
|
87
|
-
for (let i = 0; i < back.length; i += CHUNK) {
|
|
88
|
-
await git(["--literal-pathspecs", "checkout", tree, "--", ...back.slice(i, i + CHUNK)], { cwd: r.root, env: r.env });
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/** 本会话记下的快照,最新的在前 */
|
|
92
|
-
async list() {
|
|
93
|
-
try {
|
|
94
|
-
const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8"));
|
|
95
|
-
return Array.isArray(all) ? all.sort((a, b) => b.at - a.at) : [];
|
|
96
|
-
}
|
|
97
|
-
catch {
|
|
98
|
-
return [];
|
|
8
|
+
#dir;
|
|
9
|
+
#cwd;
|
|
10
|
+
#ready;
|
|
11
|
+
constructor(dir, cwd) {
|
|
12
|
+
this.#dir = dir;
|
|
13
|
+
this.#cwd = cwd;
|
|
14
|
+
}
|
|
15
|
+
#prepare() {
|
|
16
|
+
return this.#ready ??= (async () => {
|
|
17
|
+
let root;
|
|
18
|
+
let objects;
|
|
19
|
+
try {
|
|
20
|
+
root = path.resolve((await git(["rev-parse", "--show-toplevel"], { cwd: this.#cwd })).trim());
|
|
21
|
+
objects = path.resolve(this.#cwd, (await git(["rev-parse", "--git-common-dir"], { cwd: this.#cwd })).trim(), "objects");
|
|
22
|
+
} catch {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const shadow = path.join(this.#dir, "shadow");
|
|
26
|
+
const gitDir = path.join(shadow, ".git");
|
|
27
|
+
const env = { ...process.env, GIT_DIR: gitDir, GIT_WORK_TREE: root };
|
|
28
|
+
delete env.GIT_INDEX_FILE;
|
|
29
|
+
try {
|
|
30
|
+
await readFile(path.join(gitDir, "HEAD"));
|
|
31
|
+
} catch {
|
|
32
|
+
await mkdir(shadow, { recursive: true });
|
|
33
|
+
await git(["init", "--quiet", shadow], { cwd: shadow });
|
|
34
|
+
await mkdir(path.join(gitDir, "info"), { recursive: true });
|
|
35
|
+
await writeFile(path.join(gitDir, "info", "attributes"), `* -text -eol -filter -ident -working-tree-encoding
|
|
36
|
+
`);
|
|
37
|
+
await writeFile(path.join(gitDir, "objects", "info", "alternates"), `${objects.split(path.sep).join("/")}
|
|
38
|
+
`);
|
|
39
|
+
for (const [k, v] of [["core.autocrlf", "false"], ["core.safecrlf", "false"], ["core.longpaths", "true"], ["gc.auto", "0"]]) {
|
|
40
|
+
await git(["config", k, v], { cwd: root, env });
|
|
99
41
|
}
|
|
42
|
+
}
|
|
43
|
+
return { env, root };
|
|
44
|
+
})();
|
|
45
|
+
}
|
|
46
|
+
async take() {
|
|
47
|
+
const r = await this.#prepare();
|
|
48
|
+
if (!r)
|
|
49
|
+
return;
|
|
50
|
+
await git(["add", "-A", "--ignore-errors", "."], { cwd: r.root, env: r.env }).catch(() => {});
|
|
51
|
+
return (await git(["write-tree"], { cwd: r.root, env: r.env })).trim();
|
|
52
|
+
}
|
|
53
|
+
async changed(tree) {
|
|
54
|
+
const r = await this.#prepare();
|
|
55
|
+
const now = await this.take();
|
|
56
|
+
if (!r || !now || now === tree)
|
|
57
|
+
return [];
|
|
58
|
+
const out = await git(["diff-tree", "-r", "--no-renames", "--name-status", "-z", tree, now], { cwd: r.root, env: r.env });
|
|
59
|
+
const parts = out.split("\x00");
|
|
60
|
+
const changes = [];
|
|
61
|
+
for (let i = 0;i + 1 < parts.length; i += 2)
|
|
62
|
+
changes.push({ path: path.join(r.root, parts[i + 1]), added: parts[i] === "A" });
|
|
63
|
+
return changes;
|
|
64
|
+
}
|
|
65
|
+
async restore(tree, changes) {
|
|
66
|
+
const r = await this.#prepare();
|
|
67
|
+
if (!r)
|
|
68
|
+
return;
|
|
69
|
+
for (const c of changes)
|
|
70
|
+
if (c.added)
|
|
71
|
+
await rm(c.path, { force: true });
|
|
72
|
+
const back = changes.filter((c) => !c.added).map((c) => path.relative(r.root, c.path).split(path.sep).join("/"));
|
|
73
|
+
for (let i = 0;i < back.length; i += CHUNK) {
|
|
74
|
+
await git(["--literal-pathspecs", "checkout", tree, "--", ...back.slice(i, i + CHUNK)], { cwd: r.root, env: r.env });
|
|
100
75
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
76
|
+
}
|
|
77
|
+
async list() {
|
|
78
|
+
try {
|
|
79
|
+
const all = JSON.parse(await readFile(path.join(this.#dir, FILE), "utf8"));
|
|
80
|
+
return Array.isArray(all) ? all.sort((a, b) => b.at - a.at) : [];
|
|
81
|
+
} catch {
|
|
82
|
+
return [];
|
|
105
83
|
}
|
|
84
|
+
}
|
|
85
|
+
async save(snapshot) {
|
|
86
|
+
const all = await this.list();
|
|
87
|
+
await mkdir(this.#dir, { recursive: true });
|
|
88
|
+
await writeFile(path.join(this.#dir, FILE), JSON.stringify([snapshot, ...all]));
|
|
89
|
+
}
|
|
106
90
|
}
|
|
107
91
|
function git(args, opts) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
92
|
+
return new Promise((resolve, reject) => {
|
|
93
|
+
execFile("git", args, { cwd: opts.cwd, env: opts.env, maxBuffer: 64 * 1024 * 1024, windowsHide: true }, (err, stdout) => err ? reject(err) : resolve(stdout));
|
|
94
|
+
});
|
|
111
95
|
}
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export const dim = paint("2", "22");
|
|
11
|
-
export const red = paint("31", "39");
|
|
12
|
-
export const green = paint("32", "39");
|
|
13
|
-
export const yellow = paint("33", "39");
|
|
14
|
-
export const bold = paint("1", "22");
|
|
1
|
+
export const clip = (s, n) => s.length > n ? s.slice(0, n) + "…" : s;
|
|
2
|
+
const styled = (style) => (text) => ({ text, style });
|
|
3
|
+
export const dim = styled("dim");
|
|
4
|
+
export const bold = styled("bold");
|
|
5
|
+
export const ok = styled("ok");
|
|
6
|
+
export const warn = styled("warn");
|
|
7
|
+
export const error = styled("error");
|
|
8
|
+
export const lines = (rows) => rows.flatMap((r, i) => i ? [`
|
|
9
|
+
`, r] : [r]);
|