@uncarved/jade 0.3.1 → 0.4.1
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 +490 -29
- package/dist/session.js.map +1 -1
- package/package.json +4 -4
- package/plugins/approval/index.ts +36 -14
- package/plugins/approval/policy.ts +15 -10
- 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 +42 -9
- 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 +47 -12
- package/plugins/models/index.ts +27 -1
- package/plugins/models/provider.ts +24 -0
- package/plugins/models/saved.ts +1 -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 +8 -8
- package/plugins/schedule/index.ts +5 -1
- package/plugins/screenshot/browser.ts +10 -0
- package/plugins/sessions/index.ts +48 -2
- package/plugins/shell/index.ts +27 -28
- package/plugins/shell/plugin.json +1 -1
- package/plugins/shell/renderers.ts +9 -7
- package/plugins/shell/shell.ts +3 -1
- 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 +80 -50
- package/plugins/task/plugin.json +6 -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 +9 -2
- package/plugins/tui/frontend.ts +139 -89
- package/plugins/tui/index.ts +2 -3
- 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 +337 -101
- package/plugins/tui/vendor.ts +1 -1
- package/plugins/undo/checkpoint.ts +8 -4
- package/plugins/undo/index.ts +170 -31
- package/plugins/undo/plugin.json +1 -0
- package/plugins/undo/snapshot.ts +87 -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 -418
- package/plugins-dist/approval/policy.js +391 -508
- 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 -253
- 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 +421 -474
- package/plugins-dist/models/context.js +14 -22
- package/plugins-dist/models/index.js +182 -189
- package/plugins-dist/models/provider.js +263 -307
- package/plugins-dist/models/saved.js +60 -72
- 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 +86 -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 +131 -174
- package/plugins-dist/schedule/cron.js +49 -56
- package/plugins-dist/schedule/index.js +230 -247
- package/plugins-dist/schedule/store.js +93 -109
- package/plugins-dist/scratch/index.js +12 -18
- package/plugins-dist/screenshot/browser.js +136 -154
- package/plugins-dist/screenshot/index.js +37 -46
- package/plugins-dist/screenshot/sys.js +53 -65
- package/plugins-dist/screenshot/tool.js +98 -126
- package/plugins-dist/sessions/index.js +149 -117
- package/plugins-dist/shell/index.js +117 -135
- package/plugins-dist/shell/plugin.json +1 -1
- package/plugins-dist/shell/renderers.js +28 -33
- package/plugins-dist/shell/service.js +17 -23
- package/plugins-dist/shell/shell.js +307 -362
- 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 -117
- package/plugins-dist/task/plugin.json +5 -2
- 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 +36 -42
- package/plugins-dist/tui/editor.js +145 -160
- package/plugins-dist/tui/frontend.js +639 -678
- 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 -355
- 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 -1656
- package/plugins-dist/tui/vendor.js +1088 -1388
- package/plugins-dist/undo/checkpoint.js +97 -118
- package/plugins-dist/undo/index.js +281 -192
- package/plugins-dist/undo/plugin.json +3 -0
- package/plugins-dist/undo/snapshot.js +134 -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 +496 -36
|
@@ -1,210 +1,299 @@
|
|
|
1
|
-
|
|
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),撤销时把之后变了的文件恢复回去。
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
7
2
|
import { readFile } from "node:fs/promises";
|
|
8
3
|
import { realpathSync } from "node:fs";
|
|
9
4
|
import path from "node:path";
|
|
10
5
|
import { definePlugin } from "@uncarved/jade-api";
|
|
11
6
|
import { Checkpoints, lastTurnChanges, safe, sha256, turnToolNames } from "./checkpoint.js";
|
|
12
|
-
import {
|
|
13
|
-
import { dim,
|
|
14
|
-
/** 会改文件、参数里有 path 的工具 */
|
|
15
|
-
export const WRITE_TOOLS = new Set(["write_file", "edit_file", "multi_edit"]);
|
|
16
|
-
/** 没有工作区快照(不在 git 仓库里、配置关掉了)时,shell 命令改了什么无从得知,撤销不了;这一轮跑过时提醒一句 */
|
|
7
|
+
import { snapshotsAt } from "./snapshot.js";
|
|
8
|
+
import { dim, ok } from "./style.js";
|
|
17
9
|
const SHELL_NOTE = "This turn also ran shell commands; what they changed is not undone.";
|
|
18
|
-
|
|
19
|
-
const SNAPSHOT_TIMEOUT_MS = 60_000;
|
|
20
|
-
/** 对话框和结果里最多列出这么多个文件 */
|
|
10
|
+
const SNAPSHOT_TIMEOUT_MS = 60000;
|
|
21
11
|
const MAX_LISTED = 30;
|
|
22
|
-
/** 比较两个路径是不是同一个文件用的写法:git 给的和工具参数里的大小写、斜杠、短文件名可能不同 */
|
|
23
12
|
function same(p) {
|
|
24
|
-
|
|
25
|
-
|
|
13
|
+
const full = path.join(realPath(path.dirname(p)), path.basename(p));
|
|
14
|
+
return process.platform === "win32" ? full.toLowerCase() : full;
|
|
26
15
|
}
|
|
27
|
-
/** 显示用:工作目录内的路径用相对形式,统一为正斜杠 */
|
|
28
16
|
function display(cwd, full) {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
17
|
+
const rel = path.relative(cwd, full);
|
|
18
|
+
const shown = rel && rel !== ".." && !rel.startsWith(`..${path.sep}`) && !path.isAbsolute(rel) ? rel : full;
|
|
19
|
+
return shown.split(path.sep).join("/");
|
|
32
20
|
}
|
|
33
21
|
function realPath(p) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
22
|
+
try {
|
|
23
|
+
return realpathSync.native(p);
|
|
24
|
+
} catch {
|
|
25
|
+
return path.resolve(p);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
const decidedBy = new Map;
|
|
29
|
+
const links = new Map;
|
|
30
|
+
const turnSnaps = new Map;
|
|
31
|
+
const closing = new Map;
|
|
32
|
+
const now = () => performance.timeOrigin + performance.now();
|
|
33
|
+
const snapshotsOf = (home, sessionId, cwd) => snapshotsAt(path.join(home, "checkpoints", safe(sessionId)), cwd);
|
|
34
|
+
function closeTurn(root) {
|
|
35
|
+
const p = turnSnaps.get(root);
|
|
36
|
+
if (!p)
|
|
37
|
+
return;
|
|
38
|
+
turnSnaps.delete(root);
|
|
39
|
+
const done = p.then((r) => r?.snapshots.close(r.toolUseId)).catch(() => {});
|
|
40
|
+
closing.set(root, done);
|
|
41
|
+
return done;
|
|
40
42
|
}
|
|
41
|
-
/** 文件现在的内容;不存在时 null */
|
|
42
43
|
async function contentOf(file) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
44
|
+
try {
|
|
45
|
+
return await readFile(file, "utf8");
|
|
46
|
+
} catch (e) {
|
|
47
|
+
if (e.code === "ENOENT")
|
|
48
|
+
return null;
|
|
49
|
+
throw e;
|
|
50
|
+
}
|
|
51
51
|
}
|
|
52
52
|
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
|
-
const
|
|
148
|
-
const
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
const
|
|
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
|
-
|
|
53
|
+
process(ctx) {
|
|
54
|
+
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
55
|
+
const cwd = realPath(ctx.cwd);
|
|
56
|
+
ctx.provide("undo@1", {
|
|
57
|
+
async track(session, label, fn) {
|
|
58
|
+
if (ctx.config.shell === false)
|
|
59
|
+
return fn();
|
|
60
|
+
const snapshots = snapshotsOf(ctx.home, session, ctx.cwd);
|
|
61
|
+
await closing.get(session);
|
|
62
|
+
let id;
|
|
63
|
+
try {
|
|
64
|
+
const tree = await snapshots.take();
|
|
65
|
+
if (tree) {
|
|
66
|
+
id = `user-${randomUUID()}`;
|
|
67
|
+
await snapshots.save({ toolUseId: id, tree, label, at: now() });
|
|
68
|
+
}
|
|
69
|
+
} catch {}
|
|
70
|
+
try {
|
|
71
|
+
return await fn();
|
|
72
|
+
} finally {
|
|
73
|
+
if (id)
|
|
74
|
+
await snapshots.close(id).catch(() => {});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
const shownName = (p) => display(cwd, path.join(realPath(path.dirname(p)), path.basename(p)));
|
|
79
|
+
const undoRun = async (sessionId, snapshots, run, { changes, conflicts }, ui, signal) => {
|
|
80
|
+
const shown = new Map(changes.map((c) => [c.path, shownName(c.path)]));
|
|
81
|
+
const rows = changes.map((c) => ` ${c.added ? "delete " : "restore"} ${shown.get(c.path)}`);
|
|
82
|
+
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join(`
|
|
83
|
+
`) : rows.join(`
|
|
84
|
+
`);
|
|
85
|
+
const changed = changes.filter((c) => conflicts.has(c.path)).map((c) => shown.get(c.path));
|
|
86
|
+
const files = `${changes.length} file${changes.length > 1 ? "s" : ""}`;
|
|
87
|
+
const answer = await ui.select({
|
|
88
|
+
title: changed.length > 0 ? `${changed.join(", ")} changed since your command ${run.label} ran. Undo it anyway and discard those changes too?` : `Undo your command ${run.label} (${files})?`,
|
|
89
|
+
detail: list,
|
|
90
|
+
options: [
|
|
91
|
+
{ label: "No", value: "no" },
|
|
92
|
+
{ label: changed.length > 0 ? "Yes, undo anyway" : "Yes, undo", value: "yes" }
|
|
93
|
+
]
|
|
94
|
+
}, signal);
|
|
95
|
+
if (answer !== "yes")
|
|
96
|
+
return;
|
|
97
|
+
await snapshots.restore(run.tree, changes);
|
|
98
|
+
await checkpoints.markUndone(sessionId, [run.toolUseId]);
|
|
99
|
+
const what = changes.slice(0, MAX_LISTED).map((c) => c.added ? `deleted ${shown.get(c.path)} (the command had created it)` : `restored ${shown.get(c.path)}`);
|
|
100
|
+
if (changes.length > MAX_LISTED)
|
|
101
|
+
what.push(`and ${changes.length - MAX_LISTED} more files`);
|
|
102
|
+
ctx.service("prompts@1")?.note(sessionId, `The user undid what their own shell command ${run.label} changed: ${what.join("; ")}. Read these files again before changing them.`);
|
|
103
|
+
return [ok(`Undid your command ${run.label} (${files}):`), `
|
|
104
|
+
`, dim(list)];
|
|
105
|
+
};
|
|
106
|
+
ctx.service("commands@1")?.register({
|
|
107
|
+
name: "undo",
|
|
108
|
+
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)",
|
|
109
|
+
async run(_args, { sessions, ui, signal }) {
|
|
110
|
+
const s = sessions.current;
|
|
111
|
+
if (!s)
|
|
112
|
+
return "No session is open.";
|
|
113
|
+
await closing.get(s.id);
|
|
114
|
+
const snapshots = snapshotsOf(ctx.home, s.id, ctx.cwd);
|
|
115
|
+
const undone = await checkpoints.undone(s.id);
|
|
116
|
+
const snaps = await snapshots.list();
|
|
117
|
+
const runs = snaps.filter((x) => x.label !== undefined);
|
|
118
|
+
const entries = [...await checkpoints.list(s.id), ...snaps.filter((x) => x.label === undefined)].sort((x, y) => y.at - x.at);
|
|
119
|
+
let cps = [];
|
|
120
|
+
let snap;
|
|
121
|
+
let tree = [];
|
|
122
|
+
let conflicts = new Set;
|
|
123
|
+
for (;; ) {
|
|
124
|
+
const hits = lastTurnChanges(s.state.messages, entries, undone);
|
|
125
|
+
const run = runs.find((r) => !undone.has(r.toolUseId));
|
|
126
|
+
if (run && (hits.length === 0 || run.at > Math.min(...hits.map((h) => h.at)))) {
|
|
127
|
+
const found = await snapshots.changes(run);
|
|
128
|
+
if (found.changes.length > 0)
|
|
129
|
+
return undoRun(s.id, snapshots, run, found, ui, signal);
|
|
130
|
+
undone.add(run.toolUseId);
|
|
131
|
+
await checkpoints.markUndone(s.id, [run.toolUseId]);
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
cps = hits.filter((h) => ("path" in h));
|
|
135
|
+
snap = hits.find((h) => ("tree" in h));
|
|
136
|
+
({ changes: tree, conflicts } = snap ? await snapshots.changes(snap) : { changes: [], conflicts: new Set });
|
|
137
|
+
if (!snap || cps.length > 0 || tree.length > 0)
|
|
138
|
+
break;
|
|
139
|
+
undone.add(snap.toolUseId);
|
|
140
|
+
await checkpoints.markUndone(s.id, [snap.toolUseId]);
|
|
141
|
+
}
|
|
142
|
+
const ranShell = (names) => [...names].some((n) => s.tool(n)?.meta?.shell !== undefined);
|
|
143
|
+
if (cps.length === 0 && !snap)
|
|
144
|
+
return dim(snaps.length === 0 && ranShell(turnToolNames(s.state.messages)) ? `Nothing to undo. ${SHELL_NOTE}` : "Nothing to undo.");
|
|
145
|
+
const shellToo = !snap && ranShell(turnToolNames(s.state.messages, cps[0].toolUseId));
|
|
146
|
+
const inTree = new Set(tree.map((c) => same(c.path)));
|
|
147
|
+
const fileCps = cps.filter((cp) => !(snap && cp.at > snap.at && inTree.has(same(cp.path))));
|
|
148
|
+
const files = new Map;
|
|
149
|
+
for (const cp of fileCps)
|
|
150
|
+
files.set(cp.path, cp);
|
|
151
|
+
const plan = new Map;
|
|
152
|
+
for (const c of tree)
|
|
153
|
+
plan.set(same(c.path), { path: c.path, gone: c.added });
|
|
154
|
+
for (const cp of files.values())
|
|
155
|
+
plan.set(same(cp.path), { path: cp.path, gone: cp.before === null });
|
|
156
|
+
const shown = new Map([...plan.values()].map((f) => [f.path, display(cwd, path.join(realPath(path.dirname(f.path)), path.basename(f.path)))]));
|
|
157
|
+
const show = (p) => shown.get(p) ?? display(cwd, p);
|
|
158
|
+
const latest = new Map;
|
|
159
|
+
for (const cp of fileCps)
|
|
160
|
+
if (!latest.has(cp.path))
|
|
161
|
+
latest.set(cp.path, cp);
|
|
162
|
+
const changed = [];
|
|
163
|
+
for (const cp of latest.values())
|
|
164
|
+
if (!inTree.has(same(cp.path)) && !await checkpoints.unchangedSince(cp))
|
|
165
|
+
changed.push(show(cp.path));
|
|
166
|
+
for (const c of tree)
|
|
167
|
+
if (conflicts.has(c.path))
|
|
168
|
+
changed.push(show(c.path));
|
|
169
|
+
const rows = [...plan.values()].map((f) => ` ${f.gone ? "delete " : "restore"} ${show(f.path)}`);
|
|
170
|
+
const list = rows.length > MAX_LISTED ? [...rows.slice(0, MAX_LISTED), ` … ${rows.length - MAX_LISTED} more`].join(`
|
|
171
|
+
`) : rows.join(`
|
|
172
|
+
`);
|
|
173
|
+
if (plan.size > 1 || changed.length > 0 || tree.length > 0) {
|
|
174
|
+
const ok = await ui.select({
|
|
175
|
+
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${snap?.after ? "." : ", and edits you made to them since then are lost too."}` : `Undo the agent's last turn of changes (${plan.size} files)?`,
|
|
176
|
+
detail: list,
|
|
177
|
+
options: [
|
|
178
|
+
{ label: "No", value: "no" },
|
|
179
|
+
{ label: changed.length > 0 ? "Yes, undo anyway" : "Yes, undo", value: "yes" }
|
|
180
|
+
]
|
|
181
|
+
}, signal);
|
|
182
|
+
if (ok !== "yes")
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
185
|
+
if (snap)
|
|
186
|
+
await snapshots.restore(snap.tree, tree);
|
|
187
|
+
for (const cp of fileCps)
|
|
188
|
+
await checkpoints.restore(cp);
|
|
189
|
+
await checkpoints.markUndone(s.id, [...cps.map((c) => c.toolUseId), ...snap ? [snap.toolUseId] : []]);
|
|
190
|
+
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`);
|
|
191
|
+
if (plan.size > MAX_LISTED)
|
|
192
|
+
what.push(`and ${plan.size - MAX_LISTED} more files`);
|
|
193
|
+
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.`);
|
|
194
|
+
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:`;
|
|
195
|
+
return [ok(title), `
|
|
196
|
+
`, dim(list), ...shellToo ? [`
|
|
197
|
+
`, dim(SHELL_NOTE)] : []];
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
},
|
|
201
|
+
session(ctx) {
|
|
202
|
+
const checkpoints = new Checkpoints(path.join(ctx.home, "checkpoints"));
|
|
203
|
+
const sessionId = ctx.session.id;
|
|
204
|
+
const decided = new Map;
|
|
205
|
+
decidedBy.set(sessionId, decided);
|
|
206
|
+
const link = () => {
|
|
207
|
+
const p = ctx.session.parent;
|
|
208
|
+
if (!p)
|
|
209
|
+
return { root: sessionId };
|
|
210
|
+
const up = links.get(p.sessionId)?.();
|
|
211
|
+
if (up?.via)
|
|
212
|
+
return up;
|
|
213
|
+
const via = decidedBy.get(p.sessionId)?.get(p.eventId);
|
|
214
|
+
return { root: up?.root ?? p.sessionId, ...via ? { via } : {} };
|
|
215
|
+
};
|
|
216
|
+
links.set(sessionId, link);
|
|
217
|
+
ctx.effect(() => () => {
|
|
218
|
+
if (decidedBy.get(sessionId) === decided)
|
|
219
|
+
decidedBy.delete(sessionId);
|
|
220
|
+
if (links.get(sessionId) === link)
|
|
221
|
+
links.delete(sessionId);
|
|
222
|
+
if (!ctx.session.parent)
|
|
223
|
+
closeTurn(sessionId);
|
|
224
|
+
});
|
|
225
|
+
const before = new Map;
|
|
226
|
+
const beforeCommand = (toolUseId) => {
|
|
227
|
+
const { root, via } = link();
|
|
228
|
+
let p = turnSnaps.get(root);
|
|
229
|
+
if (!p) {
|
|
230
|
+
const snapshots = snapshotsOf(ctx.home, root, ctx.cwd);
|
|
231
|
+
p = (async () => {
|
|
232
|
+
await closing.get(root);
|
|
233
|
+
const tree = await snapshots.take();
|
|
234
|
+
if (!tree)
|
|
235
|
+
return;
|
|
236
|
+
await snapshots.save({ toolUseId, tree, at: now(), ...via ? { via } : {} });
|
|
237
|
+
return { snapshots, toolUseId };
|
|
238
|
+
})().catch(() => {
|
|
239
|
+
return;
|
|
208
240
|
});
|
|
209
|
-
|
|
241
|
+
turnSnaps.set(root, p);
|
|
242
|
+
}
|
|
243
|
+
return p;
|
|
244
|
+
};
|
|
245
|
+
ctx.kernel.hook({
|
|
246
|
+
kind: "transform",
|
|
247
|
+
point: "tool.before",
|
|
248
|
+
order: 50,
|
|
249
|
+
optional: true,
|
|
250
|
+
timeoutMs: SNAPSHOT_TIMEOUT_MS,
|
|
251
|
+
fn: async (call) => {
|
|
252
|
+
const meta = ctx.session.tool(call.name)?.meta;
|
|
253
|
+
if (meta?.shell !== undefined && ctx.config.shell !== false) {
|
|
254
|
+
await beforeCommand(call.toolUseId);
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
if (meta?.writes === undefined)
|
|
258
|
+
return;
|
|
259
|
+
const p = call.input?.[meta.writes];
|
|
260
|
+
if (typeof p !== "string")
|
|
261
|
+
return;
|
|
262
|
+
const full = path.resolve(ctx.cwd, p);
|
|
263
|
+
before.set(call.toolUseId, { tool: call.name, path: full, before: await contentOf(full) });
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
ctx.kernel.hook({
|
|
267
|
+
kind: "transform",
|
|
268
|
+
point: "tool.after",
|
|
269
|
+
optional: true,
|
|
270
|
+
fn: async (out) => {
|
|
271
|
+
const snap = before.get(out.call.toolUseId);
|
|
272
|
+
if (!snap)
|
|
273
|
+
return;
|
|
274
|
+
before.delete(out.call.toolUseId);
|
|
275
|
+
if (out.isError)
|
|
276
|
+
return;
|
|
277
|
+
const after = await contentOf(snap.path);
|
|
278
|
+
if (after === snap.before)
|
|
279
|
+
return;
|
|
280
|
+
const { root, via } = link();
|
|
281
|
+
await checkpoints.save(root, { toolUseId: out.call.toolUseId, ...snap, afterHash: sha256(after ?? ""), ...via ? { via } : {} });
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
ctx.kernel.hook({
|
|
285
|
+
kind: "observe",
|
|
286
|
+
point: "event",
|
|
287
|
+
fn: (e) => {
|
|
288
|
+
if (e.type === "tool.decided")
|
|
289
|
+
decided.set(e.id, e.data.toolUseId);
|
|
290
|
+
if (e.type !== "turn.ended")
|
|
291
|
+
return;
|
|
292
|
+
before.clear();
|
|
293
|
+
decided.clear();
|
|
294
|
+
if (!ctx.session.parent)
|
|
295
|
+
return closeTurn(sessionId);
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
}
|
|
210
299
|
});
|