dsh-agent-enhancer 0.3.1 → 0.3.2
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 +5 -5
- package/README.md +5 -5
- package/lib/client/dsh/settings.d.ts.map +1 -1
- package/lib/client/index.d.ts.map +1 -1
- package/lib/client/locales.d.ts +37 -27
- package/lib/client/locales.d.ts.map +1 -1
- package/lib/client/rollback/controller.d.ts +1 -0
- package/lib/client/rollback/controller.d.ts.map +1 -1
- package/lib/client/rollback/cut-cache.d.ts +13 -0
- package/lib/client/rollback/cut-cache.d.ts.map +1 -0
- package/lib/client/rollback/modal-host.d.ts.map +1 -1
- package/lib/client/rollback/remote.d.ts +2 -1
- package/lib/client/rollback/remote.d.ts.map +1 -1
- package/lib/client/rollback/scope.d.ts +3 -1
- package/lib/client/rollback/scope.d.ts.map +1 -1
- package/lib/client/rollback/session-stats-dock.d.ts +1 -1
- package/lib/client/rollback/session-stats-dock.d.ts.map +1 -1
- package/lib/client/rollback/session-stats-filter.d.ts +9 -8
- package/lib/client/rollback/session-stats-filter.d.ts.map +1 -1
- package/lib/client/rollback/session-stats-meter.d.ts +4 -2
- package/lib/client/rollback/session-stats-meter.d.ts.map +1 -1
- package/lib/client/rollback/timeline-panel.d.ts +2 -4
- package/lib/client/rollback/timeline-panel.d.ts.map +1 -1
- package/lib/client/rollback/timeline-tab.d.ts +3 -2
- package/lib/client/rollback/timeline-tab.d.ts.map +1 -1
- package/lib/client/rollback/transcript-filter.d.ts +16 -6
- package/lib/client/rollback/transcript-filter.d.ts.map +1 -1
- package/lib/client/rollback/transcript.d.ts +15 -9
- package/lib/client/rollback/transcript.d.ts.map +1 -1
- package/lib/client/rollback/types.d.ts +4 -1
- package/lib/client/rollback/types.d.ts.map +1 -1
- package/lib/client/rollback/user-node.d.ts.map +1 -1
- package/lib/client.js +449 -322
- package/lib/client.js.map +1 -1
- package/lib/config.d.ts +3 -1
- package/lib/config.d.ts.map +1 -1
- package/lib/host/auto-route/classifier.d.ts.map +1 -1
- package/lib/host/checkpoint/chain-files.d.ts +35 -0
- package/lib/host/checkpoint/chain-files.d.ts.map +1 -0
- package/lib/host/checkpoint/cut-filter.d.ts +9 -0
- package/lib/host/checkpoint/cut-filter.d.ts.map +1 -0
- package/lib/host/checkpoint/cut-live.d.ts +13 -0
- package/lib/host/checkpoint/cut-live.d.ts.map +1 -0
- package/lib/host/checkpoint/cut-store.d.ts +30 -0
- package/lib/host/checkpoint/cut-store.d.ts.map +1 -0
- package/lib/host/checkpoint/dsh.d.ts.map +1 -1
- package/lib/host/checkpoint/files.d.ts.map +1 -1
- package/lib/host/checkpoint/live.d.ts +1 -0
- package/lib/host/checkpoint/live.d.ts.map +1 -1
- package/lib/host/checkpoint/service.d.ts +44 -5
- package/lib/host/checkpoint/service.d.ts.map +1 -1
- package/lib/host/checkpoint/store.d.ts +6 -0
- package/lib/host/checkpoint/store.d.ts.map +1 -1
- package/lib/host/index.d.ts +2 -1
- package/lib/host/index.d.ts.map +1 -1
- package/lib/host/model.d.ts.map +1 -1
- package/lib/host/rpc.d.ts +4 -0
- package/lib/host/rpc.d.ts.map +1 -1
- package/lib/identity.d.ts +1 -1
- package/lib/index.js +1018 -1071
- package/lib/index.js.map +4 -4
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// src/host/index.ts
|
|
2
2
|
import { existsSync, mkdirSync, readdirSync, renameSync, rmdirSync } from "node:fs";
|
|
3
3
|
import { homedir as homedir2 } from "node:os";
|
|
4
|
-
import { join as
|
|
5
|
-
import
|
|
4
|
+
import { join as join9 } from "node:path";
|
|
5
|
+
import z5 from "@deepseek-ai/schemastery";
|
|
6
6
|
import { RemoteError, TypertRemoteService } from "@deepseek-ai/dsh-typert-protocol";
|
|
7
7
|
|
|
8
8
|
// src/host/auto-route/sentinel.ts
|
|
@@ -160,7 +160,7 @@ var AuxiliaryError = class extends Error {
|
|
|
160
160
|
// src/identity.ts
|
|
161
161
|
var PACKAGE_NAME = "dsh-agent-enhancer";
|
|
162
162
|
var PLUGIN_ID = "agent-enhancer";
|
|
163
|
-
var PACKAGE_VERSION = "0.3.
|
|
163
|
+
var PACKAGE_VERSION = "0.3.2";
|
|
164
164
|
var LEGACY_PACKAGE_NAME = "dsh-composer-enhancer";
|
|
165
165
|
var ROLLBACK_PREFIX = `${PACKAGE_NAME}:rollback:`;
|
|
166
166
|
var UNDO_PREFIX = `${PACKAGE_NAME}:undo:`;
|
|
@@ -267,21 +267,25 @@ var DshCheckpointSessions = class {
|
|
|
267
267
|
adapters = /* @__PURE__ */ new Map();
|
|
268
268
|
resolveSession = (sessionId) => {
|
|
269
269
|
const agent = this.ctx.agents.get(sessionId);
|
|
270
|
-
|
|
270
|
+
const stored = this.ctx.get("sessions").get(sessionId);
|
|
271
|
+
if (agent === void 0 || stored === void 0 || stored.id !== agent.session.id) {
|
|
271
272
|
this.adapters.delete(sessionId);
|
|
272
273
|
return void 0;
|
|
273
274
|
}
|
|
274
275
|
const cached = this.adapters.get(sessionId);
|
|
275
276
|
if (cached?.agent === agent) return cached.adapter;
|
|
276
|
-
const workspaceRoot = agent.session.header.cwd ?? "";
|
|
277
277
|
const adapter = {
|
|
278
|
-
session
|
|
279
|
-
|
|
278
|
+
get session() {
|
|
279
|
+
return agent.session;
|
|
280
|
+
},
|
|
281
|
+
get workspaceRoot() {
|
|
282
|
+
return agent.session.header.cwd ?? "";
|
|
283
|
+
},
|
|
280
284
|
get status() {
|
|
281
285
|
return agent.status;
|
|
282
286
|
},
|
|
283
287
|
get canRestore() {
|
|
284
|
-
return
|
|
288
|
+
return (agent.session.header.cwd ?? "") !== "";
|
|
285
289
|
},
|
|
286
290
|
readEvents: () => agent.session.snapshotEvents(),
|
|
287
291
|
runMaintenance: async (task) => {
|
|
@@ -291,10 +295,11 @@ var DshCheckpointSessions = class {
|
|
|
291
295
|
},
|
|
292
296
|
synchronize: async (signal) => {
|
|
293
297
|
signal.throwIfAborted();
|
|
294
|
-
|
|
295
|
-
|
|
298
|
+
const session = agent.session;
|
|
299
|
+
this.ctx.sessionProjections.snapshot(session);
|
|
300
|
+
await this.ctx.get("sessions").flush(session);
|
|
296
301
|
signal.throwIfAborted();
|
|
297
|
-
this.ctx.sessionProjections.snapshot(
|
|
302
|
+
this.ctx.sessionProjections.snapshot(session);
|
|
298
303
|
}
|
|
299
304
|
};
|
|
300
305
|
this.adapters.set(sessionId, { agent, adapter });
|
|
@@ -809,7 +814,6 @@ var CheckpointFiles = class {
|
|
|
809
814
|
await journal.compensate();
|
|
810
815
|
throw new CheckpointFileError("FILE_RESTORE_FAILED");
|
|
811
816
|
}
|
|
812
|
-
if (!result.synchronized) throw new CheckpointFileError("FILE_RESTORE_FAILED");
|
|
813
817
|
try {
|
|
814
818
|
await journal.complete("completed");
|
|
815
819
|
return journal;
|
|
@@ -1109,10 +1113,11 @@ var DshCheckpointCapture = class {
|
|
|
1109
1113
|
this.options = options;
|
|
1110
1114
|
this.disposers.push(
|
|
1111
1115
|
ctx.on("agent/pre-step", async (payload, next) => {
|
|
1112
|
-
const
|
|
1116
|
+
const sessionId = payload.agent.session.id;
|
|
1117
|
+
const current = this.turns.get(sessionId);
|
|
1113
1118
|
const startsTurn = current === void 0 || current.turnStart !== null && current.turnStart.data.turn !== payload.turn;
|
|
1114
1119
|
if (!this.closed && startsTurn && payload.agent.session.header.origin !== "subagent") {
|
|
1115
|
-
this.turns.set(
|
|
1120
|
+
this.turns.set(sessionId, {
|
|
1116
1121
|
agent: payload.agent,
|
|
1117
1122
|
prompt: payload.messages.find((message) => message.source.kind === "user") ?? null,
|
|
1118
1123
|
turnStart: null,
|
|
@@ -1163,7 +1168,7 @@ var DshCheckpointCapture = class {
|
|
|
1163
1168
|
// this flag the host plugin's listener is filtered out and never runs.
|
|
1164
1169
|
ctx.on("tools/execute", async (exec, next) => {
|
|
1165
1170
|
if (exec.agent === void 0) return next();
|
|
1166
|
-
const current = this.turns.get(exec.agent.id);
|
|
1171
|
+
const current = this.turns.get(exec.agent.session.id);
|
|
1167
1172
|
this.actors.set(exec, exec.agent);
|
|
1168
1173
|
try {
|
|
1169
1174
|
if (current !== void 0 && isFileMutationTool(exec.name)) {
|
|
@@ -1216,7 +1221,7 @@ var DshCheckpointCapture = class {
|
|
|
1216
1221
|
if (!this.closed) await this.options.store.save(checkpoint);
|
|
1217
1222
|
}
|
|
1218
1223
|
},
|
|
1219
|
-
sessionId: turn.agent.id,
|
|
1224
|
+
sessionId: turn.agent.session.id,
|
|
1220
1225
|
session: {
|
|
1221
1226
|
format: turn.agent.session.header.version,
|
|
1222
1227
|
preset: turn.agent.session.header.agentPreset ?? null
|
|
@@ -1274,12 +1279,15 @@ var DshCheckpointCapture = class {
|
|
|
1274
1279
|
}
|
|
1275
1280
|
}
|
|
1276
1281
|
}
|
|
1282
|
+
turnOf(agent) {
|
|
1283
|
+
return this.turns.get(agent.session.id);
|
|
1284
|
+
}
|
|
1277
1285
|
async captureToolPath(agent, args) {
|
|
1278
1286
|
const path = filePathFromToolArgs(args);
|
|
1279
1287
|
if (path === null) return;
|
|
1280
1288
|
const workspaceRoot = agent.session.header.cwd;
|
|
1281
1289
|
if (workspaceRoot === void 0) {
|
|
1282
|
-
const turn = this.
|
|
1290
|
+
const turn = this.turnOf(agent);
|
|
1283
1291
|
if (turn !== void 0) turn.incomplete = true;
|
|
1284
1292
|
return;
|
|
1285
1293
|
}
|
|
@@ -1287,7 +1295,7 @@ var DshCheckpointCapture = class {
|
|
|
1287
1295
|
const absolutePath2 = isAbsolute2(path) ? path : join3(workspaceRoot, path);
|
|
1288
1296
|
await this.captureAbsolutePath(agent, absolutePath2);
|
|
1289
1297
|
} catch {
|
|
1290
|
-
const turn = this.
|
|
1298
|
+
const turn = this.turnOf(agent);
|
|
1291
1299
|
if (turn !== void 0) turn.incomplete = true;
|
|
1292
1300
|
}
|
|
1293
1301
|
}
|
|
@@ -1300,12 +1308,12 @@ var DshCheckpointCapture = class {
|
|
|
1300
1308
|
const absolutePath2 = this.ctx.fs.processPath(target);
|
|
1301
1309
|
await this.captureAbsolutePath(agent, absolutePath2);
|
|
1302
1310
|
} catch {
|
|
1303
|
-
const turn = this.
|
|
1311
|
+
const turn = this.turnOf(agent);
|
|
1304
1312
|
if (turn !== void 0) turn.incomplete = true;
|
|
1305
1313
|
}
|
|
1306
1314
|
}
|
|
1307
1315
|
async captureAbsolutePath(agent, absolutePath2) {
|
|
1308
|
-
const turn = this.
|
|
1316
|
+
const turn = this.turnOf(agent);
|
|
1309
1317
|
if (turn === void 0) return;
|
|
1310
1318
|
const workspaceRoot = agent.session.header.cwd;
|
|
1311
1319
|
if (workspaceRoot === void 0) {
|
|
@@ -1397,7 +1405,7 @@ var DshCheckpointCaptureSwitch = class {
|
|
|
1397
1405
|
};
|
|
1398
1406
|
|
|
1399
1407
|
// src/host/checkpoint/service.ts
|
|
1400
|
-
import { randomUUID as
|
|
1408
|
+
import { randomUUID as randomUUID4 } from "node:crypto";
|
|
1401
1409
|
import { MessageId } from "@deepseek-ai/dsh-llm";
|
|
1402
1410
|
import { SessionSeq } from "@deepseek-ai/dsh-session";
|
|
1403
1411
|
|
|
@@ -1439,6 +1447,223 @@ function blockingUnavailable(unavailable, scope, options = {}) {
|
|
|
1439
1447
|
return unavailable.filter((reason) => !soft.has(reason));
|
|
1440
1448
|
}
|
|
1441
1449
|
|
|
1450
|
+
// src/host/checkpoint/chain-files.ts
|
|
1451
|
+
function netFileChange(earliest, latest) {
|
|
1452
|
+
if (earliest.beforeText === null && latest.afterText === null) return null;
|
|
1453
|
+
if (earliest.beforeText === null) return "create";
|
|
1454
|
+
if (latest.afterText === null) return "delete";
|
|
1455
|
+
return "modify";
|
|
1456
|
+
}
|
|
1457
|
+
function chainTrackedFiles(checkpoints, fromTurnOrRange, untilTurnExclusive) {
|
|
1458
|
+
const range = typeof fromTurnOrRange === "number" ? { fromTurn: fromTurnOrRange, untilTurnExclusive } : fromTurnOrRange;
|
|
1459
|
+
if (!Number.isFinite(range.fromTurn) && range.fromAtSeq === void 0) return [];
|
|
1460
|
+
const inRange = checkpoints.filter((checkpoint) => {
|
|
1461
|
+
if (range.fromAtSeq !== void 0) {
|
|
1462
|
+
if (checkpoint.atSeq < range.fromAtSeq) return false;
|
|
1463
|
+
if (range.untilAtSeqExclusive !== void 0 && checkpoint.atSeq >= range.untilAtSeqExclusive) {
|
|
1464
|
+
return false;
|
|
1465
|
+
}
|
|
1466
|
+
return true;
|
|
1467
|
+
}
|
|
1468
|
+
return checkpoint.turn >= range.fromTurn && (range.untilTurnExclusive === void 0 || checkpoint.turn < range.untilTurnExclusive);
|
|
1469
|
+
}).slice().sort((left, right) => left.atSeq - right.atSeq || left.turn - right.turn);
|
|
1470
|
+
const byPath = /* @__PURE__ */ new Map();
|
|
1471
|
+
for (const checkpoint of inRange) {
|
|
1472
|
+
for (const file of checkpoint.files) {
|
|
1473
|
+
const previous = byPath.get(file.path);
|
|
1474
|
+
if (previous === void 0) {
|
|
1475
|
+
byPath.set(file.path, { earliest: file, latest: file });
|
|
1476
|
+
continue;
|
|
1477
|
+
}
|
|
1478
|
+
byPath.set(file.path, { earliest: previous.earliest, latest: file });
|
|
1479
|
+
}
|
|
1480
|
+
}
|
|
1481
|
+
const chained = [];
|
|
1482
|
+
for (const { earliest, latest } of byPath.values()) {
|
|
1483
|
+
const change = netFileChange(earliest, latest);
|
|
1484
|
+
if (change === null) continue;
|
|
1485
|
+
chained.push({
|
|
1486
|
+
path: earliest.path,
|
|
1487
|
+
change,
|
|
1488
|
+
beforeText: earliest.beforeText,
|
|
1489
|
+
beforeHash: earliest.beforeHash,
|
|
1490
|
+
afterText: latest.afterText,
|
|
1491
|
+
afterHash: latest.afterHash
|
|
1492
|
+
});
|
|
1493
|
+
}
|
|
1494
|
+
return chained;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
// src/host/checkpoint/cut-store.ts
|
|
1498
|
+
import { createHash as createHash2, randomUUID as randomUUID3 } from "node:crypto";
|
|
1499
|
+
import { chmod as chmod2, mkdir as mkdir2, readFile as readFile2, rename as rename2, unlink as unlink2, writeFile as writeFile2 } from "node:fs/promises";
|
|
1500
|
+
import { basename as basename2, join as join4 } from "node:path";
|
|
1501
|
+
import { z } from "zod";
|
|
1502
|
+
var SCHEMA_VERSION = 1;
|
|
1503
|
+
var queues = /* @__PURE__ */ new Map();
|
|
1504
|
+
async function serialize2(key, operation) {
|
|
1505
|
+
const previous = queues.get(key) ?? Promise.resolve();
|
|
1506
|
+
const result = previous.then(operation);
|
|
1507
|
+
const tail = result.then(() => void 0, () => void 0);
|
|
1508
|
+
queues.set(key, tail);
|
|
1509
|
+
try {
|
|
1510
|
+
return await result;
|
|
1511
|
+
} finally {
|
|
1512
|
+
if (queues.get(key) === tail) queues.delete(key);
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
var sessionIdSchema = z.string().min(1).transform((value) => value);
|
|
1516
|
+
var cutSchema = z.lazy(() => z.object({
|
|
1517
|
+
hideTurnsFrom: z.number().int().positive(),
|
|
1518
|
+
cutSeq: z.number().int().nonnegative(),
|
|
1519
|
+
restoredAtSeq: z.number().int().nonnegative(),
|
|
1520
|
+
draftPrompt: z.string(),
|
|
1521
|
+
scope: z.enum(["conversation", "conversation-and-files"]),
|
|
1522
|
+
journalId: z.string().min(1).optional(),
|
|
1523
|
+
previous: cutSchema.nullable()
|
|
1524
|
+
}).strict());
|
|
1525
|
+
var fileSchema = z.object({
|
|
1526
|
+
schemaVersion: z.literal(SCHEMA_VERSION),
|
|
1527
|
+
sessionId: sessionIdSchema,
|
|
1528
|
+
cut: cutSchema.nullable()
|
|
1529
|
+
}).strict();
|
|
1530
|
+
function digest2(value) {
|
|
1531
|
+
return createHash2("sha256").update(value).digest("hex");
|
|
1532
|
+
}
|
|
1533
|
+
function isMissing2(error) {
|
|
1534
|
+
return typeof error === "object" && error !== null && error.code === "ENOENT";
|
|
1535
|
+
}
|
|
1536
|
+
async function removeIfPresent2(path) {
|
|
1537
|
+
try {
|
|
1538
|
+
await unlink2(path);
|
|
1539
|
+
} catch (error) {
|
|
1540
|
+
if (!isMissing2(error)) throw error;
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
async function writeAtomic2(path, value) {
|
|
1544
|
+
const temporary = join4(join4(path, ".."), `.${basename2(path)}.${randomUUID3()}.tmp`);
|
|
1545
|
+
try {
|
|
1546
|
+
await writeFile2(temporary, JSON.stringify(value), { encoding: "utf8", mode: 384, flag: "wx" });
|
|
1547
|
+
await rename2(temporary, path);
|
|
1548
|
+
await chmod2(path, 384);
|
|
1549
|
+
} finally {
|
|
1550
|
+
await removeIfPresent2(temporary);
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
function isWithdrawnByCut(cut, seq) {
|
|
1554
|
+
return seq > cut.cutSeq && seq <= cut.restoredAtSeq;
|
|
1555
|
+
}
|
|
1556
|
+
var SessionCutStore = class {
|
|
1557
|
+
constructor(root) {
|
|
1558
|
+
this.root = root;
|
|
1559
|
+
this.directory = join4(root, "sessions");
|
|
1560
|
+
this.rootKey = join4(root, ".");
|
|
1561
|
+
}
|
|
1562
|
+
root;
|
|
1563
|
+
directory;
|
|
1564
|
+
rootKey;
|
|
1565
|
+
async get(sessionId) {
|
|
1566
|
+
await mkdir2(this.directory, { recursive: true, mode: 448 });
|
|
1567
|
+
const path = this.pathOf(sessionId);
|
|
1568
|
+
let stored;
|
|
1569
|
+
try {
|
|
1570
|
+
stored = JSON.parse(await readFile2(path, "utf8"));
|
|
1571
|
+
} catch (error) {
|
|
1572
|
+
if (isMissing2(error)) return null;
|
|
1573
|
+
throw error;
|
|
1574
|
+
}
|
|
1575
|
+
const parsed = fileSchema.safeParse(stored);
|
|
1576
|
+
if (!parsed.success) return null;
|
|
1577
|
+
return parsed.data.cut;
|
|
1578
|
+
}
|
|
1579
|
+
async set(sessionId, cut) {
|
|
1580
|
+
return serialize2(`${this.rootKey}\0${sessionId}`, async () => {
|
|
1581
|
+
await mkdir2(this.directory, { recursive: true, mode: 448 });
|
|
1582
|
+
const path = this.pathOf(sessionId);
|
|
1583
|
+
if (cut === null) {
|
|
1584
|
+
await removeIfPresent2(path);
|
|
1585
|
+
return null;
|
|
1586
|
+
}
|
|
1587
|
+
await writeAtomic2(path, {
|
|
1588
|
+
schemaVersion: SCHEMA_VERSION,
|
|
1589
|
+
sessionId,
|
|
1590
|
+
cut
|
|
1591
|
+
});
|
|
1592
|
+
return cut;
|
|
1593
|
+
});
|
|
1594
|
+
}
|
|
1595
|
+
pathOf(sessionId) {
|
|
1596
|
+
return join4(this.directory, `${digest2(sessionId)}.json`);
|
|
1597
|
+
}
|
|
1598
|
+
};
|
|
1599
|
+
|
|
1600
|
+
// src/host/checkpoint/cut-filter.ts
|
|
1601
|
+
function deriveMessagesWithCut(session, cut) {
|
|
1602
|
+
if (cut === null || cut === void 0) {
|
|
1603
|
+
}
|
|
1604
|
+
const events = session.snapshotEvents();
|
|
1605
|
+
const bySeq = new Map(events.map((event) => [Number(event.seq), event]));
|
|
1606
|
+
const messages = [];
|
|
1607
|
+
for (const seq of session.surface.nodes) {
|
|
1608
|
+
const value = Number(seq);
|
|
1609
|
+
const event = bySeq.get(value);
|
|
1610
|
+
if (event === void 0) continue;
|
|
1611
|
+
const keepSystem = event.type === "system/message";
|
|
1612
|
+
if (cut !== null && cut !== void 0 && isWithdrawnByCut(cut, value) && !keepSystem) continue;
|
|
1613
|
+
const message = session.deriveEventMessage(event);
|
|
1614
|
+
if (message !== null) messages.push(message);
|
|
1615
|
+
}
|
|
1616
|
+
return messages;
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
// src/host/checkpoint/cut-live.ts
|
|
1620
|
+
function installSessionCutFilter(ctx, options) {
|
|
1621
|
+
const patched = /* @__PURE__ */ new WeakSet();
|
|
1622
|
+
const patchSession = (session) => {
|
|
1623
|
+
if (patched.has(session)) return;
|
|
1624
|
+
patched.add(session);
|
|
1625
|
+
const native = session.deriveMessages.bind(session);
|
|
1626
|
+
session.deriveMessages = () => {
|
|
1627
|
+
const cut = options.cache.get(session.id);
|
|
1628
|
+
if (cut === void 0 || cut === null) return native();
|
|
1629
|
+
return deriveMessagesWithCut(session, cut);
|
|
1630
|
+
};
|
|
1631
|
+
};
|
|
1632
|
+
const warm = async (sessionId) => {
|
|
1633
|
+
if (options.cache.has(sessionId)) return;
|
|
1634
|
+
const cut = await options.store.get(sessionId);
|
|
1635
|
+
options.cache.set(sessionId, cut);
|
|
1636
|
+
};
|
|
1637
|
+
const disposers = [];
|
|
1638
|
+
disposers.push(ctx.on("agent/pre-step", async (payload, next) => {
|
|
1639
|
+
const agent = payload.agent;
|
|
1640
|
+
patchSession(agent.session);
|
|
1641
|
+
await warm(agent.session.id);
|
|
1642
|
+
return next();
|
|
1643
|
+
}));
|
|
1644
|
+
const agents = ctx.agents;
|
|
1645
|
+
if (agents !== void 0 && typeof agents.get === "function") {
|
|
1646
|
+
const inner = agents.get.bind(agents);
|
|
1647
|
+
agents.get = ((id) => {
|
|
1648
|
+
const agent = inner(id);
|
|
1649
|
+
if (agent?.session !== void 0) {
|
|
1650
|
+
patchSession(agent.session);
|
|
1651
|
+
void warm(agent.session.id);
|
|
1652
|
+
}
|
|
1653
|
+
return agent;
|
|
1654
|
+
});
|
|
1655
|
+
disposers.push(() => {
|
|
1656
|
+
if (agents.get !== inner) agents.get = inner;
|
|
1657
|
+
});
|
|
1658
|
+
}
|
|
1659
|
+
return () => {
|
|
1660
|
+
while (disposers.length > 0) disposers.pop()?.();
|
|
1661
|
+
};
|
|
1662
|
+
}
|
|
1663
|
+
function syncCutCache(cache, sessionId, cut) {
|
|
1664
|
+
cache.set(sessionId, cut);
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1442
1667
|
// src/host/checkpoint/service.ts
|
|
1443
1668
|
var surfaceEventTypes = /* @__PURE__ */ new Set(["system/message", "developer/message", "user/message", "assistant/message", "tool/result"]);
|
|
1444
1669
|
var PROMPT_PREVIEW_MAX = 96;
|
|
@@ -1467,6 +1692,43 @@ function toCheckpointTurnSummary(checkpoint, options = {}) {
|
|
|
1467
1692
|
tip
|
|
1468
1693
|
};
|
|
1469
1694
|
}
|
|
1695
|
+
function summaryFromClosedTurn(events, turn, options = {}) {
|
|
1696
|
+
const turnStart = events.find(
|
|
1697
|
+
(event) => event.type === "turn/start" && event.data.turn === turn
|
|
1698
|
+
);
|
|
1699
|
+
if (turnStart === void 0) return null;
|
|
1700
|
+
const prompt = recoverTurnPrompt(events, turn);
|
|
1701
|
+
return {
|
|
1702
|
+
turn,
|
|
1703
|
+
coverage: "complete",
|
|
1704
|
+
fileCount: 0,
|
|
1705
|
+
createdAt: options.now ?? Date.now(),
|
|
1706
|
+
atSeq: checkpointAtSeq(Number(turnStart.seq)),
|
|
1707
|
+
prompt,
|
|
1708
|
+
promptPreview: checkpointPromptPreview(prompt),
|
|
1709
|
+
files: [],
|
|
1710
|
+
gaps: [],
|
|
1711
|
+
rolledBack: options.rolledBack ?? false,
|
|
1712
|
+
tip: options.tip ?? false
|
|
1713
|
+
};
|
|
1714
|
+
}
|
|
1715
|
+
function cutViewOf(cut) {
|
|
1716
|
+
return {
|
|
1717
|
+
hideTurnsFrom: cut.hideTurnsFrom,
|
|
1718
|
+
cutSeq: cut.cutSeq,
|
|
1719
|
+
restoredAtSeq: cut.restoredAtSeq,
|
|
1720
|
+
draftPrompt: cut.draftPrompt,
|
|
1721
|
+
scope: cut.scope
|
|
1722
|
+
};
|
|
1723
|
+
}
|
|
1724
|
+
function windowFromCut(cut) {
|
|
1725
|
+
if (cut === null || cut === void 0) return void 0;
|
|
1726
|
+
return {
|
|
1727
|
+
tipTurn: cut.hideTurnsFrom,
|
|
1728
|
+
hideTurnsFrom: cut.hideTurnsFrom,
|
|
1729
|
+
continueAfterSeq: cut.restoredAtSeq
|
|
1730
|
+
};
|
|
1731
|
+
}
|
|
1470
1732
|
var CheckpointRollbackError = class extends Error {
|
|
1471
1733
|
constructor(code, unavailable = [], conflicts = []) {
|
|
1472
1734
|
super(code);
|
|
@@ -1479,28 +1741,9 @@ var CheckpointRollbackError = class extends Error {
|
|
|
1479
1741
|
unavailable;
|
|
1480
1742
|
conflicts;
|
|
1481
1743
|
};
|
|
1482
|
-
function rollbackMarkerId(turn, journalId, scope) {
|
|
1483
|
-
const tag = scope === "conversation" ? "c:" : "";
|
|
1484
|
-
return MessageId(`${ROLLBACK_PREFIX}${turn}:${tag}${journalId}`);
|
|
1485
|
-
}
|
|
1486
1744
|
function undoCopyId(journalId, index) {
|
|
1487
1745
|
return MessageId(`${UNDO_PREFIX}${journalId}:${index}`);
|
|
1488
1746
|
}
|
|
1489
|
-
function rollbackIdOf(id) {
|
|
1490
|
-
const start = indexOfRollbackPrefix(id);
|
|
1491
|
-
return start >= 0 ? id.slice(start) : id;
|
|
1492
|
-
}
|
|
1493
|
-
function scopeOfMarkerId(id) {
|
|
1494
|
-
const rolled = rollbackIdOf(id);
|
|
1495
|
-
for (const prefix of [ROLLBACK_PREFIX, LEGACY_ROLLBACK_PREFIX]) {
|
|
1496
|
-
const pattern = new RegExp(`^${prefix.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}\\d+:c:`);
|
|
1497
|
-
if (pattern.test(rolled)) return "conversation";
|
|
1498
|
-
}
|
|
1499
|
-
return "conversation-and-files";
|
|
1500
|
-
}
|
|
1501
|
-
function scopeOfMarker(event) {
|
|
1502
|
-
return scopeOfMarkerId(event.data.id);
|
|
1503
|
-
}
|
|
1504
1747
|
function isRollbackMarker(event, turn) {
|
|
1505
1748
|
if (event?.type !== "user/message" || event.data.source.kind !== "checkpoint-rollback" || event.data.source.form !== "notice" || event.data.content.length === 0) return false;
|
|
1506
1749
|
const prefixes = turn === void 0 ? [ROLLBACK_PREFIX, LEGACY_ROLLBACK_PREFIX] : [`${ROLLBACK_PREFIX}${turn}:`, `${LEGACY_ROLLBACK_PREFIX}${turn}:`];
|
|
@@ -1567,28 +1810,12 @@ function isUndoCopyAt(event, expected) {
|
|
|
1567
1810
|
const id = surfaceMessageId2(event);
|
|
1568
1811
|
return id === expected || (id?.startsWith(`${expected}:`) ?? false);
|
|
1569
1812
|
}
|
|
1570
|
-
function logicalSeq(event) {
|
|
1571
|
-
if (isUndoCopy(event)) {
|
|
1572
|
-
const match = surfaceMessageId2(event)?.match(/:(\d+)(?::dsh-(?:agent|composer)-enhancer:rollback:.*)?$/);
|
|
1573
|
-
if (match !== null && match !== void 0) return SessionSeq(Number(match[1]));
|
|
1574
|
-
}
|
|
1575
|
-
return event.seq;
|
|
1576
|
-
}
|
|
1577
1813
|
function isOwnReplacement(event) {
|
|
1578
1814
|
return isRollbackMarker(event) || isUndoCopy(event);
|
|
1579
1815
|
}
|
|
1580
1816
|
function isProtectedSystemMessage(event) {
|
|
1581
1817
|
return event?.type === "system/message";
|
|
1582
1818
|
}
|
|
1583
|
-
function rollbackSurfaceNodes(events, candidates) {
|
|
1584
|
-
return candidates.filter((node) => !isProtectedSystemMessage(events[node]));
|
|
1585
|
-
}
|
|
1586
|
-
function isAlreadyRestoredRollback(events, nodes, turn, surfaceNodes) {
|
|
1587
|
-
if (nodes.length === 0) return false;
|
|
1588
|
-
if (!nodes.every((node) => isRollbackMarker(events[node]))) return false;
|
|
1589
|
-
const window = activeRollbackWindow(events, surfaceNodes);
|
|
1590
|
-
return window !== void 0 && turn === window.tipTurn;
|
|
1591
|
-
}
|
|
1592
1819
|
function abortError() {
|
|
1593
1820
|
return new DOMException("Checkpoint rollback was cancelled", "AbortError");
|
|
1594
1821
|
}
|
|
@@ -1614,278 +1841,6 @@ function scopeOf(request) {
|
|
|
1614
1841
|
function directionOf(operation) {
|
|
1615
1842
|
return operation === "rollback" ? "rollback" : "undo";
|
|
1616
1843
|
}
|
|
1617
|
-
function turnOwningSeq(events, seq) {
|
|
1618
|
-
let current;
|
|
1619
|
-
for (const event of events) {
|
|
1620
|
-
if (Number(event.seq) > seq) break;
|
|
1621
|
-
if (event.type === "turn/start") current = event.data.turn;
|
|
1622
|
-
}
|
|
1623
|
-
return current;
|
|
1624
|
-
}
|
|
1625
|
-
function logicalTurnOwning(events, event) {
|
|
1626
|
-
return turnOwningSeq(events, Number(logicalSeq(event)));
|
|
1627
|
-
}
|
|
1628
|
-
function sourceEvents(events, marker) {
|
|
1629
|
-
if (marker.surfaceOp === "append" && marker.sourceEventSeqs?.length === 1) {
|
|
1630
|
-
const original = events[marker.sourceEventSeqs[0]];
|
|
1631
|
-
if (isRollbackMarker(original)) return sourceEvents(events, original);
|
|
1632
|
-
}
|
|
1633
|
-
if (marker.sourceEventSeqs === void 0 || marker.sourceEventSeqs.length === 0) return null;
|
|
1634
|
-
const sources = [];
|
|
1635
|
-
for (const seq of marker.sourceEventSeqs) {
|
|
1636
|
-
const event = events[seq];
|
|
1637
|
-
if (event === void 0) return null;
|
|
1638
|
-
if (isRollbackMarker(event)) {
|
|
1639
|
-
const nested = sourceEvents(events, event);
|
|
1640
|
-
if (nested === null) return null;
|
|
1641
|
-
sources.push(...nested);
|
|
1642
|
-
continue;
|
|
1643
|
-
}
|
|
1644
|
-
if (!surfaceEventTypes.has(event.type)) return null;
|
|
1645
|
-
sources.push(event);
|
|
1646
|
-
}
|
|
1647
|
-
if (sources[0]?.type !== "user/message") return null;
|
|
1648
|
-
return sources;
|
|
1649
|
-
}
|
|
1650
|
-
function peelUndoSources(events, sources, tipTurn) {
|
|
1651
|
-
const reappear = sources.filter((event) => logicalTurnOwning(events, event) === tipTurn);
|
|
1652
|
-
const stillWithdraw = sources.filter((event) => {
|
|
1653
|
-
const owned = logicalTurnOwning(events, event);
|
|
1654
|
-
return owned !== void 0 && owned !== tipTurn;
|
|
1655
|
-
});
|
|
1656
|
-
if (reappear.length === 0 || stillWithdraw.length === 0) {
|
|
1657
|
-
return { undoSources: sources, nextTip: void 0 };
|
|
1658
|
-
}
|
|
1659
|
-
const nextTipTurn = stillWithdraw.reduce((min, event) => {
|
|
1660
|
-
const owned = logicalTurnOwning(events, event);
|
|
1661
|
-
return owned === void 0 ? min : Math.min(min, owned);
|
|
1662
|
-
}, Number.POSITIVE_INFINITY);
|
|
1663
|
-
if (!Number.isFinite(nextTipTurn)) return { undoSources: sources, nextTip: void 0 };
|
|
1664
|
-
return {
|
|
1665
|
-
undoSources: reappear,
|
|
1666
|
-
nextTip: { turn: nextTipTurn, sources: stillWithdraw }
|
|
1667
|
-
};
|
|
1668
|
-
}
|
|
1669
|
-
function nextTurnNumber(events) {
|
|
1670
|
-
let max = 0;
|
|
1671
|
-
for (const event of events) {
|
|
1672
|
-
if (event.type === "turn/start") max = Math.max(max, event.data.turn);
|
|
1673
|
-
}
|
|
1674
|
-
return max + 1;
|
|
1675
|
-
}
|
|
1676
|
-
function undoTurnAfterMarker(events, markerSeq) {
|
|
1677
|
-
for (const event of events) {
|
|
1678
|
-
if (Number(event.seq) > Number(markerSeq) && event.type === "turn/start") {
|
|
1679
|
-
return event.data.turn;
|
|
1680
|
-
}
|
|
1681
|
-
}
|
|
1682
|
-
throw new Error("Partial undo is missing its turn/start");
|
|
1683
|
-
}
|
|
1684
|
-
function tipRollbackMarker(events, nodes, turn) {
|
|
1685
|
-
let tip;
|
|
1686
|
-
for (const node of nodes) {
|
|
1687
|
-
const event = events[node];
|
|
1688
|
-
if (!isRollbackMarker(event, turn) || !markerStillActive(event, events)) continue;
|
|
1689
|
-
tip = event;
|
|
1690
|
-
}
|
|
1691
|
-
return tip;
|
|
1692
|
-
}
|
|
1693
|
-
function resolveUndoTipMarker(events, nodes) {
|
|
1694
|
-
const onSurface = tipRollbackMarker(events, nodes);
|
|
1695
|
-
if (onSurface !== void 0) return onSurface;
|
|
1696
|
-
let tip;
|
|
1697
|
-
for (const event of events) {
|
|
1698
|
-
if (!isRollbackMarker(event) || !markerStillActive(event, events)) continue;
|
|
1699
|
-
tip = event;
|
|
1700
|
-
}
|
|
1701
|
-
if (tip !== void 0 && nodes.some((node) => Number(node) === Number(tip.seq))) return tip;
|
|
1702
|
-
let surfaceTip;
|
|
1703
|
-
for (const node of nodes) {
|
|
1704
|
-
const event = events[node];
|
|
1705
|
-
if (isRollbackMarker(event)) surfaceTip = event;
|
|
1706
|
-
}
|
|
1707
|
-
return surfaceTip;
|
|
1708
|
-
}
|
|
1709
|
-
function undoReplaceEndSeq(events, nodes, markerSeq) {
|
|
1710
|
-
const markerIndex = nodes.findIndex((node) => Number(node) === Number(markerSeq));
|
|
1711
|
-
if (markerIndex < 0) return markerSeq;
|
|
1712
|
-
let end = markerSeq;
|
|
1713
|
-
for (let index = markerIndex + 1; index < nodes.length; index += 1) {
|
|
1714
|
-
const event = events[nodes[index]];
|
|
1715
|
-
if (ignoreOnUndoKeepPrefix(event)) continue;
|
|
1716
|
-
end = SessionSeq(nodes[index]);
|
|
1717
|
-
}
|
|
1718
|
-
return end;
|
|
1719
|
-
}
|
|
1720
|
-
function undoShadowedSourceSeqs(nodes, markerSeq, replaceEndSeq, sourceSeq) {
|
|
1721
|
-
const seqs = [];
|
|
1722
|
-
let inRange = false;
|
|
1723
|
-
for (const node of nodes) {
|
|
1724
|
-
if (Number(node) === Number(markerSeq)) inRange = true;
|
|
1725
|
-
if (!inRange) continue;
|
|
1726
|
-
seqs.push(SessionSeq(node));
|
|
1727
|
-
if (Number(node) === Number(replaceEndSeq)) break;
|
|
1728
|
-
}
|
|
1729
|
-
if (!seqs.some((seq) => Number(seq) === Number(sourceSeq))) seqs.push(sourceSeq);
|
|
1730
|
-
return seqs;
|
|
1731
|
-
}
|
|
1732
|
-
function ignoreOnUndoKeepPrefix(event) {
|
|
1733
|
-
return isProtectedSystemMessage(event) || event?.type === "developer/message";
|
|
1734
|
-
}
|
|
1735
|
-
function toolCallsOf(event) {
|
|
1736
|
-
const calls = [];
|
|
1737
|
-
for (const block of event.data.message.content) {
|
|
1738
|
-
if (block.type !== "tool-call") continue;
|
|
1739
|
-
calls.push({ callId: block.id, name: block.name, arguments: block.arguments });
|
|
1740
|
-
}
|
|
1741
|
-
return calls;
|
|
1742
|
-
}
|
|
1743
|
-
function appendUndoToolCalls(session, calls, turn, step, openCalls) {
|
|
1744
|
-
for (const call of calls) {
|
|
1745
|
-
session.append("tool/call", {
|
|
1746
|
-
turn,
|
|
1747
|
-
step,
|
|
1748
|
-
callId: call.callId,
|
|
1749
|
-
name: call.name,
|
|
1750
|
-
arguments: call.arguments
|
|
1751
|
-
});
|
|
1752
|
-
openCalls.set(String(call.callId), call);
|
|
1753
|
-
}
|
|
1754
|
-
}
|
|
1755
|
-
function settleOpenUndoToolCalls(session, journalId, turn, step, openCalls) {
|
|
1756
|
-
for (const call of openCalls.values()) {
|
|
1757
|
-
session.append("tool/result", {
|
|
1758
|
-
turn,
|
|
1759
|
-
step,
|
|
1760
|
-
message: {
|
|
1761
|
-
id: MessageId(`${UNDO_PREFIX}${journalId}:orphan:${call.callId}`),
|
|
1762
|
-
role: "tool",
|
|
1763
|
-
source: { kind: "tool", callId: call.callId },
|
|
1764
|
-
toolCallId: call.callId,
|
|
1765
|
-
isError: true,
|
|
1766
|
-
content: [{
|
|
1767
|
-
type: "text",
|
|
1768
|
-
text: "The tool call was restored without a recorded result."
|
|
1769
|
-
}]
|
|
1770
|
-
},
|
|
1771
|
-
error: {
|
|
1772
|
-
name: "UndoOrphanToolCall",
|
|
1773
|
-
code: "UNDO_ORPHAN_TOOL_CALL",
|
|
1774
|
-
reason: "Restored assistant tool call had no matching tool/result in the rollback surface."
|
|
1775
|
-
}
|
|
1776
|
-
}, { surfaceOp: "append" });
|
|
1777
|
-
}
|
|
1778
|
-
openCalls.clear();
|
|
1779
|
-
}
|
|
1780
|
-
function ensureCopiedAssistantToolCalls(session, events, journalId, completed, turn, step, openCalls) {
|
|
1781
|
-
const first = events.find((event) => isUndoCopyAt(event, undoCopyId(journalId, 0)));
|
|
1782
|
-
if (first === void 0) return;
|
|
1783
|
-
const undoStart = Number(first.seq);
|
|
1784
|
-
const started = /* @__PURE__ */ new Set();
|
|
1785
|
-
const settled = /* @__PURE__ */ new Set();
|
|
1786
|
-
for (const event of events) {
|
|
1787
|
-
if (Number(event.seq) < undoStart) continue;
|
|
1788
|
-
if (event.type === "tool/call") started.add(String(event.data.callId));
|
|
1789
|
-
if (event.type === "tool/result") settled.add(String(event.data.message.toolCallId));
|
|
1790
|
-
}
|
|
1791
|
-
for (let index = 0; index < completed; index += 1) {
|
|
1792
|
-
const event = events.find((candidate) => isUndoCopyAt(candidate, undoCopyId(journalId, index)));
|
|
1793
|
-
if (event?.type !== "assistant/message") continue;
|
|
1794
|
-
const calls = toolCallsOf(event);
|
|
1795
|
-
appendUndoToolCalls(
|
|
1796
|
-
session,
|
|
1797
|
-
calls.filter((call) => !started.has(String(call.callId))),
|
|
1798
|
-
turn,
|
|
1799
|
-
step,
|
|
1800
|
-
openCalls
|
|
1801
|
-
);
|
|
1802
|
-
for (const call of calls) {
|
|
1803
|
-
if (!settled.has(String(call.callId))) openCalls.set(String(call.callId), call);
|
|
1804
|
-
}
|
|
1805
|
-
}
|
|
1806
|
-
}
|
|
1807
|
-
function countPartialUndoCopies(events, first, journalId, tip) {
|
|
1808
|
-
let completed = 0;
|
|
1809
|
-
let cursor = Number(first.seq);
|
|
1810
|
-
while (cursor <= tip) {
|
|
1811
|
-
const event = events[SessionSeq(cursor)];
|
|
1812
|
-
if (event === void 0) throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
1813
|
-
if (isUndoCopyAt(event, undoCopyId(journalId, completed))) {
|
|
1814
|
-
completed += 1;
|
|
1815
|
-
cursor += 1;
|
|
1816
|
-
continue;
|
|
1817
|
-
}
|
|
1818
|
-
if (event.type === "tool/call" && completed > 0) {
|
|
1819
|
-
cursor += 1;
|
|
1820
|
-
continue;
|
|
1821
|
-
}
|
|
1822
|
-
throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
1823
|
-
}
|
|
1824
|
-
return completed;
|
|
1825
|
-
}
|
|
1826
|
-
function appendUndoCopy(session, source, journalId, index, tipMarkerSeq, mode, replaceStartSeq, replaceEndSeq, turn, step, openCalls) {
|
|
1827
|
-
const copyId = MessageId(`${undoCopyId(journalId, index)}:${source.seq}`);
|
|
1828
|
-
const id = isRollbackMarker(source) ? MessageId(`${copyId}:${source.data.id}`) : copyId;
|
|
1829
|
-
if (source.type === "user/message") {
|
|
1830
|
-
if (mode === "append" || index > 0) {
|
|
1831
|
-
return session.append("user/message", { ...source.data, id }, {
|
|
1832
|
-
surfaceOp: "append",
|
|
1833
|
-
sourceEventSeqs: [source.seq]
|
|
1834
|
-
});
|
|
1835
|
-
}
|
|
1836
|
-
const shadowed = undoShadowedSourceSeqs(
|
|
1837
|
-
session.surface.nodes,
|
|
1838
|
-
replaceStartSeq,
|
|
1839
|
-
replaceEndSeq,
|
|
1840
|
-
source.seq
|
|
1841
|
-
);
|
|
1842
|
-
if (!shadowed.some((seq) => Number(seq) === Number(tipMarkerSeq))) {
|
|
1843
|
-
shadowed.unshift(tipMarkerSeq);
|
|
1844
|
-
}
|
|
1845
|
-
return session.append("user/message", { ...source.data, id }, {
|
|
1846
|
-
surfaceOp: { op: "replace", startSeq: replaceStartSeq, endSeq: replaceEndSeq },
|
|
1847
|
-
sourceEventSeqs: shadowed
|
|
1848
|
-
});
|
|
1849
|
-
}
|
|
1850
|
-
if (index === 0 && mode === "replace") {
|
|
1851
|
-
throw new Error("The first restored surface event must be a user message");
|
|
1852
|
-
}
|
|
1853
|
-
switch (source.type) {
|
|
1854
|
-
case "assistant/message": {
|
|
1855
|
-
const event = session.append("assistant/message", {
|
|
1856
|
-
...source.data,
|
|
1857
|
-
turn,
|
|
1858
|
-
step,
|
|
1859
|
-
message: { ...source.data.message, id }
|
|
1860
|
-
}, { surfaceOp: "append" });
|
|
1861
|
-
appendUndoToolCalls(session, toolCallsOf(source), turn, step, openCalls);
|
|
1862
|
-
return event;
|
|
1863
|
-
}
|
|
1864
|
-
case "system/message":
|
|
1865
|
-
return session.append("system/message", {
|
|
1866
|
-
...source.data,
|
|
1867
|
-
turn,
|
|
1868
|
-
step,
|
|
1869
|
-
message: { ...source.data.message, id }
|
|
1870
|
-
}, { surfaceOp: "append", sourceEventSeqs: [source.seq] });
|
|
1871
|
-
case "developer/message":
|
|
1872
|
-
return session.append("developer/message", {
|
|
1873
|
-
...source.data,
|
|
1874
|
-
turn,
|
|
1875
|
-
step,
|
|
1876
|
-
message: { ...source.data.message, id }
|
|
1877
|
-
}, { surfaceOp: "append", sourceEventSeqs: [source.seq] });
|
|
1878
|
-
case "tool/result": {
|
|
1879
|
-
openCalls.delete(String(source.data.message.toolCallId));
|
|
1880
|
-
return session.append("tool/result", {
|
|
1881
|
-
...source.data,
|
|
1882
|
-
turn,
|
|
1883
|
-
step,
|
|
1884
|
-
message: { ...source.data.message, id }
|
|
1885
|
-
}, { surfaceOp: "append", sourceEventSeqs: [source.seq] });
|
|
1886
|
-
}
|
|
1887
|
-
}
|
|
1888
|
-
}
|
|
1889
1844
|
var CheckpointRollbackService = class {
|
|
1890
1845
|
constructor(options) {
|
|
1891
1846
|
this.options = options;
|
|
@@ -1904,6 +1859,9 @@ var CheckpointRollbackService = class {
|
|
|
1904
1859
|
summaries(request, signal) {
|
|
1905
1860
|
return this.track(this.runSummaries(request, signal));
|
|
1906
1861
|
}
|
|
1862
|
+
getCut(request, signal) {
|
|
1863
|
+
return this.track(this.runGetCut(request, signal));
|
|
1864
|
+
}
|
|
1907
1865
|
recover() {
|
|
1908
1866
|
if (this.recovery !== void 0) return this.recovery;
|
|
1909
1867
|
const operation = this.track(this.runRecovery());
|
|
@@ -1951,7 +1909,11 @@ var CheckpointRollbackService = class {
|
|
|
1951
1909
|
try {
|
|
1952
1910
|
linked.signal.throwIfAborted();
|
|
1953
1911
|
if ((await this.options.store.listPendingJournals()).length > 0) {
|
|
1954
|
-
|
|
1912
|
+
try {
|
|
1913
|
+
await this.recover();
|
|
1914
|
+
} catch {
|
|
1915
|
+
throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
1916
|
+
}
|
|
1955
1917
|
}
|
|
1956
1918
|
linked.signal.throwIfAborted();
|
|
1957
1919
|
return (await this.evaluate(request)).preview;
|
|
@@ -1975,30 +1937,90 @@ var CheckpointRollbackService = class {
|
|
|
1975
1937
|
await this.options.awaitCaptures?.();
|
|
1976
1938
|
linked.signal.throwIfAborted();
|
|
1977
1939
|
const checkpoints = await this.options.store.list(request.sessionId);
|
|
1940
|
+
const liveCut = await this.readCut(request.sessionId);
|
|
1978
1941
|
const adapter = this.options.resolveSession(request.sessionId);
|
|
1979
1942
|
const events = adapter?.readEvents() ?? [];
|
|
1980
1943
|
const surfaceNodes = adapter?.session.surface.nodes;
|
|
1981
|
-
const
|
|
1944
|
+
const tipWindow = events.length === 0 ? void 0 : activeRollbackWindow(events, surfaceNodes);
|
|
1945
|
+
const window = windowFromCut(liveCut) ?? tipWindow;
|
|
1982
1946
|
const shells = undoShellRanges(events);
|
|
1983
|
-
const kept = events.length === 0 ? checkpoints : checkpoints.filter((checkpoint) => !isUndoShellCheckpoint(checkpoint.atSeq, shells));
|
|
1947
|
+
const kept = events.length === 0 ? checkpoints : checkpoints.filter((checkpoint) => checkpoint.files.length > 0 || !isUndoShellCheckpoint(checkpoint.atSeq, shells));
|
|
1984
1948
|
if (kept.length !== checkpoints.length) {
|
|
1985
1949
|
await this.options.store.replace(request.sessionId, kept);
|
|
1986
1950
|
}
|
|
1987
1951
|
void this.options.store.purgeSettledJournals().catch(() => void 0);
|
|
1988
|
-
const
|
|
1952
|
+
const tipAtSeq = liveCut?.cutSeq;
|
|
1953
|
+
const byAtSeq = /* @__PURE__ */ new Map();
|
|
1989
1954
|
for (const checkpoint of kept) {
|
|
1990
1955
|
const recoveredPrompt = checkpoint.prompt.trim() === "" ? recoverTurnPrompt(events, checkpoint.turn) : "";
|
|
1991
1956
|
const rolledBack = isRolledBackCheckpoint(checkpoint.turn, checkpoint.atSeq, window);
|
|
1992
|
-
|
|
1993
|
-
|
|
1994
|
-
|
|
1995
|
-
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
);
|
|
1957
|
+
const tip = tipAtSeq !== void 0 ? checkpoint.atSeq === tipAtSeq : window !== void 0 && checkpoint.turn === window.tipTurn;
|
|
1958
|
+
const summary = toCheckpointTurnSummary(checkpoint, {
|
|
1959
|
+
rolledBack,
|
|
1960
|
+
tip,
|
|
1961
|
+
recoveredPrompt
|
|
1962
|
+
});
|
|
1963
|
+
const previous = byAtSeq.get(checkpoint.atSeq);
|
|
1964
|
+
if (previous !== void 0 && previous.fileCount > 0 && summary.fileCount === 0) continue;
|
|
1965
|
+
byAtSeq.set(checkpoint.atSeq, summary);
|
|
2000
1966
|
}
|
|
2001
|
-
const
|
|
1967
|
+
const live = adapter;
|
|
1968
|
+
if (live != null && events.length > 0) {
|
|
1969
|
+
const closedStarts = [];
|
|
1970
|
+
for (let index = 0; index < events.length; index += 1) {
|
|
1971
|
+
const event = events[index];
|
|
1972
|
+
if (event.type !== "turn/start") continue;
|
|
1973
|
+
const turn = event.data.turn;
|
|
1974
|
+
const ended = events.slice(index + 1).some((item) => item.type === "turn/end" && item.data.turn === turn && Number(item.seq) > Number(event.seq));
|
|
1975
|
+
if (ended) closedStarts.push(event);
|
|
1976
|
+
}
|
|
1977
|
+
const backfilled = [];
|
|
1978
|
+
for (const event of closedStarts) {
|
|
1979
|
+
const atSeq = checkpointAtSeq(Number(event.seq));
|
|
1980
|
+
if (byAtSeq.has(atSeq)) continue;
|
|
1981
|
+
const soft = synthesizeCheckpoint({
|
|
1982
|
+
sessionId: request.sessionId,
|
|
1983
|
+
session: {
|
|
1984
|
+
format: live.session.header.version,
|
|
1985
|
+
preset: live.session.header.agentPreset ?? null
|
|
1986
|
+
},
|
|
1987
|
+
pluginVersion: this.options.pluginVersion,
|
|
1988
|
+
turn: event.data.turn,
|
|
1989
|
+
events,
|
|
1990
|
+
createId: this.options.createId,
|
|
1991
|
+
now: this.options.now
|
|
1992
|
+
});
|
|
1993
|
+
if (soft !== null) {
|
|
1994
|
+
if (isUndoShellCheckpoint(soft.atSeq, shells) && soft.atSeq === atSeq) continue;
|
|
1995
|
+
const pinned = soft.atSeq === atSeq ? soft : { ...soft, atSeq };
|
|
1996
|
+
if (isUndoShellCheckpoint(pinned.atSeq, shells)) continue;
|
|
1997
|
+
backfilled.push(pinned);
|
|
1998
|
+
byAtSeq.set(
|
|
1999
|
+
atSeq,
|
|
2000
|
+
toCheckpointTurnSummary(pinned, {
|
|
2001
|
+
rolledBack: isRolledBackCheckpoint(pinned.turn, pinned.atSeq, window),
|
|
2002
|
+
tip: tipAtSeq !== void 0 ? pinned.atSeq === tipAtSeq : window !== void 0 && pinned.turn === window.tipTurn
|
|
2003
|
+
})
|
|
2004
|
+
);
|
|
2005
|
+
continue;
|
|
2006
|
+
}
|
|
2007
|
+
const row = summaryFromClosedTurn(events, event.data.turn, {
|
|
2008
|
+
rolledBack: isRolledBackCheckpoint(event.data.turn, atSeq, window),
|
|
2009
|
+
tip: tipAtSeq !== void 0 ? atSeq === tipAtSeq : window !== void 0 && event.data.turn === window.tipTurn,
|
|
2010
|
+
now: this.options.now?.()
|
|
2011
|
+
});
|
|
2012
|
+
if (row !== null && !isUndoShellCheckpoint(atSeq, shells)) {
|
|
2013
|
+
byAtSeq.set(atSeq, { ...row, atSeq });
|
|
2014
|
+
}
|
|
2015
|
+
}
|
|
2016
|
+
for (const soft of backfilled) {
|
|
2017
|
+
try {
|
|
2018
|
+
await this.options.store.save(soft);
|
|
2019
|
+
} catch {
|
|
2020
|
+
}
|
|
2021
|
+
}
|
|
2022
|
+
}
|
|
2023
|
+
const turns = [...byAtSeq.values()].sort((left, right) => left.atSeq - right.atSeq || left.turn - right.turn || left.createdAt - right.createdAt);
|
|
2002
2024
|
const liveCheckpoints = turns.filter((turn) => !turn.rolledBack).length;
|
|
2003
2025
|
const activity = liveActivityStats(events, {
|
|
2004
2026
|
hideTurnsFrom: window?.hideTurnsFrom,
|
|
@@ -2017,6 +2039,36 @@ var CheckpointRollbackService = class {
|
|
|
2017
2039
|
linked.cleanup();
|
|
2018
2040
|
}
|
|
2019
2041
|
}
|
|
2042
|
+
async runGetCut(request, callerSignal) {
|
|
2043
|
+
const linked = this.linkedSignal([callerSignal]);
|
|
2044
|
+
try {
|
|
2045
|
+
linked.signal.throwIfAborted();
|
|
2046
|
+
const cut = await this.readCut(request.sessionId);
|
|
2047
|
+
return {
|
|
2048
|
+
sessionId: request.sessionId,
|
|
2049
|
+
cut: cut === null ? null : cutViewOf(cut)
|
|
2050
|
+
};
|
|
2051
|
+
} catch (error) {
|
|
2052
|
+
throw this.sanitize(error, linked.signal);
|
|
2053
|
+
} finally {
|
|
2054
|
+
linked.cleanup();
|
|
2055
|
+
}
|
|
2056
|
+
}
|
|
2057
|
+
async readCut(sessionId) {
|
|
2058
|
+
const cached = this.options.cutCache?.get(sessionId);
|
|
2059
|
+
if (cached !== void 0) return cached;
|
|
2060
|
+
if (this.options.cutStore === void 0) return null;
|
|
2061
|
+
const cut = await this.options.cutStore.get(sessionId);
|
|
2062
|
+
if (this.options.cutCache !== void 0) syncCutCache(this.options.cutCache, sessionId, cut);
|
|
2063
|
+
return cut;
|
|
2064
|
+
}
|
|
2065
|
+
async writeCut(sessionId, cut) {
|
|
2066
|
+
if (this.options.cutStore === void 0) {
|
|
2067
|
+
throw new CheckpointRollbackError("ROLLBACK_FAILED");
|
|
2068
|
+
}
|
|
2069
|
+
await this.options.cutStore.set(sessionId, cut);
|
|
2070
|
+
if (this.options.cutCache !== void 0) syncCutCache(this.options.cutCache, sessionId, cut);
|
|
2071
|
+
}
|
|
2020
2072
|
async runExecute(request, callerSignal) {
|
|
2021
2073
|
const linked = this.linkedSignal([callerSignal]);
|
|
2022
2074
|
try {
|
|
@@ -2035,46 +2087,71 @@ var CheckpointRollbackService = class {
|
|
|
2035
2087
|
const current = await this.evaluate(request, { withinMaintenance: true });
|
|
2036
2088
|
const scope = this.effectiveScope(request, current);
|
|
2037
2089
|
this.assertExecutable(current, scope);
|
|
2038
|
-
if (current.adapter?.session !== adapter.session || current.surface === null) {
|
|
2090
|
+
if (current.adapter?.session.id !== adapter.session.id || current.surface === null) {
|
|
2039
2091
|
throw new CheckpointRollbackError("ROLLBACK_UNAVAILABLE", ["session-changed"]);
|
|
2040
2092
|
}
|
|
2041
2093
|
const checkpoint = current.checkpoint;
|
|
2042
2094
|
const operation = operationOf(request);
|
|
2043
2095
|
const session = adapter.session;
|
|
2044
|
-
const expectedSeq = session.seq;
|
|
2045
|
-
const
|
|
2046
|
-
const journalId = (this.options.createId ?? randomUUID3)();
|
|
2096
|
+
const expectedSeq = Number(session.seq);
|
|
2097
|
+
const journalId = (this.options.createId ?? randomUUID4)();
|
|
2047
2098
|
const files = this.files(adapter, checkpoint, journalId);
|
|
2048
|
-
const filesToRestore = scope === "conversation" ? [] : checkpoint.files;
|
|
2049
2099
|
const surface = current.surface;
|
|
2100
|
+
const filesToRestore = scope === "conversation" ? [] : this.trackedFilesForRestore(
|
|
2101
|
+
await this.options.store.list(request.sessionId),
|
|
2102
|
+
adapter,
|
|
2103
|
+
surface,
|
|
2104
|
+
await this.readCut(request.sessionId)
|
|
2105
|
+
);
|
|
2106
|
+
const draftPrompt = checkpoint.prompt.trim() !== "" ? checkpoint.prompt : recoverTurnPrompt(adapter.readEvents(), request.turn);
|
|
2050
2107
|
if (operation === "rollback" && surface.operation === "rollback" && surface.alreadyRestored && filesToRestore.length === 0) {
|
|
2051
|
-
const draftPrompt2 = checkpoint.prompt.trim() !== "" ? checkpoint.prompt : recoverTurnPrompt(adapter.readEvents(), request.turn);
|
|
2052
2108
|
return {
|
|
2053
2109
|
sessionId: request.sessionId,
|
|
2054
2110
|
turn: request.turn,
|
|
2055
|
-
draft:
|
|
2111
|
+
draft: draftPrompt
|
|
2056
2112
|
};
|
|
2057
2113
|
}
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2114
|
+
if (maintenance.signal.aborted) {
|
|
2115
|
+
throw this.sanitize(abortError(), maintenance.signal);
|
|
2116
|
+
}
|
|
2117
|
+
if (adapter.session.id !== session.id || Number(adapter.session.seq) !== expectedSeq || !this.canCommit(adapter, scope)) {
|
|
2118
|
+
throw new CheckpointRollbackError("ROLLBACK_FAILED");
|
|
2119
|
+
}
|
|
2120
|
+
try {
|
|
2121
|
+
if (surface.operation === "rollback") {
|
|
2122
|
+
const previous = await this.readCut(request.sessionId);
|
|
2123
|
+
const next = {
|
|
2124
|
+
hideTurnsFrom: surface.turn,
|
|
2125
|
+
cutSeq: surface.cutSeq,
|
|
2126
|
+
restoredAtSeq: expectedSeq,
|
|
2127
|
+
draftPrompt,
|
|
2128
|
+
scope,
|
|
2129
|
+
...filesToRestore.length > 0 ? { journalId } : {},
|
|
2130
|
+
previous
|
|
2131
|
+
};
|
|
2132
|
+
await this.writeCut(request.sessionId, next);
|
|
2133
|
+
} else {
|
|
2134
|
+
await this.writeCut(request.sessionId, surface.restoreCut);
|
|
2063
2135
|
}
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
|
|
2073
|
-
}
|
|
2074
|
-
|
|
2136
|
+
} catch (error) {
|
|
2137
|
+
if (error instanceof CheckpointRollbackError) throw error;
|
|
2138
|
+
throw new CheckpointRollbackError("ROLLBACK_FAILED");
|
|
2139
|
+
}
|
|
2140
|
+
const latest = this.options.resolveSession(request.sessionId);
|
|
2141
|
+
const syncTarget = latest !== void 0 && latest.session.id === session.id ? latest : adapter;
|
|
2142
|
+
try {
|
|
2143
|
+
await syncTarget.synchronize(maintenance.signal);
|
|
2144
|
+
} catch {
|
|
2145
|
+
}
|
|
2146
|
+
if (filesToRestore.length > 0) {
|
|
2147
|
+
await files.runTransaction(filesToRestore, async () => {
|
|
2148
|
+
if (maintenance.signal.aborted) return { appended: false, synchronized: false };
|
|
2149
|
+
return { appended: true, synchronized: true };
|
|
2150
|
+
}, directionOf(operation));
|
|
2151
|
+
}
|
|
2075
2152
|
return {
|
|
2076
2153
|
sessionId: request.sessionId,
|
|
2077
|
-
turn: request.turn,
|
|
2154
|
+
turn: surface.operation === "undo" ? surface.tipTurn : request.turn,
|
|
2078
2155
|
draft: operation === "rollback" ? draftPrompt : ""
|
|
2079
2156
|
};
|
|
2080
2157
|
} catch (error) {
|
|
@@ -2096,127 +2173,6 @@ var CheckpointRollbackService = class {
|
|
|
2096
2173
|
linked.cleanup();
|
|
2097
2174
|
}
|
|
2098
2175
|
}
|
|
2099
|
-
async appendAndSynchronize(adapter, surface, journalId, scope, signal) {
|
|
2100
|
-
try {
|
|
2101
|
-
if (surface.operation === "rollback") this.appendRollback(adapter, surface, journalId, scope);
|
|
2102
|
-
else {
|
|
2103
|
-
this.appendUndo(
|
|
2104
|
-
adapter.session,
|
|
2105
|
-
surface.marker,
|
|
2106
|
-
surface.sources,
|
|
2107
|
-
journalId,
|
|
2108
|
-
surface.mode,
|
|
2109
|
-
surface.replaceStartSeq,
|
|
2110
|
-
surface.replaceEndSeq
|
|
2111
|
-
);
|
|
2112
|
-
if (surface.nextTip !== void 0 && surface.nextTip.sources.length > 0) {
|
|
2113
|
-
this.appendRollbackNotice(adapter, surface.nextTip.turn, journalId, scopeOfMarker(surface.marker), {
|
|
2114
|
-
surfaceOp: "append",
|
|
2115
|
-
sourceEventSeqs: surface.nextTip.sources.map((event) => SessionSeq(event.seq))
|
|
2116
|
-
});
|
|
2117
|
-
}
|
|
2118
|
-
}
|
|
2119
|
-
} catch {
|
|
2120
|
-
return { appended: this.appendOccurred(adapter, journalId), synchronized: false };
|
|
2121
|
-
}
|
|
2122
|
-
try {
|
|
2123
|
-
await adapter.synchronize(signal);
|
|
2124
|
-
return { appended: true, synchronized: true };
|
|
2125
|
-
} catch {
|
|
2126
|
-
return { appended: true, synchronized: false };
|
|
2127
|
-
}
|
|
2128
|
-
}
|
|
2129
|
-
appendRollback(adapter, surface, journalId, scope) {
|
|
2130
|
-
const forward = surface.forwardFromTip;
|
|
2131
|
-
if (forward !== void 0) {
|
|
2132
|
-
if (forward.stillWithdraw.length === 0) {
|
|
2133
|
-
this.appendUndo(
|
|
2134
|
-
adapter.session,
|
|
2135
|
-
forward.marker,
|
|
2136
|
-
forward.reappear,
|
|
2137
|
-
journalId,
|
|
2138
|
-
"replace",
|
|
2139
|
-
SessionSeq(forward.marker.seq),
|
|
2140
|
-
SessionSeq(forward.marker.seq)
|
|
2141
|
-
);
|
|
2142
|
-
return;
|
|
2143
|
-
}
|
|
2144
|
-
if (forward.reappear.length === 0) {
|
|
2145
|
-
this.appendRollbackNotice(adapter, surface.turn, journalId, scope, {
|
|
2146
|
-
surfaceOp: {
|
|
2147
|
-
op: "replace",
|
|
2148
|
-
startSeq: SessionSeq(forward.marker.seq),
|
|
2149
|
-
endSeq: SessionSeq(forward.marker.seq)
|
|
2150
|
-
},
|
|
2151
|
-
sourceEventSeqs: forward.stillWithdraw.map((event) => SessionSeq(event.seq))
|
|
2152
|
-
});
|
|
2153
|
-
return;
|
|
2154
|
-
}
|
|
2155
|
-
this.appendUndo(
|
|
2156
|
-
adapter.session,
|
|
2157
|
-
forward.marker,
|
|
2158
|
-
forward.reappear,
|
|
2159
|
-
journalId,
|
|
2160
|
-
"replace",
|
|
2161
|
-
SessionSeq(forward.marker.seq),
|
|
2162
|
-
SessionSeq(forward.marker.seq)
|
|
2163
|
-
);
|
|
2164
|
-
this.appendRollbackNotice(adapter, surface.turn, journalId, scope, {
|
|
2165
|
-
surfaceOp: "append",
|
|
2166
|
-
sourceEventSeqs: forward.stillWithdraw.map((event) => SessionSeq(event.seq))
|
|
2167
|
-
});
|
|
2168
|
-
return;
|
|
2169
|
-
}
|
|
2170
|
-
this.appendRollbackNotice(adapter, surface.turn, journalId, scope, {
|
|
2171
|
-
surfaceOp: {
|
|
2172
|
-
op: "replace",
|
|
2173
|
-
startSeq: surface.nodes[0],
|
|
2174
|
-
endSeq: surface.nodes[surface.nodes.length - 1]
|
|
2175
|
-
},
|
|
2176
|
-
sourceEventSeqs: [...surface.nodes]
|
|
2177
|
-
});
|
|
2178
|
-
}
|
|
2179
|
-
appendRollbackNotice(adapter, checkpointTurn, journalId, scope, append) {
|
|
2180
|
-
const conversationOnly = scope === "conversation";
|
|
2181
|
-
const summary = conversationOnly ? `Conversation rolled back before turn ${checkpointTurn}` : `Conversation and tracked files rolled back before turn ${checkpointTurn}`;
|
|
2182
|
-
const detail = conversationOnly ? `The user rolled back the conversation to before turn ${checkpointTurn}. Later events remain in the append-only log but are no longer active model context. Tracked files were not restored.` : `The user rolled back the conversation and tracked files to before turn ${checkpointTurn}. Later events remain in the append-only log but are no longer active model context. Re-read tracked files before relying on their contents.`;
|
|
2183
|
-
adapter.session.append("user/message", {
|
|
2184
|
-
id: rollbackMarkerId(checkpointTurn, journalId, scope),
|
|
2185
|
-
role: "user",
|
|
2186
|
-
content: [{ type: "text", text: detail }],
|
|
2187
|
-
source: { kind: "checkpoint-rollback", form: "notice", summary }
|
|
2188
|
-
}, {
|
|
2189
|
-
surfaceOp: append.surfaceOp,
|
|
2190
|
-
sourceEventSeqs: [...append.sourceEventSeqs]
|
|
2191
|
-
});
|
|
2192
|
-
}
|
|
2193
|
-
appendUndo(session, marker, sources, journalId, mode, replaceStartSeq, replaceEndSeq, start = 0, openCalls = /* @__PURE__ */ new Map()) {
|
|
2194
|
-
const events = session.snapshotEvents();
|
|
2195
|
-
const turn = start === 0 ? nextTurnNumber(events) : undoTurnAfterMarker(events, marker.seq);
|
|
2196
|
-
const step = 1;
|
|
2197
|
-
if (start === 0) {
|
|
2198
|
-
session.append("turn/start", { turn });
|
|
2199
|
-
session.append("step/start", { turn, step });
|
|
2200
|
-
}
|
|
2201
|
-
for (let index = start; index < sources.length; index += 1) {
|
|
2202
|
-
appendUndoCopy(
|
|
2203
|
-
session,
|
|
2204
|
-
sources[index],
|
|
2205
|
-
journalId,
|
|
2206
|
-
index,
|
|
2207
|
-
marker.seq,
|
|
2208
|
-
mode,
|
|
2209
|
-
replaceStartSeq,
|
|
2210
|
-
replaceEndSeq,
|
|
2211
|
-
turn,
|
|
2212
|
-
step,
|
|
2213
|
-
openCalls
|
|
2214
|
-
);
|
|
2215
|
-
}
|
|
2216
|
-
settleOpenUndoToolCalls(session, journalId, turn, step, openCalls);
|
|
2217
|
-
session.append("step/end", { turn, step });
|
|
2218
|
-
session.append("turn/end", { turn, reason: { kind: "completed" } });
|
|
2219
|
-
}
|
|
2220
2176
|
async runRecovery() {
|
|
2221
2177
|
try {
|
|
2222
2178
|
this.lifetime.signal.throwIfAborted();
|
|
@@ -2242,10 +2198,8 @@ var CheckpointRollbackService = class {
|
|
|
2242
2198
|
now: this.options.now
|
|
2243
2199
|
});
|
|
2244
2200
|
await files.recoverFiles(async (journal) => {
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
await adapter.synchronize(linked.signal);
|
|
2248
|
-
return true;
|
|
2201
|
+
const cut = await this.readCut(sessionId);
|
|
2202
|
+
return cut?.journalId === journal.id;
|
|
2249
2203
|
});
|
|
2250
2204
|
} finally {
|
|
2251
2205
|
linked.cleanup();
|
|
@@ -2257,59 +2211,6 @@ var CheckpointRollbackService = class {
|
|
|
2257
2211
|
throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
2258
2212
|
}
|
|
2259
2213
|
}
|
|
2260
|
-
async resumeUndo(adapter, journalId) {
|
|
2261
|
-
const events = adapter.readEvents();
|
|
2262
|
-
const first = events.find((event) => isUndoCopyAt(event, undoCopyId(journalId, 0)));
|
|
2263
|
-
if (first === void 0) return;
|
|
2264
|
-
if (first.type !== "user/message") {
|
|
2265
|
-
throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
2266
|
-
}
|
|
2267
|
-
const appendOnly = first.surfaceOp === "append";
|
|
2268
|
-
const hinted = first.sourceEventSeqs?.map((seq) => events[seq]).find((event) => isRollbackMarker(event));
|
|
2269
|
-
const marker = !appendOnly && first.surfaceOp !== void 0 && first.surfaceOp !== "append" && isRollbackMarker(events[first.surfaceOp.startSeq]) ? events[first.surfaceOp.startSeq] : hinted;
|
|
2270
|
-
if (!isRollbackMarker(marker)) throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
2271
|
-
const sources = sourceEvents(events, marker);
|
|
2272
|
-
if (sources === null) throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
2273
|
-
const tipTurn = turnFromRollbackId(marker.data.id);
|
|
2274
|
-
if (tipTurn === void 0) throw new CheckpointRollbackError("ROLLBACK_RECOVERY_FAILED");
|
|
2275
|
-
const peeled = peelUndoSources(events, sources, tipTurn);
|
|
2276
|
-
const tip = Number(adapter.session.seq) - 1;
|
|
2277
|
-
const completed = countPartialUndoCopies(events, first, journalId, tip);
|
|
2278
|
-
const openCalls = /* @__PURE__ */ new Map();
|
|
2279
|
-
const turn = undoTurnAfterMarker(events, marker.seq);
|
|
2280
|
-
const step = 1;
|
|
2281
|
-
ensureCopiedAssistantToolCalls(
|
|
2282
|
-
adapter.session,
|
|
2283
|
-
events,
|
|
2284
|
-
journalId,
|
|
2285
|
-
completed,
|
|
2286
|
-
turn,
|
|
2287
|
-
step,
|
|
2288
|
-
openCalls
|
|
2289
|
-
);
|
|
2290
|
-
const replaceStart = appendOnly || first.surfaceOp === "append" || first.surfaceOp === void 0 ? marker.seq : SessionSeq(first.surfaceOp.startSeq);
|
|
2291
|
-
const replaceEnd = appendOnly || first.surfaceOp === "append" || first.surfaceOp === void 0 ? marker.seq : SessionSeq(first.surfaceOp.endSeq);
|
|
2292
|
-
this.appendUndo(
|
|
2293
|
-
adapter.session,
|
|
2294
|
-
marker,
|
|
2295
|
-
peeled.undoSources,
|
|
2296
|
-
journalId,
|
|
2297
|
-
appendOnly ? "append" : "replace",
|
|
2298
|
-
replaceStart,
|
|
2299
|
-
replaceEnd,
|
|
2300
|
-
completed,
|
|
2301
|
-
openCalls
|
|
2302
|
-
);
|
|
2303
|
-
if (peeled.nextTip !== void 0 && peeled.nextTip.sources.length > 0) {
|
|
2304
|
-
const live = activeRollbackWindow(adapter.readEvents(), adapter.session.surface.nodes);
|
|
2305
|
-
if (live?.tipTurn !== peeled.nextTip.turn) {
|
|
2306
|
-
this.appendRollbackNotice(adapter, peeled.nextTip.turn, journalId, scopeOfMarker(marker), {
|
|
2307
|
-
surfaceOp: "append",
|
|
2308
|
-
sourceEventSeqs: peeled.nextTip.sources.map((event) => SessionSeq(event.seq))
|
|
2309
|
-
});
|
|
2310
|
-
}
|
|
2311
|
-
}
|
|
2312
|
-
}
|
|
2313
2214
|
async evaluate(request, options = {}) {
|
|
2314
2215
|
const operation = operationOf(request);
|
|
2315
2216
|
await this.options.awaitCaptures?.();
|
|
@@ -2317,7 +2218,8 @@ var CheckpointRollbackService = class {
|
|
|
2317
2218
|
const events = adapter?.readEvents() ?? [];
|
|
2318
2219
|
const shells = undoShellRanges(events);
|
|
2319
2220
|
const checkpoints = await this.options.store.list(request.sessionId);
|
|
2320
|
-
|
|
2221
|
+
const candidates = checkpoints.filter((checkpoint) => checkpoint.turn === request.turn && (checkpoint.files.length > 0 || !isUndoShellCheckpoint(checkpoint.atSeq, shells)));
|
|
2222
|
+
let stored = (request.atSeq === void 0 ? void 0 : candidates.find((checkpoint) => checkpoint.atSeq === request.atSeq)) ?? candidates.find((checkpoint) => checkpoint.files.length > 0) ?? candidates[0] ?? null;
|
|
2321
2223
|
if (stored === null && adapter !== null && operation === "rollback") {
|
|
2322
2224
|
stored = synthesizeCheckpoint({
|
|
2323
2225
|
sessionId: request.sessionId,
|
|
@@ -2354,6 +2256,7 @@ var CheckpointRollbackService = class {
|
|
|
2354
2256
|
}
|
|
2355
2257
|
let messages = [];
|
|
2356
2258
|
let surface = null;
|
|
2259
|
+
const liveCut = await this.readCut(request.sessionId);
|
|
2357
2260
|
if (stored !== null && adapter !== null && stored.atSeq >= 0) {
|
|
2358
2261
|
const events2 = adapter.readEvents();
|
|
2359
2262
|
const cut = resolveRollbackCut(events2, stored.turn, stored.atSeq);
|
|
@@ -2363,165 +2266,102 @@ var CheckpointRollbackService = class {
|
|
|
2363
2266
|
if (events2.some((event) => Number(event.seq) > cut.cutSeq && typeof event.surfaceOp === "object" && !isOwnReplacement(event))) {
|
|
2364
2267
|
unavailable.push("compacted-surface");
|
|
2365
2268
|
}
|
|
2366
|
-
const
|
|
2367
|
-
const
|
|
2368
|
-
if (
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
seq: event.seq,
|
|
2378
|
-
type: event.type,
|
|
2379
|
-
text: textOf(event)
|
|
2380
|
-
}));
|
|
2381
|
-
surface = {
|
|
2382
|
-
operation,
|
|
2383
|
-
turn: stored.turn,
|
|
2384
|
-
nodes: [SessionSeq(marker.seq)],
|
|
2385
|
-
alreadyRestored: false,
|
|
2386
|
-
forwardFromTip: { marker, reappear, stillWithdraw }
|
|
2387
|
-
};
|
|
2388
|
-
}
|
|
2269
|
+
const withdrawn = events2.filter((event) => Number(event.seq) > cut.cutSeq && surfaceEventTypes.has(event.type) && !isProtectedSystemMessage(event));
|
|
2270
|
+
const alreadyRestored = liveCut !== null && liveCut.hideTurnsFrom === stored.turn && liveCut.cutSeq === cut.cutSeq && liveCut.restoredAtSeq === Number(adapter.session.seq);
|
|
2271
|
+
if (withdrawn.length === 0 || alreadyRestored) {
|
|
2272
|
+
unavailable.push("already-restored");
|
|
2273
|
+
surface = {
|
|
2274
|
+
operation,
|
|
2275
|
+
turn: stored.turn,
|
|
2276
|
+
cutSeq: cut.cutSeq,
|
|
2277
|
+
alreadyRestored: true
|
|
2278
|
+
};
|
|
2279
|
+
messages = [];
|
|
2389
2280
|
} else {
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
|
|
2399
|
-
|
|
2400
|
-
|
|
2401
|
-
surface = { operation, turn: stored.turn, nodes, alreadyRestored };
|
|
2402
|
-
messages = alreadyRestored ? [] : nodes.map((node) => ({
|
|
2403
|
-
seq: node,
|
|
2404
|
-
type: events2[node].type,
|
|
2405
|
-
text: textOf(events2[node])
|
|
2406
|
-
}));
|
|
2407
|
-
}
|
|
2281
|
+
surface = {
|
|
2282
|
+
operation,
|
|
2283
|
+
turn: stored.turn,
|
|
2284
|
+
cutSeq: cut.cutSeq,
|
|
2285
|
+
alreadyRestored: false
|
|
2286
|
+
};
|
|
2287
|
+
messages = withdrawn.map((event) => ({
|
|
2288
|
+
seq: Number(event.seq),
|
|
2289
|
+
type: event.type,
|
|
2290
|
+
text: textOf(event)
|
|
2291
|
+
}));
|
|
2408
2292
|
}
|
|
2293
|
+
} else if (liveCut === null) {
|
|
2294
|
+
unavailable.push("session-changed");
|
|
2409
2295
|
} else {
|
|
2410
|
-
const
|
|
2411
|
-
|
|
2412
|
-
|
|
2413
|
-
|
|
2414
|
-
|
|
2415
|
-
|
|
2416
|
-
|
|
2417
|
-
|
|
2418
|
-
|
|
2419
|
-
|
|
2420
|
-
|
|
2421
|
-
|
|
2422
|
-
|
|
2296
|
+
const tipTurn = liveCut.hideTurnsFrom;
|
|
2297
|
+
let undoStored = tipTurn === stored.turn ? stored : checkpoints.find((item) => item.turn === tipTurn && !isUndoShellCheckpoint(item.atSeq, shells)) ?? null;
|
|
2298
|
+
if (undoStored === null) {
|
|
2299
|
+
undoStored = synthesizeCheckpoint({
|
|
2300
|
+
sessionId: request.sessionId,
|
|
2301
|
+
session: {
|
|
2302
|
+
format: adapter.session.header.version,
|
|
2303
|
+
preset: adapter.session.header.agentPreset ?? null
|
|
2304
|
+
},
|
|
2305
|
+
pluginVersion: this.options.pluginVersion,
|
|
2306
|
+
turn: tipTurn,
|
|
2307
|
+
events: events2,
|
|
2308
|
+
createId: this.options.createId,
|
|
2309
|
+
now: this.options.now
|
|
2310
|
+
});
|
|
2423
2311
|
}
|
|
2424
|
-
|
|
2425
|
-
|
|
2426
|
-
unavailable.push("session-changed");
|
|
2312
|
+
if (undoStored === null) {
|
|
2313
|
+
unavailable.push("checkpoint-not-found");
|
|
2427
2314
|
} else {
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
|
|
2435
|
-
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
events: events2,
|
|
2439
|
-
createId: this.options.createId,
|
|
2440
|
-
now: this.options.now
|
|
2441
|
-
});
|
|
2442
|
-
}
|
|
2443
|
-
if (undoStored === null) {
|
|
2444
|
-
unavailable.push("checkpoint-not-found");
|
|
2445
|
-
} else {
|
|
2446
|
-
if (tipTurn !== stored.turn) {
|
|
2447
|
-
const drop = /* @__PURE__ */ new Set([
|
|
2448
|
-
"checkpoint-not-found",
|
|
2449
|
-
"incomplete-checkpoint",
|
|
2450
|
-
"prompt-non-text-content",
|
|
2451
|
-
"workspace-capture-failed",
|
|
2452
|
-
"incompatible-session"
|
|
2453
|
-
]);
|
|
2454
|
-
for (let index = unavailable.length - 1; index >= 0; index -= 1) {
|
|
2455
|
-
if (drop.has(unavailable[index])) unavailable.splice(index, 1);
|
|
2456
|
-
}
|
|
2457
|
-
if (undoStored.coverage !== "complete" || undoStored.gaps.length > 0) {
|
|
2458
|
-
unavailable.push("incomplete-checkpoint");
|
|
2459
|
-
for (const gap of undoStored.gaps) {
|
|
2460
|
-
unavailable.push(gap.type === "prompt" ? "prompt-non-text-content" : "workspace-capture-failed");
|
|
2461
|
-
}
|
|
2462
|
-
}
|
|
2463
|
-
if (undoStored.session.format !== adapter.session.header.version) {
|
|
2464
|
-
unavailable.push("incompatible-session");
|
|
2465
|
-
}
|
|
2315
|
+
if (tipTurn !== stored.turn) {
|
|
2316
|
+
const drop = /* @__PURE__ */ new Set([
|
|
2317
|
+
"checkpoint-not-found",
|
|
2318
|
+
"incomplete-checkpoint",
|
|
2319
|
+
"prompt-non-text-content",
|
|
2320
|
+
"workspace-capture-failed",
|
|
2321
|
+
"incompatible-session"
|
|
2322
|
+
]);
|
|
2323
|
+
for (let index = unavailable.length - 1; index >= 0; index -= 1) {
|
|
2324
|
+
if (drop.has(unavailable[index])) unavailable.splice(index, 1);
|
|
2466
2325
|
}
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
} else {
|
|
2472
|
-
const markerOnSurface = currentNodes.some((node) => Number(node) === Number(marker.seq));
|
|
2473
|
-
let mode = "replace";
|
|
2474
|
-
let replaceStartSeq = marker.seq;
|
|
2475
|
-
let replaceEndSeq = undoReplaceEndSeq(events2, currentNodes, marker.seq);
|
|
2476
|
-
if (!markerOnSurface) {
|
|
2477
|
-
const tipCut = resolveRollbackCut(events2, tipTurn, undoStored.atSeq);
|
|
2478
|
-
const afterCut = tipCut === null ? [] : currentNodes.filter((node) => {
|
|
2479
|
-
const event = events2[node];
|
|
2480
|
-
if (ignoreOnUndoKeepPrefix(event)) return false;
|
|
2481
|
-
return Number(logicalSeq(event)) > tipCut.cutSeq;
|
|
2482
|
-
});
|
|
2483
|
-
if (afterCut.length === 0) {
|
|
2484
|
-
mode = "append";
|
|
2485
|
-
const last = currentNodes[currentNodes.length - 1];
|
|
2486
|
-
replaceStartSeq = last === void 0 ? marker.seq : SessionSeq(last);
|
|
2487
|
-
replaceEndSeq = replaceStartSeq;
|
|
2488
|
-
} else {
|
|
2489
|
-
replaceStartSeq = SessionSeq(afterCut[0]);
|
|
2490
|
-
replaceEndSeq = SessionSeq(afterCut[afterCut.length - 1]);
|
|
2491
|
-
}
|
|
2326
|
+
if (undoStored.coverage !== "complete" || undoStored.gaps.length > 0) {
|
|
2327
|
+
unavailable.push("incomplete-checkpoint");
|
|
2328
|
+
for (const gap of undoStored.gaps) {
|
|
2329
|
+
unavailable.push(gap.type === "prompt" ? "prompt-non-text-content" : "workspace-capture-failed");
|
|
2492
2330
|
}
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
|
|
2496
|
-
marker,
|
|
2497
|
-
sources: peeled.undoSources,
|
|
2498
|
-
mode,
|
|
2499
|
-
replaceStartSeq,
|
|
2500
|
-
replaceEndSeq,
|
|
2501
|
-
...peeled.nextTip !== void 0 ? { nextTip: peeled.nextTip } : {}
|
|
2502
|
-
};
|
|
2503
|
-
messages = peeled.undoSources.map((event) => ({
|
|
2504
|
-
seq: event.seq,
|
|
2505
|
-
type: event.type,
|
|
2506
|
-
text: textOf(event)
|
|
2507
|
-
}));
|
|
2331
|
+
}
|
|
2332
|
+
if (undoStored.session.format !== adapter.session.header.version) {
|
|
2333
|
+
unavailable.push("incompatible-session");
|
|
2508
2334
|
}
|
|
2509
2335
|
}
|
|
2336
|
+
stored = undoStored;
|
|
2337
|
+
surface = {
|
|
2338
|
+
operation,
|
|
2339
|
+
tipTurn,
|
|
2340
|
+
live: liveCut,
|
|
2341
|
+
restoreCut: liveCut.previous
|
|
2342
|
+
};
|
|
2343
|
+
messages = events2.filter((event) => Number(event.seq) > liveCut.cutSeq && Number(event.seq) <= liveCut.restoredAtSeq && surfaceEventTypes.has(event.type) && !isProtectedSystemMessage(event)).map((event) => ({
|
|
2344
|
+
seq: Number(event.seq),
|
|
2345
|
+
type: event.type,
|
|
2346
|
+
text: textOf(event)
|
|
2347
|
+
}));
|
|
2510
2348
|
}
|
|
2511
2349
|
}
|
|
2512
2350
|
}
|
|
2351
|
+
const trackedFiles = stored === null || adapter === null || surface === null ? [] : this.trackedFilesForRestore(checkpoints, adapter, surface, liveCut);
|
|
2513
2352
|
let conflicts = [];
|
|
2514
2353
|
if (stored !== null && adapter !== null) {
|
|
2515
2354
|
try {
|
|
2516
|
-
conflicts = await this.files(adapter, stored, "preview").verifyCurrent(
|
|
2355
|
+
conflicts = await this.files(adapter, stored, "preview").verifyCurrent(trackedFiles, directionOf(operation));
|
|
2517
2356
|
} catch (error) {
|
|
2518
2357
|
if (error instanceof CheckpointFileError) conflicts = error.conflicts;
|
|
2519
2358
|
else throw error;
|
|
2520
2359
|
}
|
|
2521
2360
|
}
|
|
2522
|
-
const
|
|
2523
|
-
const tipWindow = adapter !== null ? activeRollbackWindow(adapter.readEvents(), adapter.session.surface.nodes) : void 0;
|
|
2524
|
-
const previewTurn = surface?.operation === "undo" ?
|
|
2361
|
+
const cutWindow = windowFromCut(liveCut);
|
|
2362
|
+
const tipWindow = adapter !== null ? cutWindow ?? activeRollbackWindow(adapter.readEvents(), adapter.session.surface.nodes) : void 0;
|
|
2363
|
+
const previewTurn = surface?.operation === "undo" ? surface.tipTurn : request.turn;
|
|
2364
|
+
const undoScope = surface?.operation === "undo" ? surface.live.scope : void 0;
|
|
2525
2365
|
return {
|
|
2526
2366
|
checkpoint: stored,
|
|
2527
2367
|
adapter,
|
|
@@ -2529,16 +2369,46 @@ var CheckpointRollbackService = class {
|
|
|
2529
2369
|
preview: {
|
|
2530
2370
|
sessionId: request.sessionId,
|
|
2531
2371
|
turn: previewTurn,
|
|
2372
|
+
...stored === null ? {} : { atSeq: stored.atSeq },
|
|
2532
2373
|
...operation === "undo" ? { operation } : {},
|
|
2533
|
-
...
|
|
2374
|
+
...undoScope === void 0 ? {} : { scope: undoScope },
|
|
2534
2375
|
...tipWindow === void 0 ? {} : { tipTurn: tipWindow.tipTurn },
|
|
2535
2376
|
messages,
|
|
2536
|
-
files:
|
|
2377
|
+
files: trackedFiles.map((file) => ({ path: file.path, change: file.change })),
|
|
2537
2378
|
conflicts,
|
|
2538
2379
|
unavailable: [...new Set(unavailable)]
|
|
2539
2380
|
}
|
|
2540
2381
|
};
|
|
2541
2382
|
}
|
|
2383
|
+
/**
|
|
2384
|
+
* Tracked text files for this restore/undo.
|
|
2385
|
+
* Rollback with no live cut: target turn → tip.
|
|
2386
|
+
* Rollback while a later cut is live: only the gap down to that tip (already restored).
|
|
2387
|
+
* Undo: reverse only the segment this tip added (down to previous tip, or open-ended).
|
|
2388
|
+
*/
|
|
2389
|
+
trackedFilesForRestore(checkpoints, adapter, surface, liveCut) {
|
|
2390
|
+
const shells = undoShellRanges(adapter.readEvents());
|
|
2391
|
+
const real = checkpoints.filter((checkpoint) => checkpoint.files.length > 0 || !isUndoShellCheckpoint(checkpoint.atSeq, shells));
|
|
2392
|
+
if (surface.operation === "undo") {
|
|
2393
|
+
const previous = liveCut?.previous ?? null;
|
|
2394
|
+
return chainTrackedFiles(real, {
|
|
2395
|
+
fromTurn: surface.tipTurn,
|
|
2396
|
+
fromAtSeq: surface.live.cutSeq,
|
|
2397
|
+
...previous !== null && previous.hideTurnsFrom > surface.tipTurn ? {
|
|
2398
|
+
untilTurnExclusive: previous.hideTurnsFrom,
|
|
2399
|
+
untilAtSeqExclusive: previous.cutSeq
|
|
2400
|
+
} : {}
|
|
2401
|
+
});
|
|
2402
|
+
}
|
|
2403
|
+
return chainTrackedFiles(real, {
|
|
2404
|
+
fromTurn: surface.turn,
|
|
2405
|
+
fromAtSeq: surface.cutSeq,
|
|
2406
|
+
...liveCut !== null && liveCut.hideTurnsFrom > surface.turn ? {
|
|
2407
|
+
untilTurnExclusive: liveCut.hideTurnsFrom,
|
|
2408
|
+
untilAtSeqExclusive: liveCut.cutSeq
|
|
2409
|
+
} : {}
|
|
2410
|
+
});
|
|
2411
|
+
}
|
|
2542
2412
|
effectiveScope(request, evaluation) {
|
|
2543
2413
|
if (operationOf(request) === "undo") {
|
|
2544
2414
|
return evaluation.preview.scope ?? "conversation-and-files";
|
|
@@ -2592,71 +2462,90 @@ var CheckpointRollbackService = class {
|
|
|
2592
2462
|
};
|
|
2593
2463
|
|
|
2594
2464
|
// src/host/checkpoint/store.ts
|
|
2595
|
-
import { createHash as
|
|
2596
|
-
import { chmod as
|
|
2597
|
-
import { basename as
|
|
2598
|
-
import { z } from "zod";
|
|
2465
|
+
import { createHash as createHash3, randomUUID as randomUUID5 } from "node:crypto";
|
|
2466
|
+
import { chmod as chmod3, mkdir as mkdir3, readdir as readdir2, readFile as readFile3, rename as rename3, unlink as unlink3, writeFile as writeFile3 } from "node:fs/promises";
|
|
2467
|
+
import { basename as basename3, join as join5 } from "node:path";
|
|
2468
|
+
import { z as z2 } from "zod";
|
|
2599
2469
|
var MAX_CHECKPOINTS = 20;
|
|
2600
2470
|
var checkpointQueues = /* @__PURE__ */ new Map();
|
|
2601
2471
|
var journalQueues = /* @__PURE__ */ new Map();
|
|
2602
|
-
|
|
2603
|
-
|
|
2472
|
+
function byStoreOrder(left, right) {
|
|
2473
|
+
return left.createdAt - right.createdAt || left.atSeq - right.atSeq || left.turn - right.turn || left.id.localeCompare(right.id);
|
|
2474
|
+
}
|
|
2475
|
+
function retainCheckpoints(checkpoints, max = MAX_CHECKPOINTS) {
|
|
2476
|
+
if (checkpoints.length <= max) return [...checkpoints].sort(byStoreOrder);
|
|
2477
|
+
const rich = checkpoints.filter((checkpoint) => checkpoint.files.length > 0);
|
|
2478
|
+
const empty = checkpoints.filter((checkpoint) => checkpoint.files.length === 0);
|
|
2479
|
+
const emptyOldestFirst = [...empty].sort(byStoreOrder);
|
|
2480
|
+
const dropEmpty = Math.max(0, checkpoints.length - max);
|
|
2481
|
+
const keptEmpty = emptyOldestFirst.slice(dropEmpty);
|
|
2482
|
+
let kept = [...rich, ...keptEmpty];
|
|
2483
|
+
if (kept.length > max) {
|
|
2484
|
+
kept = [...kept].sort(byStoreOrder).slice(kept.length - max);
|
|
2485
|
+
}
|
|
2486
|
+
return kept.sort(byStoreOrder);
|
|
2487
|
+
}
|
|
2488
|
+
async function serialize3(queues2, key, operation) {
|
|
2489
|
+
const previous = queues2.get(key) ?? Promise.resolve();
|
|
2604
2490
|
const result = previous.then(operation);
|
|
2605
2491
|
const tail = result.then(() => void 0, () => void 0);
|
|
2606
|
-
|
|
2492
|
+
queues2.set(key, tail);
|
|
2607
2493
|
try {
|
|
2608
2494
|
return await result;
|
|
2609
2495
|
} finally {
|
|
2610
|
-
if (
|
|
2496
|
+
if (queues2.get(key) === tail) queues2.delete(key);
|
|
2611
2497
|
}
|
|
2612
2498
|
}
|
|
2613
|
-
var
|
|
2614
|
-
var gapSchema =
|
|
2615
|
-
|
|
2616
|
-
|
|
2499
|
+
var sessionIdSchema2 = z2.string().min(1).transform((value) => value);
|
|
2500
|
+
var gapSchema = z2.discriminatedUnion("type", [
|
|
2501
|
+
z2.object({ type: z2.literal("prompt"), reason: z2.literal("non-text-content") }).strict(),
|
|
2502
|
+
z2.object({ type: z2.literal("workspace"), reason: z2.literal("capture-failed") }).strict()
|
|
2617
2503
|
]);
|
|
2618
|
-
var
|
|
2619
|
-
path:
|
|
2620
|
-
change:
|
|
2621
|
-
beforeText:
|
|
2622
|
-
beforeHash:
|
|
2623
|
-
afterText:
|
|
2624
|
-
afterHash:
|
|
2504
|
+
var fileSchema2 = z2.object({
|
|
2505
|
+
path: z2.string().min(1),
|
|
2506
|
+
change: z2.enum(["create", "modify", "delete"]),
|
|
2507
|
+
beforeText: z2.string().nullable(),
|
|
2508
|
+
beforeHash: z2.string(),
|
|
2509
|
+
afterText: z2.string().nullable(),
|
|
2510
|
+
afterHash: z2.string()
|
|
2625
2511
|
}).strict().superRefine((file, context) => {
|
|
2626
2512
|
const validBefore = file.change === "create" ? file.beforeText === null : file.beforeText !== null;
|
|
2627
2513
|
if (!validBefore) context.addIssue({ code: "custom", path: ["beforeText"], message: "Invalid before-image" });
|
|
2628
2514
|
const validAfter = file.change === "delete" ? file.afterText === null : file.afterText !== null;
|
|
2629
2515
|
if (!validAfter) context.addIssue({ code: "custom", path: ["afterText"], message: "Invalid after-image" });
|
|
2630
2516
|
});
|
|
2631
|
-
var checkpointSchema =
|
|
2632
|
-
schemaVersion:
|
|
2633
|
-
pluginVersion:
|
|
2634
|
-
id:
|
|
2635
|
-
sessionId:
|
|
2636
|
-
session:
|
|
2637
|
-
turn:
|
|
2638
|
-
atSeq:
|
|
2639
|
-
prompt:
|
|
2640
|
-
createdAt:
|
|
2641
|
-
stage:
|
|
2642
|
-
coverage:
|
|
2643
|
-
gaps:
|
|
2644
|
-
files:
|
|
2517
|
+
var checkpointSchema = z2.object({
|
|
2518
|
+
schemaVersion: z2.literal(CHECKPOINT_SCHEMA_VERSION),
|
|
2519
|
+
pluginVersion: z2.string().min(1),
|
|
2520
|
+
id: z2.string().min(1),
|
|
2521
|
+
sessionId: sessionIdSchema2,
|
|
2522
|
+
session: z2.object({ format: z2.number().int().positive(), preset: z2.string().nullable() }).strict(),
|
|
2523
|
+
turn: z2.number().int().positive(),
|
|
2524
|
+
atSeq: z2.number().int().nonnegative(),
|
|
2525
|
+
prompt: z2.string(),
|
|
2526
|
+
createdAt: z2.number().int().nonnegative(),
|
|
2527
|
+
stage: z2.literal("committed"),
|
|
2528
|
+
coverage: z2.enum(["complete", "partial"]),
|
|
2529
|
+
gaps: z2.array(gapSchema),
|
|
2530
|
+
files: z2.array(fileSchema2),
|
|
2531
|
+
/** Legacy shadow-git fields (ignored; stripped on save). */
|
|
2532
|
+
shadowBeforeCommit: z2.string().min(7).optional(),
|
|
2533
|
+
shadowAfterCommit: z2.string().min(7).optional()
|
|
2645
2534
|
}).strict().superRefine((checkpoint, context) => {
|
|
2646
2535
|
const valid = checkpoint.coverage === "complete" ? checkpoint.gaps.length === 0 : checkpoint.gaps.length > 0;
|
|
2647
2536
|
if (!valid) context.addIssue({ code: "custom", path: ["gaps"], message: "Coverage does not match gaps" });
|
|
2648
2537
|
});
|
|
2649
|
-
var storeSchema =
|
|
2650
|
-
schemaVersion:
|
|
2651
|
-
checkpoints:
|
|
2538
|
+
var storeSchema = z2.object({
|
|
2539
|
+
schemaVersion: z2.literal(CHECKPOINT_SCHEMA_VERSION),
|
|
2540
|
+
checkpoints: z2.array(checkpointSchema).max(MAX_CHECKPOINTS)
|
|
2652
2541
|
}).strict();
|
|
2653
|
-
var journalSchema =
|
|
2654
|
-
schemaVersion:
|
|
2655
|
-
id:
|
|
2656
|
-
sessionId:
|
|
2657
|
-
checkpointId:
|
|
2658
|
-
status:
|
|
2659
|
-
createdAt:
|
|
2542
|
+
var journalSchema = z2.object({
|
|
2543
|
+
schemaVersion: z2.literal(CHECKPOINT_SCHEMA_VERSION),
|
|
2544
|
+
id: z2.string().min(1),
|
|
2545
|
+
sessionId: sessionIdSchema2,
|
|
2546
|
+
checkpointId: z2.string().min(1),
|
|
2547
|
+
status: z2.enum(["prepared", "files-restored", "completed", "compensated"]),
|
|
2548
|
+
createdAt: z2.number().int().nonnegative()
|
|
2660
2549
|
}).strict();
|
|
2661
2550
|
var CheckpointStoreError = class extends Error {
|
|
2662
2551
|
constructor(code) {
|
|
@@ -2669,11 +2558,11 @@ var CheckpointStoreError = class extends Error {
|
|
|
2669
2558
|
function isRecord2(value) {
|
|
2670
2559
|
return typeof value === "object" && value !== null;
|
|
2671
2560
|
}
|
|
2672
|
-
function
|
|
2561
|
+
function isMissing3(error) {
|
|
2673
2562
|
return isRecord2(error) && error.code === "ENOENT";
|
|
2674
2563
|
}
|
|
2675
|
-
function
|
|
2676
|
-
return
|
|
2564
|
+
function digest3(value) {
|
|
2565
|
+
return createHash3("sha256").update(value).digest("hex");
|
|
2677
2566
|
}
|
|
2678
2567
|
function assertCheckpointSchema(value) {
|
|
2679
2568
|
if (isRecord2(value) && value.schemaVersion !== CHECKPOINT_SCHEMA_VERSION) {
|
|
@@ -2687,29 +2576,29 @@ function assertStoredSchema(value) {
|
|
|
2687
2576
|
for (const checkpoint of value.checkpoints) assertCheckpointSchema(checkpoint);
|
|
2688
2577
|
}
|
|
2689
2578
|
}
|
|
2690
|
-
async function
|
|
2579
|
+
async function removeIfPresent3(path) {
|
|
2691
2580
|
try {
|
|
2692
|
-
await
|
|
2581
|
+
await unlink3(path);
|
|
2693
2582
|
} catch (error) {
|
|
2694
|
-
if (!
|
|
2583
|
+
if (!isMissing3(error)) throw error;
|
|
2695
2584
|
}
|
|
2696
2585
|
}
|
|
2697
|
-
async function
|
|
2698
|
-
const temporary =
|
|
2586
|
+
async function writeAtomic3(path, value) {
|
|
2587
|
+
const temporary = join5(join5(path, ".."), `.${basename3(path)}.${randomUUID5()}.tmp`);
|
|
2699
2588
|
try {
|
|
2700
|
-
await
|
|
2701
|
-
await
|
|
2702
|
-
await
|
|
2589
|
+
await writeFile3(temporary, JSON.stringify(value), { encoding: "utf8", mode: 384, flag: "wx" });
|
|
2590
|
+
await rename3(temporary, path);
|
|
2591
|
+
await chmod3(path, 384);
|
|
2703
2592
|
} finally {
|
|
2704
|
-
await
|
|
2593
|
+
await removeIfPresent3(temporary);
|
|
2705
2594
|
}
|
|
2706
2595
|
}
|
|
2707
2596
|
var CheckpointStore = class {
|
|
2708
2597
|
constructor(root) {
|
|
2709
2598
|
this.root = root;
|
|
2710
|
-
this.checkpointDirectory =
|
|
2711
|
-
this.journalDirectory =
|
|
2712
|
-
this.rootKey =
|
|
2599
|
+
this.checkpointDirectory = join5(root, "checkpoints");
|
|
2600
|
+
this.journalDirectory = join5(root, "journals");
|
|
2601
|
+
this.rootKey = join5(root, ".");
|
|
2713
2602
|
}
|
|
2714
2603
|
root;
|
|
2715
2604
|
checkpointDirectory;
|
|
@@ -2719,16 +2608,15 @@ var CheckpointStore = class {
|
|
|
2719
2608
|
assertCheckpointSchema(checkpoint);
|
|
2720
2609
|
const parsed = checkpointSchema.safeParse(checkpoint);
|
|
2721
2610
|
if (!parsed.success) throw new CheckpointStoreError("INVALID_CHECKPOINT");
|
|
2722
|
-
await
|
|
2611
|
+
await serialize3(checkpointQueues, this.checkpointKey(checkpoint.sessionId), async () => {
|
|
2723
2612
|
const checkpoints = await this.list(checkpoint.sessionId);
|
|
2724
2613
|
const existing = checkpoints.findIndex((value) => value.id === checkpoint.id);
|
|
2725
2614
|
if (existing >= 0) checkpoints.splice(existing, 1, parsed.data);
|
|
2726
2615
|
else checkpoints.push(parsed.data);
|
|
2727
|
-
checkpoints.sort((left, right) => left.createdAt - right.createdAt || left.turn - right.turn || left.id.localeCompare(right.id));
|
|
2728
2616
|
await this.ensureDirectories();
|
|
2729
|
-
await
|
|
2617
|
+
await writeAtomic3(this.checkpointPath(checkpoint.sessionId), {
|
|
2730
2618
|
schemaVersion: CHECKPOINT_SCHEMA_VERSION,
|
|
2731
|
-
checkpoints: checkpoints
|
|
2619
|
+
checkpoints: retainCheckpoints(checkpoints, MAX_CHECKPOINTS)
|
|
2732
2620
|
});
|
|
2733
2621
|
});
|
|
2734
2622
|
}
|
|
@@ -2738,7 +2626,7 @@ var CheckpointStore = class {
|
|
|
2738
2626
|
try {
|
|
2739
2627
|
stored = await this.readJson(this.checkpointPath(sessionId));
|
|
2740
2628
|
} catch (error) {
|
|
2741
|
-
if (
|
|
2629
|
+
if (isMissing3(error)) return [];
|
|
2742
2630
|
throw error;
|
|
2743
2631
|
}
|
|
2744
2632
|
assertStoredSchema(stored);
|
|
@@ -2751,11 +2639,11 @@ var CheckpointStore = class {
|
|
|
2751
2639
|
* Empty list removes the session file.
|
|
2752
2640
|
*/
|
|
2753
2641
|
async replace(sessionId, checkpoints) {
|
|
2754
|
-
await
|
|
2642
|
+
await serialize3(checkpointQueues, this.checkpointKey(sessionId), async () => {
|
|
2755
2643
|
await this.ensureDirectories();
|
|
2756
2644
|
const path = this.checkpointPath(sessionId);
|
|
2757
2645
|
if (checkpoints.length === 0) {
|
|
2758
|
-
await
|
|
2646
|
+
await removeIfPresent3(path);
|
|
2759
2647
|
return;
|
|
2760
2648
|
}
|
|
2761
2649
|
const next = [];
|
|
@@ -2766,43 +2654,42 @@ var CheckpointStore = class {
|
|
|
2766
2654
|
if (parsed.data.sessionId !== sessionId) throw new CheckpointStoreError("INVALID_CHECKPOINT");
|
|
2767
2655
|
next.push(parsed.data);
|
|
2768
2656
|
}
|
|
2769
|
-
|
|
2770
|
-
await writeAtomic2(path, {
|
|
2657
|
+
await writeAtomic3(path, {
|
|
2771
2658
|
schemaVersion: CHECKPOINT_SCHEMA_VERSION,
|
|
2772
|
-
checkpoints: next
|
|
2659
|
+
checkpoints: retainCheckpoints(next, MAX_CHECKPOINTS)
|
|
2773
2660
|
});
|
|
2774
2661
|
});
|
|
2775
2662
|
}
|
|
2776
2663
|
/** Drop settled journal files left behind after compensate / complete races. */
|
|
2777
2664
|
async purgeSettledJournals() {
|
|
2778
|
-
return
|
|
2665
|
+
return serialize3(journalQueues, this.rootKey, async () => {
|
|
2779
2666
|
await this.ensureDirectories();
|
|
2780
2667
|
let names;
|
|
2781
2668
|
try {
|
|
2782
2669
|
names = await readdir2(this.journalDirectory);
|
|
2783
2670
|
} catch (error) {
|
|
2784
|
-
if (
|
|
2671
|
+
if (isMissing3(error)) return 0;
|
|
2785
2672
|
throw error;
|
|
2786
2673
|
}
|
|
2787
2674
|
let removed = 0;
|
|
2788
2675
|
for (const name2 of names) {
|
|
2789
2676
|
if (!name2.endsWith(".json")) continue;
|
|
2790
|
-
const path =
|
|
2677
|
+
const path = join5(this.journalDirectory, name2);
|
|
2791
2678
|
const stored = await this.readJson(path);
|
|
2792
2679
|
assertCheckpointSchema(stored);
|
|
2793
2680
|
const parsed = journalSchema.safeParse(stored);
|
|
2794
2681
|
if (!parsed.success) continue;
|
|
2795
2682
|
if (parsed.data.status === "prepared" || parsed.data.status === "files-restored") continue;
|
|
2796
|
-
await
|
|
2683
|
+
await removeIfPresent3(path);
|
|
2797
2684
|
removed += 1;
|
|
2798
2685
|
}
|
|
2799
2686
|
return removed;
|
|
2800
2687
|
});
|
|
2801
2688
|
}
|
|
2802
2689
|
async recoverInterruptedWrites(sessionId) {
|
|
2803
|
-
await
|
|
2690
|
+
await serialize3(checkpointQueues, this.checkpointKey(sessionId), async () => {
|
|
2804
2691
|
await this.ensureDirectories();
|
|
2805
|
-
const prefix = `.${
|
|
2692
|
+
const prefix = `.${basename3(this.checkpointPath(sessionId))}.`;
|
|
2806
2693
|
await this.cleanTemporaryFiles(this.checkpointDirectory, prefix);
|
|
2807
2694
|
});
|
|
2808
2695
|
}
|
|
@@ -2810,22 +2697,22 @@ var CheckpointStore = class {
|
|
|
2810
2697
|
assertCheckpointSchema(journal);
|
|
2811
2698
|
const parsed = journalSchema.safeParse(journal);
|
|
2812
2699
|
if (!parsed.success) throw new CheckpointStoreError("INVALID_JOURNAL");
|
|
2813
|
-
await
|
|
2700
|
+
await serialize3(journalQueues, this.rootKey, async () => {
|
|
2814
2701
|
await this.ensureDirectories();
|
|
2815
|
-
await
|
|
2702
|
+
await writeAtomic3(join5(this.journalDirectory, `${digest3(journal.id)}.json`), parsed.data);
|
|
2816
2703
|
});
|
|
2817
2704
|
}
|
|
2818
2705
|
async completeJournal(id) {
|
|
2819
|
-
await
|
|
2706
|
+
await serialize3(journalQueues, this.rootKey, async () => {
|
|
2820
2707
|
await this.ensureDirectories();
|
|
2821
|
-
await
|
|
2708
|
+
await removeIfPresent3(join5(this.journalDirectory, `${digest3(id)}.json`));
|
|
2822
2709
|
});
|
|
2823
2710
|
}
|
|
2824
2711
|
async listPendingJournals() {
|
|
2825
2712
|
return this.readPendingJournals();
|
|
2826
2713
|
}
|
|
2827
2714
|
async recoverJournal() {
|
|
2828
|
-
return
|
|
2715
|
+
return serialize3(journalQueues, this.rootKey, async () => {
|
|
2829
2716
|
await this.ensureDirectories();
|
|
2830
2717
|
await this.cleanTemporaryFiles(this.journalDirectory);
|
|
2831
2718
|
return this.readPendingJournals();
|
|
@@ -2836,13 +2723,13 @@ var CheckpointStore = class {
|
|
|
2836
2723
|
try {
|
|
2837
2724
|
names = await readdir2(this.journalDirectory);
|
|
2838
2725
|
} catch (error) {
|
|
2839
|
-
if (
|
|
2726
|
+
if (isMissing3(error)) return [];
|
|
2840
2727
|
throw error;
|
|
2841
2728
|
}
|
|
2842
2729
|
const journals = [];
|
|
2843
2730
|
for (const name2 of names.sort()) {
|
|
2844
2731
|
if (!name2.endsWith(".json")) continue;
|
|
2845
|
-
const stored = await this.readJson(
|
|
2732
|
+
const stored = await this.readJson(join5(this.journalDirectory, name2));
|
|
2846
2733
|
assertCheckpointSchema(stored);
|
|
2847
2734
|
const parsed = journalSchema.safeParse(stored);
|
|
2848
2735
|
if (!parsed.success) throw new CheckpointStoreError("INVALID_STORE");
|
|
@@ -2854,20 +2741,20 @@ var CheckpointStore = class {
|
|
|
2854
2741
|
return `${this.rootKey}\0${sessionId}`;
|
|
2855
2742
|
}
|
|
2856
2743
|
checkpointPath(sessionId) {
|
|
2857
|
-
return
|
|
2744
|
+
return join5(this.checkpointDirectory, `${digest3(sessionId)}.json`);
|
|
2858
2745
|
}
|
|
2859
2746
|
async ensureDirectories() {
|
|
2860
|
-
await
|
|
2861
|
-
await
|
|
2862
|
-
await
|
|
2747
|
+
await mkdir3(this.root, { recursive: true, mode: 448 });
|
|
2748
|
+
await mkdir3(this.checkpointDirectory, { recursive: true, mode: 448 });
|
|
2749
|
+
await mkdir3(this.journalDirectory, { recursive: true, mode: 448 });
|
|
2863
2750
|
}
|
|
2864
2751
|
async cleanTemporaryFiles(directory, prefix = "") {
|
|
2865
2752
|
for (const name2 of await readdir2(directory)) {
|
|
2866
|
-
if (name2.startsWith(prefix) && name2.endsWith(".tmp")) await
|
|
2753
|
+
if (name2.startsWith(prefix) && name2.endsWith(".tmp")) await unlink3(join5(directory, name2));
|
|
2867
2754
|
}
|
|
2868
2755
|
}
|
|
2869
2756
|
async readJson(path) {
|
|
2870
|
-
const text3 = await
|
|
2757
|
+
const text3 = await readFile3(path, "utf8");
|
|
2871
2758
|
try {
|
|
2872
2759
|
return JSON.parse(text3);
|
|
2873
2760
|
} catch {
|
|
@@ -2963,11 +2850,8 @@ function createClassifier(llm, route) {
|
|
|
2963
2850
|
}
|
|
2964
2851
|
return async (text3, signal) => withUsageSource("autoClassifier", async () => {
|
|
2965
2852
|
const timeout = new AbortController();
|
|
2966
|
-
const
|
|
2967
|
-
|
|
2968
|
-
}, CLASSIFIER_TIMEOUT_MS);
|
|
2969
|
-
const unlink4 = signal === void 0 ? void 0 : linkAbortSignal(signal, timeout);
|
|
2970
|
-
try {
|
|
2853
|
+
const unlink5 = signal === void 0 ? void 0 : linkAbortSignal(signal, timeout);
|
|
2854
|
+
const run = (async () => {
|
|
2971
2855
|
timeout.signal.throwIfAborted();
|
|
2972
2856
|
const call = await llm.prepareCall(
|
|
2973
2857
|
{ provider: route.provider, model: route.model, maxTokens: CLASSIFIER_MAX_TOKENS },
|
|
@@ -2996,9 +2880,21 @@ function createClassifier(llm, route) {
|
|
|
2996
2880
|
throw new Error("Classifier finished without stop");
|
|
2997
2881
|
}
|
|
2998
2882
|
throw new Error(`Invalid classifier output: ${raw}`);
|
|
2883
|
+
})();
|
|
2884
|
+
let timer;
|
|
2885
|
+
try {
|
|
2886
|
+
return await Promise.race([
|
|
2887
|
+
run,
|
|
2888
|
+
new Promise((_resolve, reject) => {
|
|
2889
|
+
timer = setTimeout(() => {
|
|
2890
|
+
timeout.abort();
|
|
2891
|
+
reject(new Error("Classifier timed out"));
|
|
2892
|
+
}, CLASSIFIER_TIMEOUT_MS);
|
|
2893
|
+
})
|
|
2894
|
+
]);
|
|
2999
2895
|
} finally {
|
|
3000
|
-
clearTimeout(timer);
|
|
3001
|
-
|
|
2896
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
2897
|
+
unlink5?.();
|
|
3002
2898
|
}
|
|
3003
2899
|
});
|
|
3004
2900
|
}
|
|
@@ -5003,45 +4899,45 @@ function mergeUsageCalls(stored, history) {
|
|
|
5003
4899
|
}
|
|
5004
4900
|
|
|
5005
4901
|
// src/host/usage/ledger.ts
|
|
5006
|
-
import { randomUUID as
|
|
5007
|
-
import { chmod as
|
|
5008
|
-
import { basename as
|
|
5009
|
-
import { z as
|
|
5010
|
-
var callSchema =
|
|
5011
|
-
at:
|
|
5012
|
-
source:
|
|
5013
|
-
provider:
|
|
5014
|
-
model:
|
|
4902
|
+
import { randomUUID as randomUUID6 } from "node:crypto";
|
|
4903
|
+
import { chmod as chmod4, mkdir as mkdir4, readFile as readFile4, rename as rename4, unlink as unlink4, writeFile as writeFile4 } from "node:fs/promises";
|
|
4904
|
+
import { basename as basename4, join as join6 } from "node:path";
|
|
4905
|
+
import { z as z3 } from "zod";
|
|
4906
|
+
var callSchema = z3.object({
|
|
4907
|
+
at: z3.number().int().nonnegative(),
|
|
4908
|
+
source: z3.enum(USAGE_SOURCES),
|
|
4909
|
+
provider: z3.string().min(1),
|
|
4910
|
+
model: z3.string().min(1),
|
|
5015
4911
|
// Older ledgers omit effort; treat as unknown.
|
|
5016
|
-
reasoningEffort:
|
|
5017
|
-
inputTokens:
|
|
5018
|
-
outputTokens:
|
|
5019
|
-
cacheReadTokens:
|
|
5020
|
-
cacheWriteTokens:
|
|
5021
|
-
reasoningTokens:
|
|
4912
|
+
reasoningEffort: z3.string().optional().default(""),
|
|
4913
|
+
inputTokens: z3.number().int().nonnegative(),
|
|
4914
|
+
outputTokens: z3.number().int().nonnegative(),
|
|
4915
|
+
cacheReadTokens: z3.number().int().nonnegative(),
|
|
4916
|
+
cacheWriteTokens: z3.number().int().nonnegative(),
|
|
4917
|
+
reasoningTokens: z3.number().int().nonnegative()
|
|
5022
4918
|
}).strict();
|
|
5023
|
-
var
|
|
5024
|
-
schemaVersion:
|
|
5025
|
-
calls:
|
|
4919
|
+
var fileSchema3 = z3.object({
|
|
4920
|
+
schemaVersion: z3.literal(1),
|
|
4921
|
+
calls: z3.array(z3.unknown())
|
|
5026
4922
|
}).strict();
|
|
5027
|
-
function
|
|
4923
|
+
function isMissing4(error) {
|
|
5028
4924
|
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
5029
4925
|
}
|
|
5030
|
-
async function
|
|
4926
|
+
async function removeIfPresent4(path) {
|
|
5031
4927
|
try {
|
|
5032
|
-
await
|
|
4928
|
+
await unlink4(path);
|
|
5033
4929
|
} catch (error) {
|
|
5034
|
-
if (!
|
|
4930
|
+
if (!isMissing4(error)) throw error;
|
|
5035
4931
|
}
|
|
5036
4932
|
}
|
|
5037
|
-
async function
|
|
5038
|
-
const temporary =
|
|
4933
|
+
async function writeAtomic4(path, value) {
|
|
4934
|
+
const temporary = join6(join6(path, ".."), `.${basename4(path)}.${randomUUID6()}.tmp`);
|
|
5039
4935
|
try {
|
|
5040
|
-
await
|
|
5041
|
-
await
|
|
5042
|
-
await
|
|
4936
|
+
await writeFile4(temporary, JSON.stringify(value), { encoding: "utf8", mode: 384, flag: "wx" });
|
|
4937
|
+
await rename4(temporary, path);
|
|
4938
|
+
await chmod4(path, 384);
|
|
5043
4939
|
} finally {
|
|
5044
|
-
await
|
|
4940
|
+
await removeIfPresent4(temporary);
|
|
5045
4941
|
}
|
|
5046
4942
|
}
|
|
5047
4943
|
var UsageLedger = class {
|
|
@@ -5049,7 +4945,7 @@ var UsageLedger = class {
|
|
|
5049
4945
|
}) {
|
|
5050
4946
|
this.root = root;
|
|
5051
4947
|
this.warn = warn;
|
|
5052
|
-
this.file =
|
|
4948
|
+
this.file = join6(root, "ledger.json");
|
|
5053
4949
|
}
|
|
5054
4950
|
root;
|
|
5055
4951
|
warn;
|
|
@@ -5090,9 +4986,9 @@ var UsageLedger = class {
|
|
|
5090
4986
|
async read() {
|
|
5091
4987
|
let text3;
|
|
5092
4988
|
try {
|
|
5093
|
-
text3 = await
|
|
4989
|
+
text3 = await readFile4(this.file, "utf8");
|
|
5094
4990
|
} catch (error) {
|
|
5095
|
-
if (
|
|
4991
|
+
if (isMissing4(error)) return [];
|
|
5096
4992
|
throw error;
|
|
5097
4993
|
}
|
|
5098
4994
|
let parsed;
|
|
@@ -5101,7 +4997,7 @@ var UsageLedger = class {
|
|
|
5101
4997
|
} catch {
|
|
5102
4998
|
return [];
|
|
5103
4999
|
}
|
|
5104
|
-
const file =
|
|
5000
|
+
const file = fileSchema3.safeParse(parsed);
|
|
5105
5001
|
if (!file.success) return [];
|
|
5106
5002
|
const calls = [];
|
|
5107
5003
|
for (const item of file.data.calls) {
|
|
@@ -5116,8 +5012,8 @@ var UsageLedger = class {
|
|
|
5116
5012
|
await this.persist();
|
|
5117
5013
|
}
|
|
5118
5014
|
async persist() {
|
|
5119
|
-
await
|
|
5120
|
-
await
|
|
5015
|
+
await mkdir4(this.root, { recursive: true, mode: 448 });
|
|
5016
|
+
await writeAtomic4(this.file, { schemaVersion: 1, calls: this.calls });
|
|
5121
5017
|
}
|
|
5122
5018
|
};
|
|
5123
5019
|
|
|
@@ -5159,53 +5055,53 @@ function installUsageObserver(ctx, recorder) {
|
|
|
5159
5055
|
|
|
5160
5056
|
// src/host/rpc.ts
|
|
5161
5057
|
import { createTypertRemoteInfo } from "@deepseek-ai/dsh-typert-protocol/remote-info";
|
|
5162
|
-
import { z as
|
|
5058
|
+
import { z as z4 } from "zod";
|
|
5163
5059
|
|
|
5164
5060
|
// src/host/update/types.ts
|
|
5165
5061
|
var APPLY_UPDATE_CODES = ["restart", "link", "manual", "none", "failed"];
|
|
5166
5062
|
|
|
5167
5063
|
// src/host/rpc.ts
|
|
5168
|
-
var
|
|
5169
|
-
var recentExchangeSchema =
|
|
5170
|
-
turn:
|
|
5171
|
-
prompt:
|
|
5172
|
-
response:
|
|
5064
|
+
var sessionIdSchema3 = z4.string().min(1).transform((value) => value);
|
|
5065
|
+
var recentExchangeSchema = z4.strictObject({
|
|
5066
|
+
turn: z4.number().int().nonnegative(),
|
|
5067
|
+
prompt: z4.string(),
|
|
5068
|
+
response: z4.string()
|
|
5173
5069
|
});
|
|
5174
|
-
var optimizeReferenceSchema =
|
|
5175
|
-
offset:
|
|
5176
|
-
length:
|
|
5177
|
-
text:
|
|
5070
|
+
var optimizeReferenceSchema = z4.strictObject({
|
|
5071
|
+
offset: z4.number().int().nonnegative(),
|
|
5072
|
+
length: z4.number().int().positive(),
|
|
5073
|
+
text: z4.string().min(1)
|
|
5178
5074
|
});
|
|
5179
|
-
var uiLocaleSchema =
|
|
5180
|
-
var optimizeRequestSchema =
|
|
5181
|
-
operation:
|
|
5182
|
-
requestId:
|
|
5183
|
-
sessionId:
|
|
5184
|
-
source:
|
|
5185
|
-
references:
|
|
5186
|
-
context:
|
|
5075
|
+
var uiLocaleSchema = z4.enum(["zh", "en"]).optional();
|
|
5076
|
+
var optimizeRequestSchema = z4.strictObject({
|
|
5077
|
+
operation: z4.literal("optimize"),
|
|
5078
|
+
requestId: z4.string(),
|
|
5079
|
+
sessionId: sessionIdSchema3,
|
|
5080
|
+
source: z4.string(),
|
|
5081
|
+
references: z4.array(optimizeReferenceSchema),
|
|
5082
|
+
context: z4.array(recentExchangeSchema),
|
|
5187
5083
|
uiLocale: uiLocaleSchema
|
|
5188
5084
|
});
|
|
5189
|
-
var predictRequestSchema =
|
|
5190
|
-
operation:
|
|
5191
|
-
requestId:
|
|
5192
|
-
sessionId:
|
|
5193
|
-
context:
|
|
5085
|
+
var predictRequestSchema = z4.strictObject({
|
|
5086
|
+
operation: z4.literal("predict"),
|
|
5087
|
+
requestId: z4.string(),
|
|
5088
|
+
sessionId: sessionIdSchema3,
|
|
5089
|
+
context: z4.array(recentExchangeSchema),
|
|
5194
5090
|
uiLocale: uiLocaleSchema
|
|
5195
5091
|
});
|
|
5196
|
-
var auxiliaryRequestSchema =
|
|
5092
|
+
var auxiliaryRequestSchema = z4.discriminatedUnion("operation", [
|
|
5197
5093
|
optimizeRequestSchema,
|
|
5198
5094
|
predictRequestSchema
|
|
5199
5095
|
]);
|
|
5200
|
-
var rpcPayloadSchema =
|
|
5201
|
-
method:
|
|
5096
|
+
var rpcPayloadSchema = z4.strictObject({
|
|
5097
|
+
method: z4.literal("generate"),
|
|
5202
5098
|
data: auxiliaryRequestSchema
|
|
5203
5099
|
});
|
|
5204
|
-
var auxiliaryResultSchema =
|
|
5205
|
-
requestId:
|
|
5206
|
-
text:
|
|
5207
|
-
route:
|
|
5208
|
-
segments:
|
|
5100
|
+
var auxiliaryResultSchema = z4.strictObject({
|
|
5101
|
+
requestId: z4.string(),
|
|
5102
|
+
text: z4.string(),
|
|
5103
|
+
route: z4.strictObject({ provider: z4.string(), model: z4.string() }),
|
|
5104
|
+
segments: z4.array(z4.string()).optional()
|
|
5209
5105
|
});
|
|
5210
5106
|
function codec(typeSymbol, schema) {
|
|
5211
5107
|
return { mode: "strict", typeSymbol, create: () => schema };
|
|
@@ -5213,50 +5109,50 @@ function codec(typeSymbol, schema) {
|
|
|
5213
5109
|
var payloadCodec = codec("dsh-agent-enhancer#RpcPayload", rpcPayloadSchema);
|
|
5214
5110
|
var resultCodec = codec(
|
|
5215
5111
|
"dsh-agent-enhancer#AuxiliaryResult|null",
|
|
5216
|
-
|
|
5112
|
+
z4.union([auxiliaryResultSchema, z4.null()])
|
|
5217
5113
|
);
|
|
5218
|
-
var modelReasoningEffortSchema =
|
|
5219
|
-
id:
|
|
5220
|
-
name:
|
|
5221
|
-
description:
|
|
5114
|
+
var modelReasoningEffortSchema = z4.object({
|
|
5115
|
+
id: z4.string(),
|
|
5116
|
+
name: z4.string(),
|
|
5117
|
+
description: z4.string().optional()
|
|
5222
5118
|
});
|
|
5223
|
-
var modelReasoningSchema =
|
|
5224
|
-
efforts:
|
|
5225
|
-
defaultEffort:
|
|
5119
|
+
var modelReasoningSchema = z4.object({
|
|
5120
|
+
efforts: z4.array(modelReasoningEffortSchema),
|
|
5121
|
+
defaultEffort: z4.string().optional()
|
|
5226
5122
|
});
|
|
5227
|
-
var modelCatalogModelSchema =
|
|
5228
|
-
id:
|
|
5229
|
-
name:
|
|
5230
|
-
description:
|
|
5123
|
+
var modelCatalogModelSchema = z4.object({
|
|
5124
|
+
id: z4.string(),
|
|
5125
|
+
name: z4.string(),
|
|
5126
|
+
description: z4.string().optional(),
|
|
5231
5127
|
reasoning: modelReasoningSchema.optional()
|
|
5232
5128
|
});
|
|
5233
|
-
var modelProviderGroupSchema =
|
|
5234
|
-
id:
|
|
5235
|
-
name:
|
|
5236
|
-
models:
|
|
5129
|
+
var modelProviderGroupSchema = z4.object({
|
|
5130
|
+
id: z4.string(),
|
|
5131
|
+
name: z4.string(),
|
|
5132
|
+
models: z4.array(modelCatalogModelSchema)
|
|
5237
5133
|
});
|
|
5238
|
-
var modelCatalogFailureSchema =
|
|
5239
|
-
id:
|
|
5240
|
-
name:
|
|
5241
|
-
message:
|
|
5134
|
+
var modelCatalogFailureSchema = z4.object({
|
|
5135
|
+
id: z4.string(),
|
|
5136
|
+
name: z4.string(),
|
|
5137
|
+
message: z4.string()
|
|
5242
5138
|
});
|
|
5243
|
-
var modelCatalogSchema =
|
|
5244
|
-
default:
|
|
5245
|
-
provider:
|
|
5246
|
-
model:
|
|
5247
|
-
reasoningEffort:
|
|
5139
|
+
var modelCatalogSchema = z4.object({
|
|
5140
|
+
default: z4.object({
|
|
5141
|
+
provider: z4.string(),
|
|
5142
|
+
model: z4.string(),
|
|
5143
|
+
reasoningEffort: z4.string().optional()
|
|
5248
5144
|
}),
|
|
5249
|
-
routableProviders:
|
|
5250
|
-
groups:
|
|
5251
|
-
failures:
|
|
5145
|
+
routableProviders: z4.array(z4.string()),
|
|
5146
|
+
groups: z4.array(modelProviderGroupSchema),
|
|
5147
|
+
failures: z4.array(modelCatalogFailureSchema)
|
|
5252
5148
|
});
|
|
5253
5149
|
var catalogCodec = codec("dsh-agent-enhancer#ModelCatalog", modelCatalogSchema);
|
|
5254
|
-
var contextInspectCategorySchema =
|
|
5255
|
-
var contextInspectRequestSchema =
|
|
5256
|
-
sessionId:
|
|
5150
|
+
var contextInspectCategorySchema = z4.enum(["system", "tools", "messages"]);
|
|
5151
|
+
var contextInspectRequestSchema = z4.strictObject({
|
|
5152
|
+
sessionId: sessionIdSchema3,
|
|
5257
5153
|
category: contextInspectCategorySchema
|
|
5258
5154
|
});
|
|
5259
|
-
var contextInspectKindSchema =
|
|
5155
|
+
var contextInspectKindSchema = z4.enum([
|
|
5260
5156
|
"system",
|
|
5261
5157
|
"tools",
|
|
5262
5158
|
"user",
|
|
@@ -5265,144 +5161,144 @@ var contextInspectKindSchema = z3.enum([
|
|
|
5265
5161
|
"tool",
|
|
5266
5162
|
"developer"
|
|
5267
5163
|
]);
|
|
5268
|
-
var contextInspectItemSchema =
|
|
5269
|
-
id:
|
|
5270
|
-
title:
|
|
5271
|
-
detail:
|
|
5272
|
-
body:
|
|
5273
|
-
tokens:
|
|
5164
|
+
var contextInspectItemSchema = z4.strictObject({
|
|
5165
|
+
id: z4.string(),
|
|
5166
|
+
title: z4.string(),
|
|
5167
|
+
detail: z4.string(),
|
|
5168
|
+
body: z4.string(),
|
|
5169
|
+
tokens: z4.number().int().nonnegative(),
|
|
5274
5170
|
kind: contextInspectKindSchema,
|
|
5275
|
-
replaced:
|
|
5171
|
+
replaced: z4.boolean()
|
|
5276
5172
|
});
|
|
5277
|
-
var contextInspectPressureSchema =
|
|
5278
|
-
projectedTokens:
|
|
5279
|
-
contextWindow:
|
|
5173
|
+
var contextInspectPressureSchema = z4.strictObject({
|
|
5174
|
+
projectedTokens: z4.number().int().nonnegative().optional(),
|
|
5175
|
+
contextWindow: z4.number().int().positive().optional()
|
|
5280
5176
|
});
|
|
5281
|
-
var contextInspectEventKindSchema =
|
|
5282
|
-
var contextInspectEventMarkSchema =
|
|
5283
|
-
id:
|
|
5177
|
+
var contextInspectEventKindSchema = z4.enum(["compact", "prune", "replace"]);
|
|
5178
|
+
var contextInspectEventMarkSchema = z4.strictObject({
|
|
5179
|
+
id: z4.string(),
|
|
5284
5180
|
kind: contextInspectEventKindSchema,
|
|
5285
|
-
seq:
|
|
5286
|
-
shadowed:
|
|
5287
|
-
title:
|
|
5181
|
+
seq: z4.number().int().nonnegative(),
|
|
5182
|
+
shadowed: z4.number().int().nonnegative(),
|
|
5183
|
+
title: z4.string()
|
|
5288
5184
|
});
|
|
5289
|
-
var contextInspectResultSchema =
|
|
5290
|
-
sessionId:
|
|
5185
|
+
var contextInspectResultSchema = z4.strictObject({
|
|
5186
|
+
sessionId: sessionIdSchema3,
|
|
5291
5187
|
category: contextInspectCategorySchema,
|
|
5292
|
-
totalTokens:
|
|
5293
|
-
approximate:
|
|
5294
|
-
items:
|
|
5188
|
+
totalTokens: z4.number().int().nonnegative(),
|
|
5189
|
+
approximate: z4.literal(true),
|
|
5190
|
+
items: z4.array(contextInspectItemSchema),
|
|
5295
5191
|
pressure: contextInspectPressureSchema,
|
|
5296
|
-
events:
|
|
5192
|
+
events: z4.array(contextInspectEventMarkSchema)
|
|
5297
5193
|
});
|
|
5298
5194
|
var contextInspectRequestCodec = codec("dsh-agent-enhancer#ContextInspectRequest", contextInspectRequestSchema);
|
|
5299
5195
|
var contextInspectResultCodec = codec("dsh-agent-enhancer#ContextInspectResult", contextInspectResultSchema);
|
|
5300
|
-
var contextCompactRequestSchema =
|
|
5301
|
-
sessionId:
|
|
5196
|
+
var contextCompactRequestSchema = z4.strictObject({
|
|
5197
|
+
sessionId: sessionIdSchema3
|
|
5302
5198
|
});
|
|
5303
|
-
var contextCompactResultSchema =
|
|
5304
|
-
sessionId:
|
|
5305
|
-
status:
|
|
5306
|
-
shadowedCount:
|
|
5307
|
-
shadowedTokens:
|
|
5199
|
+
var contextCompactResultSchema = z4.strictObject({
|
|
5200
|
+
sessionId: sessionIdSchema3,
|
|
5201
|
+
status: z4.enum(["compacted", "noop"]),
|
|
5202
|
+
shadowedCount: z4.number().int().nonnegative(),
|
|
5203
|
+
shadowedTokens: z4.number().int().nonnegative()
|
|
5308
5204
|
});
|
|
5309
5205
|
var contextCompactRequestCodec = codec("dsh-agent-enhancer#ContextCompactRequest", contextCompactRequestSchema);
|
|
5310
5206
|
var contextCompactResultCodec = codec("dsh-agent-enhancer#ContextCompactResult", contextCompactResultSchema);
|
|
5311
|
-
var updateCheckResultSchema =
|
|
5312
|
-
current:
|
|
5313
|
-
latest:
|
|
5314
|
-
updateAvailable:
|
|
5315
|
-
installKind:
|
|
5316
|
-
canAutoApply:
|
|
5317
|
-
command:
|
|
5207
|
+
var updateCheckResultSchema = z4.strictObject({
|
|
5208
|
+
current: z4.string(),
|
|
5209
|
+
latest: z4.string().nullable(),
|
|
5210
|
+
updateAvailable: z4.boolean(),
|
|
5211
|
+
installKind: z4.enum(["npm", "link", "unknown"]),
|
|
5212
|
+
canAutoApply: z4.boolean(),
|
|
5213
|
+
command: z4.string()
|
|
5318
5214
|
});
|
|
5319
|
-
var applyUpdateResultSchema =
|
|
5320
|
-
ok:
|
|
5321
|
-
message:
|
|
5322
|
-
command:
|
|
5215
|
+
var applyUpdateResultSchema = z4.strictObject({
|
|
5216
|
+
ok: z4.boolean(),
|
|
5217
|
+
message: z4.enum(APPLY_UPDATE_CODES),
|
|
5218
|
+
command: z4.string().optional()
|
|
5323
5219
|
});
|
|
5324
5220
|
var updateCheckResultCodec = codec("dsh-agent-enhancer#UpdateCheckResult", updateCheckResultSchema);
|
|
5325
5221
|
var applyUpdateResultCodec = codec("dsh-agent-enhancer#ApplyUpdateResult", applyUpdateResultSchema);
|
|
5326
5222
|
var usageTokenFields = {
|
|
5327
|
-
inputTokens:
|
|
5328
|
-
outputTokens:
|
|
5329
|
-
cacheReadTokens:
|
|
5330
|
-
cacheWriteTokens:
|
|
5331
|
-
reasoningTokens:
|
|
5223
|
+
inputTokens: z4.number().int().nonnegative(),
|
|
5224
|
+
outputTokens: z4.number().int().nonnegative(),
|
|
5225
|
+
cacheReadTokens: z4.number().int().nonnegative(),
|
|
5226
|
+
cacheWriteTokens: z4.number().int().nonnegative(),
|
|
5227
|
+
reasoningTokens: z4.number().int().nonnegative()
|
|
5332
5228
|
};
|
|
5333
|
-
var usageRangeSchema =
|
|
5334
|
-
var usageCallSchema =
|
|
5335
|
-
at:
|
|
5336
|
-
source:
|
|
5337
|
-
provider:
|
|
5338
|
-
model:
|
|
5339
|
-
reasoningEffort:
|
|
5229
|
+
var usageRangeSchema = z4.enum(USAGE_RANGES);
|
|
5230
|
+
var usageCallSchema = z4.object({
|
|
5231
|
+
at: z4.number().int().nonnegative(),
|
|
5232
|
+
source: z4.enum(USAGE_SOURCES),
|
|
5233
|
+
provider: z4.string(),
|
|
5234
|
+
model: z4.string(),
|
|
5235
|
+
reasoningEffort: z4.string(),
|
|
5340
5236
|
...usageTokenFields
|
|
5341
5237
|
}).strict();
|
|
5342
|
-
var usageModelLineSchema =
|
|
5343
|
-
provider:
|
|
5344
|
-
model:
|
|
5345
|
-
reasoningEffort:
|
|
5346
|
-
calls:
|
|
5238
|
+
var usageModelLineSchema = z4.object({
|
|
5239
|
+
provider: z4.string(),
|
|
5240
|
+
model: z4.string(),
|
|
5241
|
+
reasoningEffort: z4.string(),
|
|
5242
|
+
calls: z4.number().int().nonnegative(),
|
|
5347
5243
|
...usageTokenFields
|
|
5348
5244
|
}).strict();
|
|
5349
|
-
var usageSourceLineSchema =
|
|
5350
|
-
source:
|
|
5351
|
-
calls:
|
|
5352
|
-
models:
|
|
5245
|
+
var usageSourceLineSchema = z4.object({
|
|
5246
|
+
source: z4.enum(USAGE_SOURCES),
|
|
5247
|
+
calls: z4.number().int().nonnegative(),
|
|
5248
|
+
models: z4.array(usageModelLineSchema),
|
|
5353
5249
|
...usageTokenFields
|
|
5354
5250
|
}).strict();
|
|
5355
|
-
var usageHeatCellSchema =
|
|
5356
|
-
key:
|
|
5357
|
-
label:
|
|
5358
|
-
at:
|
|
5359
|
-
tokens:
|
|
5360
|
-
calls:
|
|
5251
|
+
var usageHeatCellSchema = z4.object({
|
|
5252
|
+
key: z4.string(),
|
|
5253
|
+
label: z4.string(),
|
|
5254
|
+
at: z4.number().int().nonnegative(),
|
|
5255
|
+
tokens: z4.number().int().nonnegative(),
|
|
5256
|
+
calls: z4.number().int().nonnegative()
|
|
5361
5257
|
}).strict();
|
|
5362
|
-
var usageHeatmapSchema =
|
|
5363
|
-
grain:
|
|
5364
|
-
cells:
|
|
5258
|
+
var usageHeatmapSchema = z4.object({
|
|
5259
|
+
grain: z4.enum(["hour", "day"]),
|
|
5260
|
+
cells: z4.array(usageHeatCellSchema)
|
|
5365
5261
|
}).strict();
|
|
5366
|
-
var usageMoneySchema =
|
|
5367
|
-
currency:
|
|
5368
|
-
estimatedCost:
|
|
5369
|
-
pricedCalls:
|
|
5370
|
-
unpricedCalls:
|
|
5262
|
+
var usageMoneySchema = z4.object({
|
|
5263
|
+
currency: z4.literal("CNY"),
|
|
5264
|
+
estimatedCost: z4.number().nonnegative(),
|
|
5265
|
+
pricedCalls: z4.number().int().nonnegative(),
|
|
5266
|
+
unpricedCalls: z4.number().int().nonnegative()
|
|
5371
5267
|
}).strict();
|
|
5372
|
-
var usageBalanceSchema =
|
|
5373
|
-
available:
|
|
5374
|
-
currency:
|
|
5375
|
-
total:
|
|
5376
|
-
granted:
|
|
5377
|
-
toppedUp:
|
|
5378
|
-
checkedAt:
|
|
5379
|
-
stale:
|
|
5268
|
+
var usageBalanceSchema = z4.object({
|
|
5269
|
+
available: z4.boolean(),
|
|
5270
|
+
currency: z4.string(),
|
|
5271
|
+
total: z4.string(),
|
|
5272
|
+
granted: z4.string(),
|
|
5273
|
+
toppedUp: z4.string(),
|
|
5274
|
+
checkedAt: z4.number().int().nonnegative(),
|
|
5275
|
+
stale: z4.boolean().optional()
|
|
5380
5276
|
}).strict();
|
|
5381
|
-
var usageBalanceStatusSchema =
|
|
5382
|
-
var usageSummarySchema =
|
|
5277
|
+
var usageBalanceStatusSchema = z4.enum(["ready", "no-key", "unavailable"]);
|
|
5278
|
+
var usageSummarySchema = z4.object({
|
|
5383
5279
|
range: usageRangeSchema,
|
|
5384
|
-
calls:
|
|
5385
|
-
sources:
|
|
5280
|
+
calls: z4.number().int().nonnegative(),
|
|
5281
|
+
sources: z4.array(usageSourceLineSchema),
|
|
5386
5282
|
heatmap: usageHeatmapSchema,
|
|
5387
5283
|
money: usageMoneySchema,
|
|
5388
5284
|
balanceStatus: usageBalanceStatusSchema,
|
|
5389
5285
|
balance: usageBalanceSchema.optional(),
|
|
5390
|
-
recent:
|
|
5286
|
+
recent: z4.array(usageCallSchema),
|
|
5391
5287
|
...usageTokenFields
|
|
5392
5288
|
}).strict();
|
|
5393
|
-
var providerBalanceStatusSchema =
|
|
5394
|
-
var providerBalanceSnapshotSchema =
|
|
5395
|
-
source:
|
|
5289
|
+
var providerBalanceStatusSchema = z4.enum(["ready", "no-key", "signed-out", "unavailable"]);
|
|
5290
|
+
var providerBalanceSnapshotSchema = z4.object({
|
|
5291
|
+
source: z4.enum(["account", "api-key"]),
|
|
5396
5292
|
status: providerBalanceStatusSchema,
|
|
5397
5293
|
balance: usageBalanceSchema.optional()
|
|
5398
5294
|
}).strict();
|
|
5399
|
-
var providerBalancesSchema =
|
|
5295
|
+
var providerBalancesSchema = z4.object({
|
|
5400
5296
|
account: providerBalanceSnapshotSchema,
|
|
5401
5297
|
apiKey: providerBalanceSnapshotSchema
|
|
5402
5298
|
}).strict();
|
|
5403
|
-
var providerBalancesRequestSchema =
|
|
5404
|
-
force:
|
|
5405
|
-
locale:
|
|
5299
|
+
var providerBalancesRequestSchema = z4.object({
|
|
5300
|
+
force: z4.boolean().optional(),
|
|
5301
|
+
locale: z4.string().optional()
|
|
5406
5302
|
}).strict();
|
|
5407
5303
|
var usageRangeCodec = codec("dsh-agent-enhancer#UsageRange", usageRangeSchema);
|
|
5408
5304
|
var usageSummaryCodec = codec("dsh-agent-enhancer#UsageSummary", usageSummarySchema);
|
|
@@ -5500,26 +5396,28 @@ var remoteInfo = createTypertRemoteInfo({
|
|
|
5500
5396
|
result: providerBalancesCodec
|
|
5501
5397
|
}
|
|
5502
5398
|
}
|
|
5503
|
-
},
|
|
5504
|
-
var checkpointScopeSchema =
|
|
5505
|
-
var checkpointRollbackRequestSchema =
|
|
5506
|
-
sessionId:
|
|
5507
|
-
turn:
|
|
5508
|
-
operation:
|
|
5509
|
-
scope: checkpointScopeSchema.optional()
|
|
5399
|
+
}, z4);
|
|
5400
|
+
var checkpointScopeSchema = z4.enum(["conversation", "conversation-and-files"]);
|
|
5401
|
+
var checkpointRollbackRequestSchema = z4.strictObject({
|
|
5402
|
+
sessionId: sessionIdSchema3,
|
|
5403
|
+
turn: z4.number().int().positive(),
|
|
5404
|
+
operation: z4.enum(["rollback", "undo"]).optional(),
|
|
5405
|
+
scope: checkpointScopeSchema.optional(),
|
|
5406
|
+
// Timeline cards pin the tip by atSeq (turn numbers reuse after undo).
|
|
5407
|
+
atSeq: z4.number().int().nonnegative().optional()
|
|
5510
5408
|
});
|
|
5511
|
-
var checkpointMessageSchema =
|
|
5512
|
-
seq:
|
|
5513
|
-
type:
|
|
5514
|
-
text:
|
|
5409
|
+
var checkpointMessageSchema = z4.strictObject({
|
|
5410
|
+
seq: z4.number().int().nonnegative(),
|
|
5411
|
+
type: z4.string(),
|
|
5412
|
+
text: z4.string()
|
|
5515
5413
|
});
|
|
5516
|
-
var checkpointFileSchema =
|
|
5517
|
-
path:
|
|
5518
|
-
change:
|
|
5414
|
+
var checkpointFileSchema = z4.strictObject({
|
|
5415
|
+
path: z4.string(),
|
|
5416
|
+
change: z4.enum(["create", "modify", "delete"])
|
|
5519
5417
|
});
|
|
5520
|
-
var checkpointConflictSchema =
|
|
5521
|
-
path:
|
|
5522
|
-
reason:
|
|
5418
|
+
var checkpointConflictSchema = z4.strictObject({
|
|
5419
|
+
path: z4.string(),
|
|
5420
|
+
reason: z4.enum([
|
|
5523
5421
|
"unsafe-path",
|
|
5524
5422
|
"symlink",
|
|
5525
5423
|
"not-regular-file",
|
|
@@ -5529,10 +5427,10 @@ var checkpointConflictSchema = z3.strictObject({
|
|
|
5529
5427
|
"invalid-after-image",
|
|
5530
5428
|
"hash-mismatch"
|
|
5531
5429
|
]),
|
|
5532
|
-
expectedHash:
|
|
5533
|
-
actualHash:
|
|
5430
|
+
expectedHash: z4.string().optional(),
|
|
5431
|
+
actualHash: z4.string().nullable().optional()
|
|
5534
5432
|
});
|
|
5535
|
-
var checkpointUnavailableSchema =
|
|
5433
|
+
var checkpointUnavailableSchema = z4.enum([
|
|
5536
5434
|
"checkpoint-not-found",
|
|
5537
5435
|
"incomplete-checkpoint",
|
|
5538
5436
|
"prompt-non-text-content",
|
|
@@ -5548,52 +5446,67 @@ var checkpointUnavailableSchema = z3.enum([
|
|
|
5548
5446
|
"already-restored",
|
|
5549
5447
|
"covered-by-earlier-restore"
|
|
5550
5448
|
]);
|
|
5551
|
-
var checkpointPreviewSchema =
|
|
5552
|
-
sessionId:
|
|
5553
|
-
turn:
|
|
5554
|
-
operation:
|
|
5449
|
+
var checkpointPreviewSchema = z4.strictObject({
|
|
5450
|
+
sessionId: sessionIdSchema3,
|
|
5451
|
+
turn: z4.number().int().positive(),
|
|
5452
|
+
operation: z4.enum(["rollback", "undo"]).optional(),
|
|
5555
5453
|
scope: checkpointScopeSchema.optional(),
|
|
5556
|
-
tipTurn:
|
|
5557
|
-
|
|
5558
|
-
|
|
5559
|
-
|
|
5560
|
-
|
|
5454
|
+
tipTurn: z4.number().int().positive().optional(),
|
|
5455
|
+
atSeq: z4.number().int().nonnegative().optional(),
|
|
5456
|
+
messages: z4.array(checkpointMessageSchema),
|
|
5457
|
+
files: z4.array(checkpointFileSchema),
|
|
5458
|
+
conflicts: z4.array(checkpointConflictSchema),
|
|
5459
|
+
unavailable: z4.array(checkpointUnavailableSchema)
|
|
5561
5460
|
});
|
|
5562
|
-
var checkpointResultSchema =
|
|
5563
|
-
sessionId:
|
|
5564
|
-
turn:
|
|
5565
|
-
draft:
|
|
5461
|
+
var checkpointResultSchema = z4.strictObject({
|
|
5462
|
+
sessionId: sessionIdSchema3,
|
|
5463
|
+
turn: z4.number().int().positive(),
|
|
5464
|
+
draft: z4.string()
|
|
5566
5465
|
});
|
|
5567
|
-
var checkpointSummariesRequestSchema =
|
|
5568
|
-
sessionId:
|
|
5466
|
+
var checkpointSummariesRequestSchema = z4.strictObject({
|
|
5467
|
+
sessionId: sessionIdSchema3
|
|
5569
5468
|
});
|
|
5570
|
-
var checkpointGapSchema =
|
|
5571
|
-
|
|
5572
|
-
|
|
5469
|
+
var checkpointGapSchema = z4.discriminatedUnion("type", [
|
|
5470
|
+
z4.object({ type: z4.literal("prompt"), reason: z4.literal("non-text-content") }).strict(),
|
|
5471
|
+
z4.object({ type: z4.literal("workspace"), reason: z4.literal("capture-failed") }).strict()
|
|
5573
5472
|
]);
|
|
5574
|
-
var checkpointTurnSummarySchema =
|
|
5575
|
-
turn:
|
|
5576
|
-
coverage:
|
|
5577
|
-
fileCount:
|
|
5578
|
-
createdAt:
|
|
5579
|
-
atSeq:
|
|
5580
|
-
prompt:
|
|
5581
|
-
promptPreview:
|
|
5582
|
-
files:
|
|
5583
|
-
gaps:
|
|
5584
|
-
rolledBack:
|
|
5585
|
-
tip:
|
|
5473
|
+
var checkpointTurnSummarySchema = z4.strictObject({
|
|
5474
|
+
turn: z4.number().int().positive(),
|
|
5475
|
+
coverage: z4.enum(["complete", "partial"]),
|
|
5476
|
+
fileCount: z4.number().int().nonnegative(),
|
|
5477
|
+
createdAt: z4.number().int().nonnegative(),
|
|
5478
|
+
atSeq: z4.number().int().nonnegative(),
|
|
5479
|
+
prompt: z4.string(),
|
|
5480
|
+
promptPreview: z4.string(),
|
|
5481
|
+
files: z4.array(checkpointFileSchema),
|
|
5482
|
+
gaps: z4.array(checkpointGapSchema),
|
|
5483
|
+
rolledBack: z4.boolean(),
|
|
5484
|
+
tip: z4.boolean()
|
|
5586
5485
|
});
|
|
5587
|
-
var liveActivityStatsSchema =
|
|
5588
|
-
turns:
|
|
5589
|
-
steps:
|
|
5590
|
-
checkpoints:
|
|
5591
|
-
liveCheckpoints:
|
|
5486
|
+
var liveActivityStatsSchema = z4.strictObject({
|
|
5487
|
+
turns: z4.number().int().nonnegative(),
|
|
5488
|
+
steps: z4.number().int().nonnegative(),
|
|
5489
|
+
checkpoints: z4.number().int().nonnegative(),
|
|
5490
|
+
liveCheckpoints: z4.number().int().nonnegative()
|
|
5592
5491
|
});
|
|
5593
|
-
var checkpointSummariesResultSchema =
|
|
5594
|
-
sessionId:
|
|
5492
|
+
var checkpointSummariesResultSchema = z4.strictObject({
|
|
5493
|
+
sessionId: sessionIdSchema3,
|
|
5595
5494
|
activity: liveActivityStatsSchema,
|
|
5596
|
-
turns:
|
|
5495
|
+
turns: z4.array(checkpointTurnSummarySchema)
|
|
5496
|
+
});
|
|
5497
|
+
var checkpointCutRequestSchema = z4.strictObject({
|
|
5498
|
+
sessionId: sessionIdSchema3
|
|
5499
|
+
});
|
|
5500
|
+
var checkpointCutViewSchema = z4.strictObject({
|
|
5501
|
+
hideTurnsFrom: z4.number().int().positive(),
|
|
5502
|
+
cutSeq: z4.number().int().nonnegative(),
|
|
5503
|
+
restoredAtSeq: z4.number().int().nonnegative(),
|
|
5504
|
+
draftPrompt: z4.string(),
|
|
5505
|
+
scope: checkpointScopeSchema
|
|
5506
|
+
});
|
|
5507
|
+
var checkpointCutResultSchema = z4.strictObject({
|
|
5508
|
+
sessionId: sessionIdSchema3,
|
|
5509
|
+
cut: checkpointCutViewSchema.nullable()
|
|
5597
5510
|
});
|
|
5598
5511
|
var checkpointRequestCodec = codec("dsh-agent-enhancer#CheckpointRollbackRequest", checkpointRollbackRequestSchema);
|
|
5599
5512
|
var checkpointPreviewCodec = codec("dsh-agent-enhancer#CheckpointRollbackPreview", checkpointPreviewSchema);
|
|
@@ -5606,6 +5519,14 @@ var checkpointSummariesResultCodec = codec(
|
|
|
5606
5519
|
"dsh-agent-enhancer#CheckpointSummariesResult",
|
|
5607
5520
|
checkpointSummariesResultSchema
|
|
5608
5521
|
);
|
|
5522
|
+
var checkpointCutRequestCodec = codec(
|
|
5523
|
+
"dsh-agent-enhancer#CheckpointCutRequest",
|
|
5524
|
+
checkpointCutRequestSchema
|
|
5525
|
+
);
|
|
5526
|
+
var checkpointCutResultCodec = codec(
|
|
5527
|
+
"dsh-agent-enhancer#CheckpointCutResult",
|
|
5528
|
+
checkpointCutResultSchema
|
|
5529
|
+
);
|
|
5609
5530
|
var checkpointRemoteInfo = createTypertRemoteInfo({
|
|
5610
5531
|
package: "dsh-agent-enhancer",
|
|
5611
5532
|
methods: {
|
|
@@ -5637,13 +5558,26 @@ var checkpointRemoteInfo = createTypertRemoteInfo({
|
|
|
5637
5558
|
}],
|
|
5638
5559
|
cancellation: { parameter: "signal" },
|
|
5639
5560
|
result: checkpointSummariesResultCodec
|
|
5561
|
+
},
|
|
5562
|
+
"checkpointRollback/getCut": {
|
|
5563
|
+
service: "checkpointRollback",
|
|
5564
|
+
implementation: "getCut",
|
|
5565
|
+
invocation: { kind: "direct" },
|
|
5566
|
+
parameters: [{
|
|
5567
|
+
name: "request",
|
|
5568
|
+
source: "json",
|
|
5569
|
+
codec: checkpointCutRequestCodec,
|
|
5570
|
+
strictCodec: checkpointCutRequestCodec
|
|
5571
|
+
}],
|
|
5572
|
+
cancellation: { parameter: "signal" },
|
|
5573
|
+
result: checkpointCutResultCodec
|
|
5640
5574
|
}
|
|
5641
5575
|
}
|
|
5642
|
-
},
|
|
5576
|
+
}, z4);
|
|
5643
5577
|
|
|
5644
5578
|
// src/host/update/check.ts
|
|
5645
5579
|
import { homedir } from "node:os";
|
|
5646
|
-
import { join as
|
|
5580
|
+
import { join as join8 } from "node:path";
|
|
5647
5581
|
|
|
5648
5582
|
// src/host/update/cli.ts
|
|
5649
5583
|
import { spawn as nodeSpawn } from "node:child_process";
|
|
@@ -5714,9 +5648,9 @@ async function runPluginAdd(latest, spawnFn = defaultSpawn) {
|
|
|
5714
5648
|
|
|
5715
5649
|
// src/host/update/install-kind.ts
|
|
5716
5650
|
import { lstatSync } from "node:fs";
|
|
5717
|
-
import { join as
|
|
5651
|
+
import { join as join7 } from "node:path";
|
|
5718
5652
|
function detectInstallKind(profileDir) {
|
|
5719
|
-
const pkgPath =
|
|
5653
|
+
const pkgPath = join7(profileDir, "node_modules", PACKAGE_NAME);
|
|
5720
5654
|
try {
|
|
5721
5655
|
const stat = lstatSync(pkgPath);
|
|
5722
5656
|
if (stat.isSymbolicLink()) return "link";
|
|
@@ -5847,8 +5781,8 @@ function isNewerVersion(latest, current) {
|
|
|
5847
5781
|
|
|
5848
5782
|
// src/host/update/check.ts
|
|
5849
5783
|
function resolveDesktopProfileDir(env = process.env, home = homedir()) {
|
|
5850
|
-
const dshHome = env.DSH_HOME ??
|
|
5851
|
-
return
|
|
5784
|
+
const dshHome = env.DSH_HOME ?? join8(home, ".dsh");
|
|
5785
|
+
return join8(dshHome, "profiles", "desktop");
|
|
5852
5786
|
}
|
|
5853
5787
|
async function checkPluginUpdate(deps) {
|
|
5854
5788
|
const latest = await deps.fetchLatest();
|
|
@@ -5920,35 +5854,35 @@ var inject = [
|
|
|
5920
5854
|
// Official `/compact` runs through the command registry (not ctx.compaction).
|
|
5921
5855
|
"commands"
|
|
5922
5856
|
];
|
|
5923
|
-
var Config =
|
|
5924
|
-
historyEnabled:
|
|
5925
|
-
optimizeEnabled:
|
|
5926
|
-
predictEnabled:
|
|
5927
|
-
rollbackEnabled:
|
|
5928
|
-
contextInspectEnabled:
|
|
5857
|
+
var Config = z5.object({
|
|
5858
|
+
historyEnabled: z5.boolean().default(true).volatile(),
|
|
5859
|
+
optimizeEnabled: z5.boolean().default(true).volatile(),
|
|
5860
|
+
predictEnabled: z5.boolean().default(true).volatile(),
|
|
5861
|
+
rollbackEnabled: z5.boolean().default(true).volatile(),
|
|
5862
|
+
contextInspectEnabled: z5.boolean().default(true).volatile(),
|
|
5929
5863
|
// Empty provider/model = follow the current Desktop session model.
|
|
5930
|
-
optimizeProvider:
|
|
5931
|
-
optimizeModel:
|
|
5932
|
-
optimizeReasoningEffort:
|
|
5933
|
-
predictProvider:
|
|
5934
|
-
predictModel:
|
|
5935
|
-
predictReasoningEffort:
|
|
5936
|
-
compactProvider:
|
|
5937
|
-
compactModel:
|
|
5938
|
-
compactReasoningEffort:
|
|
5939
|
-
autoClassifierEnabled:
|
|
5940
|
-
autoInstantProvider:
|
|
5941
|
-
autoInstantModel:
|
|
5942
|
-
autoInstantReasoningEffort:
|
|
5943
|
-
autoThinkingProvider:
|
|
5944
|
-
autoThinkingModel:
|
|
5945
|
-
autoThinkingReasoningEffort:
|
|
5946
|
-
autoProProvider:
|
|
5947
|
-
autoProModel:
|
|
5948
|
-
autoProReasoningEffort:
|
|
5949
|
-
autoClassifierProvider:
|
|
5950
|
-
autoClassifierModel:
|
|
5951
|
-
autoClassifierReasoningEffort:
|
|
5864
|
+
optimizeProvider: z5.string().default("").volatile(),
|
|
5865
|
+
optimizeModel: z5.string().default("").volatile(),
|
|
5866
|
+
optimizeReasoningEffort: z5.string().default("").volatile(),
|
|
5867
|
+
predictProvider: z5.string().default("").volatile(),
|
|
5868
|
+
predictModel: z5.string().default("").volatile(),
|
|
5869
|
+
predictReasoningEffort: z5.string().default("").volatile(),
|
|
5870
|
+
compactProvider: z5.string().default("").volatile(),
|
|
5871
|
+
compactModel: z5.string().default("").volatile(),
|
|
5872
|
+
compactReasoningEffort: z5.string().default("").volatile(),
|
|
5873
|
+
autoClassifierEnabled: z5.boolean().default(false).volatile(),
|
|
5874
|
+
autoInstantProvider: z5.string().default("").volatile(),
|
|
5875
|
+
autoInstantModel: z5.string().default("").volatile(),
|
|
5876
|
+
autoInstantReasoningEffort: z5.string().default("").volatile(),
|
|
5877
|
+
autoThinkingProvider: z5.string().default("").volatile(),
|
|
5878
|
+
autoThinkingModel: z5.string().default("").volatile(),
|
|
5879
|
+
autoThinkingReasoningEffort: z5.string().default("").volatile(),
|
|
5880
|
+
autoProProvider: z5.string().default("").volatile(),
|
|
5881
|
+
autoProModel: z5.string().default("").volatile(),
|
|
5882
|
+
autoProReasoningEffort: z5.string().default("").volatile(),
|
|
5883
|
+
autoClassifierProvider: z5.string().default("").volatile(),
|
|
5884
|
+
autoClassifierModel: z5.string().default("").volatile(),
|
|
5885
|
+
autoClassifierReasoningEffort: z5.string().default("").volatile()
|
|
5952
5886
|
});
|
|
5953
5887
|
var hostContribution = {
|
|
5954
5888
|
package: PACKAGE_NAME,
|
|
@@ -6076,14 +6010,14 @@ var AgentEnhancerService = class extends TypertRemoteService {
|
|
|
6076
6010
|
}
|
|
6077
6011
|
};
|
|
6078
6012
|
function resolveCheckpointRoot(dshHome) {
|
|
6079
|
-
const base =
|
|
6080
|
-
const nextPkg =
|
|
6081
|
-
const next =
|
|
6082
|
-
const prev =
|
|
6013
|
+
const base = join9(dshHome, "profiles", "desktop", "plugin-data");
|
|
6014
|
+
const nextPkg = join9(base, PACKAGE_NAME);
|
|
6015
|
+
const next = join9(nextPkg, "checkpoint");
|
|
6016
|
+
const prev = join9(base, LEGACY_PACKAGE_NAME, "checkpoint");
|
|
6083
6017
|
if (!existsSync(next) && existsSync(prev)) {
|
|
6084
6018
|
mkdirSync(nextPkg, { recursive: true });
|
|
6085
6019
|
renameSync(prev, next);
|
|
6086
|
-
const prevPkg =
|
|
6020
|
+
const prevPkg = join9(base, LEGACY_PACKAGE_NAME);
|
|
6087
6021
|
try {
|
|
6088
6022
|
if (existsSync(prevPkg) && readdirSync(prevPkg).length === 0) rmdirSync(prevPkg);
|
|
6089
6023
|
} catch {
|
|
@@ -6149,8 +6083,8 @@ function apply(ctx, config) {
|
|
|
6149
6083
|
const settings = () => readSettings(config);
|
|
6150
6084
|
const models = new AuxiliaryModels(ctx, settings);
|
|
6151
6085
|
installAutoSessionRoute(ctx, { settings });
|
|
6152
|
-
const storageRoot = resolveCheckpointRoot(process.env.DSH_HOME ??
|
|
6153
|
-
const usage = new UsageLedger(
|
|
6086
|
+
const storageRoot = resolveCheckpointRoot(process.env.DSH_HOME ?? join9(homedir2(), ".dsh"));
|
|
6087
|
+
const usage = new UsageLedger(join9(storageRoot, "..", "usage"), (message) => {
|
|
6154
6088
|
ctx.logger.warn(message);
|
|
6155
6089
|
});
|
|
6156
6090
|
const balancesFacade = (() => {
|
|
@@ -6200,6 +6134,8 @@ function apply(ctx, config) {
|
|
|
6200
6134
|
);
|
|
6201
6135
|
ctx.effect(() => ctx.typert.register(hostContribution));
|
|
6202
6136
|
const store = new CheckpointStore(storageRoot);
|
|
6137
|
+
const cutStore = new SessionCutStore(join9(storageRoot, "cuts"));
|
|
6138
|
+
const cutCache = /* @__PURE__ */ new Map();
|
|
6203
6139
|
const sessions = new DshCheckpointSessions(ctx);
|
|
6204
6140
|
let rollbackEnabled = config.rollbackEnabled.get();
|
|
6205
6141
|
const captures = new DshCheckpointCaptureSwitch(ctx, { store, pluginVersion: PACKAGE_VERSION });
|
|
@@ -6209,12 +6145,15 @@ function apply(ctx, config) {
|
|
|
6209
6145
|
pluginVersion: PACKAGE_VERSION,
|
|
6210
6146
|
resolveSession: sessions.resolveSession,
|
|
6211
6147
|
isEnabled: () => rollbackEnabled,
|
|
6212
|
-
awaitCaptures: () => captures.awaitPending()
|
|
6148
|
+
awaitCaptures: () => captures.awaitPending(),
|
|
6149
|
+
cutStore,
|
|
6150
|
+
cutCache
|
|
6213
6151
|
});
|
|
6214
6152
|
applyCheckpointRollback(ctx, rollback);
|
|
6215
6153
|
void rollback.recover().catch(() => {
|
|
6216
6154
|
ctx.logger.warn("Checkpoint rollback recovery is still pending");
|
|
6217
6155
|
});
|
|
6156
|
+
ctx.effect(() => installSessionCutFilter(ctx, { store: cutStore, cache: cutCache }));
|
|
6218
6157
|
ctx.effect(() => {
|
|
6219
6158
|
const unsubscribe = ctx.on("internal/update", (resolvedConfig, next) => {
|
|
6220
6159
|
rollbackEnabled = resolvedConfig.rollbackEnabled.get();
|
|
@@ -6266,6 +6205,14 @@ var CheckpointRollbackRemoteService = class extends TypertRemoteService {
|
|
|
6266
6205
|
throw this.remoteError(error, signal);
|
|
6267
6206
|
}
|
|
6268
6207
|
}
|
|
6208
|
+
async getCut(request, signal) {
|
|
6209
|
+
this.assertOwner();
|
|
6210
|
+
try {
|
|
6211
|
+
return await this.rollback.getCut(request, signal);
|
|
6212
|
+
} catch (error) {
|
|
6213
|
+
throw this.remoteError(error, signal);
|
|
6214
|
+
}
|
|
6215
|
+
}
|
|
6269
6216
|
assertOwner() {
|
|
6270
6217
|
if (this.ctx.invocation?.peer.principal.kind !== "owner") {
|
|
6271
6218
|
throw new RemoteError("agent-enhancer/forbidden", "Only the desktop owner may use checkpoint rollback", {});
|