dsh-claude-style 0.21.5-dev.67 → 0.21.6-dev.68
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.en.md +8 -0
- package/README.md +8 -0
- package/docs/CHANGELOG.md +50 -0
- package/docs/STYLE.md +11 -8
- package/lib/assets/4482f7143b49.js.br +0 -0
- package/lib/assets/8d79b206742e.js.br +0 -0
- package/lib/assets/manifest.json +8 -8
- package/lib/client.js +14 -14
- package/lib/host/appearance-overlays.js +4 -2
- package/lib/host/brand.js +3 -0
- package/lib/host/branding-presets.js +3 -0
- package/lib/host/routes.js +8 -2
- package/lib/host/session-panel-context.js +94 -42
- package/lib/host/session-rewind-git.js +182 -0
- package/lib/host/session-rewind-routes.js +203 -0
- package/lib/host/session-rewind.js +557 -0
- package/lib/host/settings.js +1 -2
- package/lib/host/usage.js +1 -2
- package/lib/prices.json +89 -82
- package/package.json +6 -1
- package/lib/assets/29f5ce9ce094.js.br +0 -0
- package/lib/assets/9d9ee6937d69.js.br +0 -0
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
// packages/host/src/session-rewind.ts
|
|
2
|
+
import { createHash, randomUUID } from "node:crypto";
|
|
3
|
+
import {
|
|
4
|
+
chmod,
|
|
5
|
+
lstat,
|
|
6
|
+
mkdir,
|
|
7
|
+
readFile as readFileAsync,
|
|
8
|
+
readlink,
|
|
9
|
+
rename,
|
|
10
|
+
stat,
|
|
11
|
+
symlink,
|
|
12
|
+
unlink,
|
|
13
|
+
writeFile
|
|
14
|
+
} from "node:fs/promises";
|
|
15
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
16
|
+
import { dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
17
|
+
|
|
18
|
+
// packages/engine/node/session-events.ts
|
|
19
|
+
function eventsOf(events) {
|
|
20
|
+
return Array.isArray(events) ? events : null;
|
|
21
|
+
}
|
|
22
|
+
function unreadableLog(error) {
|
|
23
|
+
const failure = error;
|
|
24
|
+
return failure?.code === "SESSION_QUERY_CORRUPT_SESSION" || failure?.code === "SESSION_QUERY_SESSION_NOT_FOUND" ? failure : null;
|
|
25
|
+
}
|
|
26
|
+
function reportUnreadable(ctx, packageName, error, sessionId, label) {
|
|
27
|
+
ctx.logger?.warn?.(`${packageName}: session ${sessionId} left out of ${label}: ${unreadableLog(error)?.message}`);
|
|
28
|
+
}
|
|
29
|
+
function releaseObservation(observation) {
|
|
30
|
+
const named = observation.dispose;
|
|
31
|
+
if (typeof named === "function") named.call(observation);
|
|
32
|
+
else if (Symbol.dispose in observation) observation[Symbol.dispose]();
|
|
33
|
+
}
|
|
34
|
+
async function readEvents(ctx, packageName, sessionId, label) {
|
|
35
|
+
const query = ctx.get("sessionQuery");
|
|
36
|
+
if (query === null || query === void 0) return null;
|
|
37
|
+
try {
|
|
38
|
+
if (typeof query.observeSession === "function") {
|
|
39
|
+
const observation = await query.observeSession(sessionId, { projectionMode: "none" });
|
|
40
|
+
try {
|
|
41
|
+
return eventsOf(observation.events);
|
|
42
|
+
} finally {
|
|
43
|
+
releaseObservation(observation);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
if (typeof query.readSession !== "function") return null;
|
|
47
|
+
const snapshot = await query.readSession(sessionId);
|
|
48
|
+
return eventsOf(snapshot?.events);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
if (unreadableLog(error) === null) throw error;
|
|
51
|
+
reportUnreadable(ctx, packageName, error, sessionId, label);
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// packages/host/src/session-rewind.ts
|
|
57
|
+
import { pluginPath } from "./harness-home.js";
|
|
58
|
+
import {
|
|
59
|
+
applyTree,
|
|
60
|
+
diffTrees,
|
|
61
|
+
locateGit,
|
|
62
|
+
snapshotTree
|
|
63
|
+
} from "./session-rewind-git.js";
|
|
64
|
+
var DOCUMENT_VERSION = 1;
|
|
65
|
+
var MAX_CAPTURE_BYTES = 16 * 1024 * 1024;
|
|
66
|
+
var PLAN_LIFETIME_MS = 5 * 6e4;
|
|
67
|
+
var PLAN_LIMIT = 32;
|
|
68
|
+
function emptyDocument() {
|
|
69
|
+
return { version: DOCUMENT_VERSION, checkpoints: {}, links: {}, conversations: [] };
|
|
70
|
+
}
|
|
71
|
+
function isRecord(value) {
|
|
72
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
73
|
+
}
|
|
74
|
+
function validDocument(value) {
|
|
75
|
+
if (!isRecord(value) || value.version !== DOCUMENT_VERSION || !isRecord(value.checkpoints) || !isRecord(value.links) || !Array.isArray(value.conversations)) {
|
|
76
|
+
throw new Error("session rewind document has an unsupported shape");
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
function messageOf(error) {
|
|
81
|
+
return error instanceof Error ? error.message : String(error);
|
|
82
|
+
}
|
|
83
|
+
function sessionCwd(session) {
|
|
84
|
+
return typeof session?.header?.cwd === "string" && session.header.cwd !== "" ? session.header.cwd : null;
|
|
85
|
+
}
|
|
86
|
+
var SessionRewind = class {
|
|
87
|
+
constructor(ctx) {
|
|
88
|
+
this.ctx = ctx;
|
|
89
|
+
this.storage = pluginPath(ctx, "rewind");
|
|
90
|
+
this.documentPath = join(this.storage, "state.json");
|
|
91
|
+
this.document = existsSync(this.documentPath) ? validDocument(JSON.parse(readFileSync(this.documentPath, "utf8"))) : emptyDocument();
|
|
92
|
+
this.listen();
|
|
93
|
+
}
|
|
94
|
+
ctx;
|
|
95
|
+
storage;
|
|
96
|
+
documentPath;
|
|
97
|
+
document;
|
|
98
|
+
recorders = /* @__PURE__ */ new Map();
|
|
99
|
+
mutations = /* @__PURE__ */ new WeakMap();
|
|
100
|
+
plans = /* @__PURE__ */ new Map();
|
|
101
|
+
writes = Promise.resolve();
|
|
102
|
+
commitChain = Promise.resolve();
|
|
103
|
+
save() {
|
|
104
|
+
this.writes = this.writes.then(async () => {
|
|
105
|
+
await mkdir(dirname(this.documentPath), { recursive: true, mode: 448 });
|
|
106
|
+
const temporary = this.documentPath + "." + process.pid + ".tmp";
|
|
107
|
+
await writeFile(temporary, JSON.stringify(this.document), { mode: 384 });
|
|
108
|
+
await chmod(temporary, 384);
|
|
109
|
+
await rename(temporary, this.documentPath);
|
|
110
|
+
});
|
|
111
|
+
return this.writes;
|
|
112
|
+
}
|
|
113
|
+
liveSessions() {
|
|
114
|
+
const sessions = this.ctx.get("sessions");
|
|
115
|
+
return sessions !== null && typeof sessions === "object" ? sessions : null;
|
|
116
|
+
}
|
|
117
|
+
recorderFor(session, cwd) {
|
|
118
|
+
let recorder = this.recorders.get(session.id);
|
|
119
|
+
if (recorder === void 0) {
|
|
120
|
+
recorder = new SessionRecorder(this, session.id, cwd);
|
|
121
|
+
this.recorders.set(session.id, recorder);
|
|
122
|
+
}
|
|
123
|
+
return recorder;
|
|
124
|
+
}
|
|
125
|
+
listen() {
|
|
126
|
+
if (typeof this.ctx.on !== "function") return;
|
|
127
|
+
this.ctx.on("session/event", (session, event) => {
|
|
128
|
+
if (session === null || typeof session.id !== "string") return;
|
|
129
|
+
const recorder = this.recorders.get(session.id);
|
|
130
|
+
if (event.type === "turn/start" && typeof event.data?.turn === "number") {
|
|
131
|
+
const cwd = sessionCwd(session);
|
|
132
|
+
if (cwd !== null && session.header?.origin !== "subagent") this.recorderFor(session, cwd).start(event.data.turn);
|
|
133
|
+
} else if (event.type === "turn/end" && typeof event.data?.turn === "number") {
|
|
134
|
+
recorder?.end(event.data.turn);
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
this.ctx.on("session/disposed", (session) => {
|
|
138
|
+
this.recorders.delete(session.id);
|
|
139
|
+
});
|
|
140
|
+
this.ctx.on("tools/pre-execute", async (exec, next) => {
|
|
141
|
+
const recorder = exec.agent?.session === void 0 ? void 0 : this.recorders.get(exec.agent.session.id);
|
|
142
|
+
if (recorder !== void 0) {
|
|
143
|
+
this.mutations.set(exec, recorder);
|
|
144
|
+
await recorder.settled();
|
|
145
|
+
}
|
|
146
|
+
return next();
|
|
147
|
+
});
|
|
148
|
+
const capture = async (target, actor) => {
|
|
149
|
+
const recorder = actor === void 0 ? void 0 : this.mutations.get(actor);
|
|
150
|
+
if (recorder === void 0) return;
|
|
151
|
+
const fs = this.ctx.get("fs");
|
|
152
|
+
if (typeof fs?.processPath !== "function") throw new Error("session rewind: host fs.processPath is unavailable");
|
|
153
|
+
await recorder.capture(fs.processPath(target));
|
|
154
|
+
await recorder.settled();
|
|
155
|
+
};
|
|
156
|
+
for (const name of ["fs/write-intent", "fs/edit-intent", "fs/delete-intent"]) {
|
|
157
|
+
this.ctx.on(name, async (target, actor, next) => {
|
|
158
|
+
const intent = await next();
|
|
159
|
+
await capture(target, actor);
|
|
160
|
+
return intent;
|
|
161
|
+
}, { prepend: true });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
async putCheckpoint(checkpoint) {
|
|
165
|
+
const rows = this.document.checkpoints[checkpoint.sessionId] ?? [];
|
|
166
|
+
const next = rows.filter((row) => row.turn !== checkpoint.turn);
|
|
167
|
+
next.push(checkpoint);
|
|
168
|
+
next.sort((a, b) => a.turn - b.turn);
|
|
169
|
+
this.document.checkpoints[checkpoint.sessionId] = next;
|
|
170
|
+
await this.save();
|
|
171
|
+
}
|
|
172
|
+
checkpointStorage() {
|
|
173
|
+
return this.storage;
|
|
174
|
+
}
|
|
175
|
+
warn(message) {
|
|
176
|
+
this.ctx.logger?.warn?.("dsh-claude-style: session rewind: " + message);
|
|
177
|
+
}
|
|
178
|
+
checkpoint(sessionId, turn, seen = /* @__PURE__ */ new Set()) {
|
|
179
|
+
if (seen.has(sessionId)) throw new Error("session rewind: fork lineage contains a cycle");
|
|
180
|
+
seen.add(sessionId);
|
|
181
|
+
const direct = this.document.checkpoints[sessionId]?.find((row) => row.turn === turn);
|
|
182
|
+
if (direct !== void 0) return direct;
|
|
183
|
+
const link = this.document.links[sessionId];
|
|
184
|
+
return link === void 0 ? void 0 : this.checkpoint(link.sourceSessionId, turn, seen);
|
|
185
|
+
}
|
|
186
|
+
workspaceOf(stored) {
|
|
187
|
+
return {
|
|
188
|
+
root: stored.root,
|
|
189
|
+
gitDir: stored.gitDir,
|
|
190
|
+
repositoryObjects: stored.repositoryObjects,
|
|
191
|
+
objects: join(this.storage, "objects"),
|
|
192
|
+
env: {
|
|
193
|
+
GIT_OBJECT_DIRECTORY: join(this.storage, "objects"),
|
|
194
|
+
GIT_ALTERNATE_OBJECT_DIRECTORIES: stored.repositoryObjects
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
async capture(absolute) {
|
|
199
|
+
let info;
|
|
200
|
+
try {
|
|
201
|
+
info = await lstat(absolute);
|
|
202
|
+
} catch (error) {
|
|
203
|
+
if (error.code === "ENOENT") return { kind: "absent" };
|
|
204
|
+
throw error;
|
|
205
|
+
}
|
|
206
|
+
if (info.isSymbolicLink()) {
|
|
207
|
+
let directory = false;
|
|
208
|
+
try {
|
|
209
|
+
directory = (await stat(absolute)).isDirectory();
|
|
210
|
+
} catch (error) {
|
|
211
|
+
if (error.code !== "ENOENT") throw error;
|
|
212
|
+
}
|
|
213
|
+
return { kind: "symlink", target: await readlink(absolute), directory };
|
|
214
|
+
}
|
|
215
|
+
if (!info.isFile()) return { kind: "unsupported" };
|
|
216
|
+
if (info.size > MAX_CAPTURE_BYTES) return { kind: "oversized" };
|
|
217
|
+
const bytes = await readFileAsync(absolute);
|
|
218
|
+
const hash = createHash("sha256").update(bytes).digest("hex");
|
|
219
|
+
const path = join(this.storage, "captures", hash);
|
|
220
|
+
await mkdir(dirname(path), { recursive: true, mode: 448 });
|
|
221
|
+
try {
|
|
222
|
+
await writeFile(path, bytes, { flag: "wx", mode: 384 });
|
|
223
|
+
} catch (error) {
|
|
224
|
+
if (error.code !== "EEXIST") throw error;
|
|
225
|
+
}
|
|
226
|
+
return { kind: "file", hash, mode: info.mode & 511 };
|
|
227
|
+
}
|
|
228
|
+
async captureFor(recorder, absolute, turn) {
|
|
229
|
+
const capture = await this.capture(absolute);
|
|
230
|
+
recorder.remember(absolute, capture, turn);
|
|
231
|
+
}
|
|
232
|
+
async restoreCapture(absolute, capture) {
|
|
233
|
+
if (capture.kind === "oversized" || capture.kind === "unsupported") throw new Error(`no complete capture for ${absolute}`);
|
|
234
|
+
if (capture.kind === "absent") {
|
|
235
|
+
try {
|
|
236
|
+
const info = await lstat(absolute);
|
|
237
|
+
if (info.isDirectory() && !info.isSymbolicLink()) throw new Error(`refusing to remove directory ${absolute}`);
|
|
238
|
+
await unlink(absolute);
|
|
239
|
+
} catch (error) {
|
|
240
|
+
if (error.code !== "ENOENT") throw error;
|
|
241
|
+
}
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
await mkdir(dirname(absolute), { recursive: true, mode: 448 });
|
|
245
|
+
if (capture.kind === "symlink") {
|
|
246
|
+
try {
|
|
247
|
+
await unlink(absolute);
|
|
248
|
+
} catch (error) {
|
|
249
|
+
if (error.code !== "ENOENT") throw error;
|
|
250
|
+
}
|
|
251
|
+
await symlink(capture.target, absolute, capture.directory ? "junction" : "file");
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
if (capture.kind !== "file") throw new Error(`no complete capture for ${absolute}`);
|
|
255
|
+
const bytes = await readFileAsync(join(this.storage, "captures", capture.hash));
|
|
256
|
+
const temporary = absolute + ".dsh-claude-rewind-" + process.pid + ".tmp";
|
|
257
|
+
try {
|
|
258
|
+
await writeFile(temporary, bytes, { mode: capture.mode });
|
|
259
|
+
await chmod(temporary, capture.mode);
|
|
260
|
+
await rename(temporary, absolute);
|
|
261
|
+
} catch (error) {
|
|
262
|
+
try {
|
|
263
|
+
await unlink(temporary);
|
|
264
|
+
} catch (cleanup) {
|
|
265
|
+
if (cleanup.code !== "ENOENT") throw new AggregateError([error, cleanup], "session rewind: temporary cleanup failed");
|
|
266
|
+
}
|
|
267
|
+
throw error;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
conversationFor(sessionId) {
|
|
271
|
+
return this.document.conversations.find((conversation) => conversation.activeSessionId === sessionId) ?? this.document.conversations.find((conversation) => conversation.revisions.some((revision) => revision.sessionId === sessionId));
|
|
272
|
+
}
|
|
273
|
+
workspaceRegistry() {
|
|
274
|
+
const registry = this.ctx.get("workspaceRegistry");
|
|
275
|
+
return registry !== null && typeof registry === "object" ? registry : null;
|
|
276
|
+
}
|
|
277
|
+
async sessionKnown(sessionId) {
|
|
278
|
+
if (this.liveSessions()?.get?.(sessionId) !== void 0) return true;
|
|
279
|
+
const query = this.ctx.get("sessionQuery");
|
|
280
|
+
if (typeof query?.observeSession !== "function") return true;
|
|
281
|
+
try {
|
|
282
|
+
const observation = await query.observeSession(sessionId);
|
|
283
|
+
if (typeof observation.dispose === "function") observation.dispose();
|
|
284
|
+
else if (Symbol.dispose in observation) observation[Symbol.dispose]();
|
|
285
|
+
return true;
|
|
286
|
+
} catch (error) {
|
|
287
|
+
if (error.code === "SESSION_QUERY_SESSION_NOT_FOUND") return false;
|
|
288
|
+
throw error;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
/** Reconcile old plugin revisions with the native archive, including records written before archival existed. */
|
|
292
|
+
async reconcileArchiveState(pinSessionId = null) {
|
|
293
|
+
const registry = this.workspaceRegistry();
|
|
294
|
+
if (registry === null) return;
|
|
295
|
+
let changed = false;
|
|
296
|
+
for (const conversation of this.document.conversations) {
|
|
297
|
+
for (const revision of conversation.revisions) {
|
|
298
|
+
const active = revision.sessionId === conversation.activeSessionId;
|
|
299
|
+
const known = await this.sessionKnown(revision.sessionId);
|
|
300
|
+
if (!active && known && typeof registry.archiveSession === "function") {
|
|
301
|
+
try {
|
|
302
|
+
await registry.archiveSession(revision.sessionId, { stopActivity: true });
|
|
303
|
+
} catch (error) {
|
|
304
|
+
if (error.name !== "WorkspaceUnknownSessionError") throw error;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const archived = active ? registry.archivedSessionIds?.includes(revision.sessionId) === true : known;
|
|
308
|
+
if (revision.archived !== archived || revision.missing === !known) {
|
|
309
|
+
revision.archived = archived;
|
|
310
|
+
revision.missing = !known;
|
|
311
|
+
changed = true;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
if (pinSessionId !== null && typeof registry.pinSession === "function") await registry.pinSession(pinSessionId);
|
|
316
|
+
if (changed) await this.save();
|
|
317
|
+
}
|
|
318
|
+
async register(sourceSessionId, childSessionId, cutSeq, action, failed) {
|
|
319
|
+
const inheritPin = this.workspaceRegistry()?.pinnedSessionIds?.includes(sourceSessionId) === true;
|
|
320
|
+
let conversation = this.conversationFor(sourceSessionId);
|
|
321
|
+
if (conversation === void 0) {
|
|
322
|
+
conversation = {
|
|
323
|
+
id: sourceSessionId,
|
|
324
|
+
activeSessionId: sourceSessionId,
|
|
325
|
+
revisions: [{ sessionId: sourceSessionId, sourceSessionId: null, cutSeq: null, action: "origin", createdAt: Date.now(), status: "active" }]
|
|
326
|
+
};
|
|
327
|
+
this.document.conversations.push(conversation);
|
|
328
|
+
}
|
|
329
|
+
for (const revision of conversation.revisions) if (revision.status === "active") revision.status = "superseded";
|
|
330
|
+
if (!failed) conversation.activeSessionId = childSessionId;
|
|
331
|
+
else {
|
|
332
|
+
const active = conversation.revisions.find((revision) => revision.sessionId === conversation.activeSessionId);
|
|
333
|
+
if (active !== void 0) active.status = "active";
|
|
334
|
+
}
|
|
335
|
+
conversation.revisions.push({
|
|
336
|
+
sessionId: childSessionId,
|
|
337
|
+
sourceSessionId,
|
|
338
|
+
cutSeq,
|
|
339
|
+
action,
|
|
340
|
+
createdAt: Date.now(),
|
|
341
|
+
status: failed ? "failed" : "active"
|
|
342
|
+
});
|
|
343
|
+
this.document.links[childSessionId] = { sourceSessionId, cutSeq };
|
|
344
|
+
await this.save();
|
|
345
|
+
await this.reconcileArchiveState(failed || !inheritPin ? null : childSessionId);
|
|
346
|
+
return structuredClone(conversation);
|
|
347
|
+
}
|
|
348
|
+
async conversations() {
|
|
349
|
+
await this.commitChain;
|
|
350
|
+
await this.reconcileArchiveState();
|
|
351
|
+
return structuredClone(this.document.conversations);
|
|
352
|
+
}
|
|
353
|
+
async plan(sessionId, messageSeq, turn) {
|
|
354
|
+
await this.commitChain;
|
|
355
|
+
if (!Number.isSafeInteger(messageSeq) || messageSeq <= 0 || !Number.isSafeInteger(turn) || turn <= 0) {
|
|
356
|
+
throw new Error("session rewind: invalid message boundary");
|
|
357
|
+
}
|
|
358
|
+
await this.recorders.get(sessionId)?.settled();
|
|
359
|
+
const events = await readEvents(this.ctx, "dsh-claude-style", sessionId, "the rewind plan");
|
|
360
|
+
if (events === null) throw new Error("session rewind: session history is unavailable");
|
|
361
|
+
const turns = events.filter((event) => event.type === "turn/start" && typeof event.data?.turn === "number").map((event) => event.data?.turn);
|
|
362
|
+
if (!turns.includes(turn)) throw new Error(`session rewind: turn ${turn} does not exist`);
|
|
363
|
+
const headSeq = events.at(-1)?.seq ?? -1;
|
|
364
|
+
const checkpoints = turns.filter((candidate) => candidate >= turn).map((candidate) => this.checkpoint(sessionId, candidate));
|
|
365
|
+
const target = checkpoints[0];
|
|
366
|
+
let reason = target === void 0 ? "\u8FD9\u4E2A\u76EE\u6807\u8F6E\u6B21\u8FD8\u6CA1\u6709\u63D2\u4EF6\u68C0\u67E5\u70B9" : "";
|
|
367
|
+
if (checkpoints.some((checkpoint) => checkpoint === void 0)) reason = "\u90E8\u5206\u540E\u7EED\u8F6E\u6B21\u6CA1\u6709\u5B8C\u6574\u68C0\u67E5\u70B9";
|
|
368
|
+
if (checkpoints.some((checkpoint) => checkpoint?.complete === false)) {
|
|
369
|
+
reason = checkpoints.find((checkpoint) => checkpoint?.complete === false)?.reason || "\u68C0\u67E5\u70B9\u5305\u542B\u65E0\u6CD5\u6062\u590D\u7684\u6587\u4EF6";
|
|
370
|
+
}
|
|
371
|
+
if (target?.git != null && checkpoints.some((checkpoint) => checkpoint?.git?.root !== target.git?.root)) {
|
|
372
|
+
reason = "\u76EE\u6807\u4E4B\u540E\u7684\u5DE5\u4F5C\u76EE\u5F55\u53D1\u751F\u4E86\u53D8\u5316\uFF0C\u65E0\u6CD5\u5B89\u5168\u6062\u590D";
|
|
373
|
+
}
|
|
374
|
+
let workspace = null;
|
|
375
|
+
let currentTree = null;
|
|
376
|
+
let files = [];
|
|
377
|
+
if (reason === "" && target?.git != null) {
|
|
378
|
+
workspace = this.workspaceOf(target.git);
|
|
379
|
+
currentTree = await snapshotTree(workspace, this.storage);
|
|
380
|
+
files = await diffTrees(workspace, target.git.beforeTree, currentTree);
|
|
381
|
+
}
|
|
382
|
+
const listed = new Set(files.map((file) => file.path));
|
|
383
|
+
for (const checkpoint of checkpoints) {
|
|
384
|
+
for (const absolute of Object.keys(checkpoint?.captures ?? {})) {
|
|
385
|
+
const inside = target?.git == null ? "" : relative(target.git.root, absolute);
|
|
386
|
+
const path = target?.git != null && inside !== "" && !isAbsolute(inside) && !inside.startsWith(".." + sep) ? inside.split(sep).join("/") : absolute;
|
|
387
|
+
if (!listed.has(path)) {
|
|
388
|
+
files.push({ path, status: "captured" });
|
|
389
|
+
listed.add(path);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
const now = Date.now();
|
|
394
|
+
for (const [id2, plan2] of this.plans) {
|
|
395
|
+
if (now - plan2.createdAt > PLAN_LIFETIME_MS || this.plans.size >= PLAN_LIMIT) this.plans.delete(id2);
|
|
396
|
+
}
|
|
397
|
+
const id = randomUUID();
|
|
398
|
+
const plan = {
|
|
399
|
+
id,
|
|
400
|
+
sessionId,
|
|
401
|
+
atSeq: messageSeq - 1,
|
|
402
|
+
turn,
|
|
403
|
+
laterTurns: turns.filter((candidate) => candidate > turn).length,
|
|
404
|
+
restoreAvailable: reason === "",
|
|
405
|
+
reason,
|
|
406
|
+
files,
|
|
407
|
+
createdAt: now,
|
|
408
|
+
headSeq,
|
|
409
|
+
checkpoints: checkpoints.filter((checkpoint) => checkpoint !== void 0),
|
|
410
|
+
workspace,
|
|
411
|
+
currentTree
|
|
412
|
+
};
|
|
413
|
+
this.plans.set(id, plan);
|
|
414
|
+
const { createdAt: _createdAt, headSeq: _headSeq, checkpoints: _checkpoints, workspace: _workspace, currentTree: _currentTree, ...answer } = plan;
|
|
415
|
+
return answer;
|
|
416
|
+
}
|
|
417
|
+
async commit(request) {
|
|
418
|
+
const run = this.commitChain.then(() => this.commitNow(request));
|
|
419
|
+
this.commitChain = run.then(() => void 0, () => void 0);
|
|
420
|
+
return run;
|
|
421
|
+
}
|
|
422
|
+
async commitNow(request) {
|
|
423
|
+
const plan = this.plans.get(request.planId);
|
|
424
|
+
if (plan === void 0 || Date.now() - plan.createdAt > PLAN_LIFETIME_MS) throw new Error("session rewind: confirmation expired");
|
|
425
|
+
this.plans.delete(request.planId);
|
|
426
|
+
if (!/^[A-Za-z0-9._\-:@]{1,200}$/.test(request.childSessionId)) throw new Error("session rewind: invalid child session id");
|
|
427
|
+
const events = await readEvents(this.ctx, "dsh-claude-style", plan.sessionId, "the rewind commit");
|
|
428
|
+
const headSeq = events?.at(-1)?.seq ?? -1;
|
|
429
|
+
if (events === null || headSeq !== plan.headSeq) {
|
|
430
|
+
await this.register(plan.sessionId, request.childSessionId, plan.atSeq, request.action, true);
|
|
431
|
+
throw new Error("session rewind: the session changed after confirmation");
|
|
432
|
+
}
|
|
433
|
+
if (request.restore && !plan.restoreAvailable) throw new Error("session rewind: code restoration is unavailable");
|
|
434
|
+
try {
|
|
435
|
+
if (request.restore) await this.restore(plan);
|
|
436
|
+
} catch (error) {
|
|
437
|
+
await this.register(plan.sessionId, request.childSessionId, plan.atSeq, request.action, true);
|
|
438
|
+
throw error;
|
|
439
|
+
}
|
|
440
|
+
return this.register(plan.sessionId, request.childSessionId, plan.atSeq, request.action, false);
|
|
441
|
+
}
|
|
442
|
+
async restore(plan) {
|
|
443
|
+
if (plan.workspace !== null && plan.currentTree !== null) {
|
|
444
|
+
const currentTree = await snapshotTree(plan.workspace, this.storage);
|
|
445
|
+
if (currentTree !== plan.currentTree) throw new Error("session rewind: the work tree changed after confirmation");
|
|
446
|
+
}
|
|
447
|
+
const paths = [...new Set(plan.checkpoints.flatMap((checkpoint) => Object.keys(checkpoint.captures)))];
|
|
448
|
+
const safety = /* @__PURE__ */ new Map();
|
|
449
|
+
for (const path of paths) {
|
|
450
|
+
const capture = await this.capture(path);
|
|
451
|
+
if (capture.kind === "oversized" || capture.kind === "unsupported") throw new Error(`cannot protect current file ${path}`);
|
|
452
|
+
safety.set(path, capture);
|
|
453
|
+
}
|
|
454
|
+
const target = plan.checkpoints.find((checkpoint) => checkpoint.turn === plan.turn);
|
|
455
|
+
try {
|
|
456
|
+
if (plan.workspace !== null && plan.currentTree !== null && target?.git != null) {
|
|
457
|
+
await applyTree(plan.workspace, this.storage, target.git.beforeTree, plan.currentTree);
|
|
458
|
+
}
|
|
459
|
+
const ordered = [...plan.checkpoints].sort((a, b) => b.turn - a.turn);
|
|
460
|
+
for (const checkpoint of ordered) {
|
|
461
|
+
for (const [path, capture] of Object.entries(checkpoint.captures)) await this.restoreCapture(path, capture);
|
|
462
|
+
}
|
|
463
|
+
} catch (error) {
|
|
464
|
+
try {
|
|
465
|
+
for (const [path, capture] of safety) await this.restoreCapture(path, capture);
|
|
466
|
+
if (plan.workspace !== null && plan.currentTree !== null && target?.git != null) {
|
|
467
|
+
await applyTree(plan.workspace, this.storage, plan.currentTree, target.git.beforeTree);
|
|
468
|
+
}
|
|
469
|
+
} catch (rollback) {
|
|
470
|
+
throw new AggregateError([error, rollback], "session rewind: restoration and its safety restoration both failed");
|
|
471
|
+
}
|
|
472
|
+
throw error;
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
var SessionRecorder = class {
|
|
477
|
+
constructor(owner, sessionId, cwd) {
|
|
478
|
+
this.owner = owner;
|
|
479
|
+
this.sessionId = sessionId;
|
|
480
|
+
this.cwd = cwd;
|
|
481
|
+
}
|
|
482
|
+
owner;
|
|
483
|
+
sessionId;
|
|
484
|
+
cwd;
|
|
485
|
+
chain = Promise.resolve();
|
|
486
|
+
active = null;
|
|
487
|
+
start(turn) {
|
|
488
|
+
const state = {
|
|
489
|
+
turn,
|
|
490
|
+
cwd: this.cwd,
|
|
491
|
+
captures: /* @__PURE__ */ new Map(),
|
|
492
|
+
git: null,
|
|
493
|
+
beforeTree: null,
|
|
494
|
+
complete: true,
|
|
495
|
+
reason: ""
|
|
496
|
+
};
|
|
497
|
+
this.active = state;
|
|
498
|
+
this.enqueue(async () => {
|
|
499
|
+
try {
|
|
500
|
+
state.git = await locateGit(state.cwd, this.owner.checkpointStorage());
|
|
501
|
+
if (state.git !== null) state.beforeTree = await snapshotTree(state.git, this.owner.checkpointStorage());
|
|
502
|
+
} catch (error) {
|
|
503
|
+
state.complete = false;
|
|
504
|
+
state.reason = messageOf(error);
|
|
505
|
+
this.owner.warn(`turn ${turn} baseline unavailable: ${state.reason}`);
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
end(turn) {
|
|
510
|
+
const state = this.active;
|
|
511
|
+
if (state === null || state.turn !== turn) return;
|
|
512
|
+
this.enqueue(async () => {
|
|
513
|
+
await this.owner.putCheckpoint({
|
|
514
|
+
sessionId: this.sessionId,
|
|
515
|
+
turn,
|
|
516
|
+
cwd: state.cwd,
|
|
517
|
+
createdAt: Date.now(),
|
|
518
|
+
git: state.git !== null && state.beforeTree !== null ? {
|
|
519
|
+
cwd: state.cwd,
|
|
520
|
+
root: state.git.root,
|
|
521
|
+
gitDir: state.git.gitDir,
|
|
522
|
+
repositoryObjects: state.git.repositoryObjects,
|
|
523
|
+
beforeTree: state.beforeTree
|
|
524
|
+
} : null,
|
|
525
|
+
captures: Object.fromEntries(state.captures),
|
|
526
|
+
complete: state.complete && [...state.captures.values()].every((capture) => capture.kind !== "oversized" && capture.kind !== "unsupported"),
|
|
527
|
+
reason: state.reason
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
}
|
|
531
|
+
async capture(path) {
|
|
532
|
+
await this.enqueue(async () => {
|
|
533
|
+
const state = this.active;
|
|
534
|
+
if (state === null || state.captures.has(path)) return;
|
|
535
|
+
try {
|
|
536
|
+
await this.owner.captureFor(this, path, state.turn);
|
|
537
|
+
} catch (error) {
|
|
538
|
+
state.complete = false;
|
|
539
|
+
state.reason = messageOf(error);
|
|
540
|
+
this.owner.warn(`capture failed for ${path}: ${state.reason}`);
|
|
541
|
+
}
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
remember(path, capture, turn) {
|
|
545
|
+
if (this.active?.turn === turn) this.active.captures.set(path, capture);
|
|
546
|
+
}
|
|
547
|
+
settled() {
|
|
548
|
+
return this.chain;
|
|
549
|
+
}
|
|
550
|
+
enqueue(task) {
|
|
551
|
+
this.chain = this.chain.then(task);
|
|
552
|
+
return this.chain;
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
export {
|
|
556
|
+
SessionRewind
|
|
557
|
+
};
|
package/lib/host/settings.js
CHANGED
|
@@ -268,7 +268,6 @@ var PREFS = {
|
|
|
268
268
|
statsFooterTotal: { type: "boolean", default: true },
|
|
269
269
|
/** How the footer's token figures read: the grouped whole numbers, or K/M/B/T units with one decimal place. */
|
|
270
270
|
statsFooterTokenFormat: { type: "choice", values: FOOTER_TOKEN_FORMATS, default: FOOTER_TOKEN_DIGITS },
|
|
271
|
-
statsFooterContext: { type: "boolean", default: true },
|
|
272
271
|
/** Which official price table the session's cost follows, or off for no figure at all. */
|
|
273
272
|
billingCurrency: { type: "choice", values: BILLING_CHOICES, default: CURRENCY_CNY },
|
|
274
273
|
/** Custom `model|input|cacheRead|output` rows per million tokens; blank buckets inherit official rates. */
|
|
@@ -276,7 +275,7 @@ var PREFS = {
|
|
|
276
275
|
workspaceView: { type: "boolean", default: true },
|
|
277
276
|
sidebarSearch: { type: "boolean", default: true },
|
|
278
277
|
searchStyle: { type: "choice", values: SEARCH_STYLES, default: SEARCH_STYLE_STANDALONE, legacy: { overlay: SEARCH_STYLE_STANDALONE } },
|
|
279
|
-
/** Whether resting the pointer on the collapsed sidebar's toggle opens the sidebar over the page. */
|
|
278
|
+
/** Whether resting the pointer on the collapsed sidebar's toggle or the window's left edge opens the sidebar over the page. */
|
|
280
279
|
sidebarPeek: { type: "boolean", default: true },
|
|
281
280
|
/** Whether the panel list carries the More menu's hidden entries and its Edit sidebar dialog. */
|
|
282
281
|
sidebarEntries: { type: "boolean", default: true },
|
package/lib/host/usage.js
CHANGED
|
@@ -268,7 +268,6 @@ var PREFS = {
|
|
|
268
268
|
statsFooterTotal: { type: "boolean", default: true },
|
|
269
269
|
/** How the footer's token figures read: the grouped whole numbers, or K/M/B/T units with one decimal place. */
|
|
270
270
|
statsFooterTokenFormat: { type: "choice", values: FOOTER_TOKEN_FORMATS, default: FOOTER_TOKEN_DIGITS },
|
|
271
|
-
statsFooterContext: { type: "boolean", default: true },
|
|
272
271
|
/** Which official price table the session's cost follows, or off for no figure at all. */
|
|
273
272
|
billingCurrency: { type: "choice", values: BILLING_CHOICES, default: CURRENCY_CNY },
|
|
274
273
|
/** Custom `model|input|cacheRead|output` rows per million tokens; blank buckets inherit official rates. */
|
|
@@ -276,7 +275,7 @@ var PREFS = {
|
|
|
276
275
|
workspaceView: { type: "boolean", default: true },
|
|
277
276
|
sidebarSearch: { type: "boolean", default: true },
|
|
278
277
|
searchStyle: { type: "choice", values: SEARCH_STYLES, default: SEARCH_STYLE_STANDALONE, legacy: { overlay: SEARCH_STYLE_STANDALONE } },
|
|
279
|
-
/** Whether resting the pointer on the collapsed sidebar's toggle opens the sidebar over the page. */
|
|
278
|
+
/** Whether resting the pointer on the collapsed sidebar's toggle or the window's left edge opens the sidebar over the page. */
|
|
280
279
|
sidebarPeek: { type: "boolean", default: true },
|
|
281
280
|
/** Whether the panel list carries the More menu's hidden entries and its Edit sidebar dialog. */
|
|
282
281
|
sidebarEntries: { type: "boolean", default: true },
|