@michengai/dsh-archive-manager 1.3.7 → 1.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
- import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
2
- import { basename, dirname, isAbsolute, join, normalize, resolve } from "node:path";
1
+ import { mkdir, readFile, rename, rm, stat, writeFile } from "node:fs/promises";
2
+ import { dirname, join, normalize, resolve } from "node:path";
3
3
  import zlib from "node:zlib";
4
4
  const ZSTD_MAGIC = 4247762216;
5
5
  const CHECKSUM_OPTS = { params: { [zlib.constants.ZSTD_c_checksumFlag]: 1 } };
@@ -12,6 +12,24 @@ function rawWorkspaceSessionIds(ws) {
12
12
  const raw = ws.record?.sessionIds;
13
13
  return Array.isArray(raw) ? raw : Array.isArray(ws.sessionIds) ? ws.sessionIds : [];
14
14
  }
15
+ function getInvalidSessionState(entry, id) {
16
+ if (!entry || !id || typeof entry !== "object") return void 0;
17
+ const e = entry;
18
+ if (typeof e.get === "function") return e.get(id);
19
+ if (typeof e.has === "function" && e.has(id)) return true;
20
+ return void 0;
21
+ }
22
+ function restoreInvalidSessionState(entry, id, state) {
23
+ if (!entry || !id || typeof entry !== "object") return;
24
+ const e = entry;
25
+ if (state === void 0) {
26
+ e.delete?.(id);
27
+ } else if (typeof e.set === "function") {
28
+ e.set(id, state);
29
+ } else if (typeof e.add === "function") {
30
+ e.add(id);
31
+ }
32
+ }
15
33
  function scanFirstZstdFrame(buf) {
16
34
  if (buf.length < 4) throw new Error("\u4F1A\u8BDD\u5DE5\u4EF6\u6570\u636E\u8FC7\u77ED\uFF0C\u65E0\u6CD5\u8BFB\u53D6 zstd \u5934\u90E8");
17
35
  if (buf.readUInt32LE(0) !== ZSTD_MAGIC) throw new Error("\u4F1A\u8BDD\u5DE5\u4EF6\u4E0D\u662F\u5408\u6CD5\u7684 zstd \u683C\u5F0F");
@@ -93,40 +111,6 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
93
111
  if (isExecuting) {
94
112
  throw new Error("\u8BE5\u4F1A\u8BDD\u6B63\u5904\u4E8E\u6267\u884C\u6216\u8FD0\u884C\u72B6\u6001\uFF0C\u8BF7\u7B49\u5F85\u5B8C\u6210\u540E\u518D\u79FB\u52A8\u3002");
95
113
  }
96
- let originalEntry;
97
- if (liveSession !== void 0) {
98
- if (typeof agent?.cancel === "function") {
99
- try {
100
- agent.cancel({ kind: "disposed" });
101
- await agent.whenIdle?.();
102
- } catch {
103
- }
104
- }
105
- if (typeof ctx.sessions?.flush === "function") {
106
- try {
107
- await ctx.sessions.flush(liveSession);
108
- } catch {
109
- }
110
- }
111
- try {
112
- await agent?.scope?.dispose?.();
113
- ctx.agents?.store?.delete(cleanSessionId);
114
- } catch {
115
- }
116
- originalEntry = ctx.sessions?.store?.get?.(cleanSessionId) ?? (typeof ctx.sessions?.liveEntryFor === "function" ? (() => {
117
- try {
118
- return ctx.sessions.liveEntryFor(liveSession);
119
- } catch {
120
- return void 0;
121
- }
122
- })() : void 0);
123
- if (typeof originalEntry?.detach === "function") {
124
- try {
125
- originalEntry.detach();
126
- } catch {
127
- }
128
- }
129
- }
130
114
  const persistence = ctx.sessionPersistence;
131
115
  let header;
132
116
  if (typeof registry.readSessionHeader === "function") {
@@ -158,10 +142,50 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
158
142
  return isOther && rawWorkspaceSessionIds(ws).includes(cleanSessionId);
159
143
  });
160
144
  if (currentNormalizedCwd && targetNormalizedCwd && currentNormalizedCwd === targetNormalizedCwd) {
161
- for (const ws of sourceWorkspaces) {
162
- await ws.detachSession(cleanSessionId);
145
+ const targetHadSession2 = rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId);
146
+ const previousPath2 = registry.sessionPaths?.get(cleanSessionId);
147
+ const previousInvalidPath2 = getInvalidSessionState(registry.invalidSessionPaths, cleanSessionId);
148
+ const wasMissing2 = registry.missingSessionPaths?.has(cleanSessionId);
149
+ const attemptedSources2 = [];
150
+ try {
151
+ await targetWorkspace.attachSession(cleanSessionId);
152
+ if (!rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId)) {
153
+ throw new Error("\u76EE\u6807\u5DE5\u4F5C\u533A\u672A\u4FDD\u7559\u4F1A\u8BDD\u5173\u8054\uFF0C\u5DF2\u53D6\u6D88\u79FB\u52A8\u3002");
154
+ }
155
+ for (const ws of sourceWorkspaces) {
156
+ attemptedSources2.push(ws);
157
+ await ws.detachSession(cleanSessionId);
158
+ }
159
+ } catch (error) {
160
+ const rollbackErrors = [];
161
+ for (const ws of attemptedSources2) {
162
+ if (rawWorkspaceSessionIds(ws).includes(cleanSessionId)) continue;
163
+ try {
164
+ await ws.attachSession(cleanSessionId);
165
+ if (!rawWorkspaceSessionIds(ws).includes(cleanSessionId)) {
166
+ throw new Error(`\u539F\u5DE5\u4F5C\u533A\u672A\u6062\u590D\u4F1A\u8BDD\u5173\u8054\uFF1A${ws.id}`);
167
+ }
168
+ } catch (restoreError) {
169
+ rollbackErrors.push(restoreError);
170
+ }
171
+ }
172
+ if (!rollbackErrors.length && !targetHadSession2 && rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId)) {
173
+ try {
174
+ await targetWorkspace.detachSession(cleanSessionId);
175
+ } catch (restoreError) {
176
+ rollbackErrors.push(restoreError);
177
+ }
178
+ }
179
+ if (rollbackErrors.length) {
180
+ throw new AggregateError([error, ...rollbackErrors], `\u79FB\u52A8\u5931\u8D25\uFF0C\u4E14\u672A\u80FD\u5B8C\u6574\u6062\u590D\u5DE5\u4F5C\u533A\u5173\u8054\uFF1A${String(error)}`);
181
+ }
182
+ if (previousPath2 === void 0) registry.sessionPaths?.delete(cleanSessionId);
183
+ else registry.sessionPaths?.set(cleanSessionId, previousPath2);
184
+ restoreInvalidSessionState(registry.invalidSessionPaths, cleanSessionId, previousInvalidPath2);
185
+ if (!wasMissing2) registry.missingSessionPaths?.delete(cleanSessionId);
186
+ else registry.missingSessionPaths?.add(cleanSessionId);
187
+ throw error;
163
188
  }
164
- await targetWorkspace.attachSession(cleanSessionId);
165
189
  registry.sessionPaths?.set(cleanSessionId, targetPath);
166
190
  if (ctx.sessionProjectionCache && typeof ctx.sessionProjectionCache.coldSnapshot === "function") {
167
191
  try {
@@ -196,6 +220,12 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
196
220
  toWorkspacePath: targetPath
197
221
  };
198
222
  }
223
+ const requireReleasedSession = () => {
224
+ if (ctx.sessions?.get?.(cleanSessionId) || ctx.agents?.get?.(cleanSessionId)) {
225
+ throw new Error("\u8BE5\u4F1A\u8BDD\u4ECD\u88AB\u5BBF\u4E3B\u6301\u6709\uFF0C\u65E0\u6CD5\u8DE8\u76EE\u5F55\u79FB\u52A8\u3002\u8BF7\u5148\u5173\u95ED\u8BE5\u4F1A\u8BDD\u518D\u91CD\u8BD5\u3002");
226
+ }
227
+ };
228
+ requireReleasedSession();
199
229
  if (!persistence || typeof persistence.locate !== "function") {
200
230
  throw new Error("\u5F53\u524D\u6301\u4E45\u5316\u540E\u7AEF\u4E0D\u652F\u6301\u7269\u7406\u5B9A\u4F4D\uFF0C\u65E0\u6CD5\u8DE8\u5DE5\u4F5C\u533A\u79FB\u52A8\u4F1A\u8BDD\u3002");
201
231
  }
@@ -232,7 +262,12 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
232
262
  throw new Error("\u8FC1\u79FB\u524D\u6821\u9A8C\u5931\u8D25\uFF1A\u6570\u636E\u6539\u5199\u9A8C\u8BC1\u672A\u901A\u8FC7\u3002");
233
263
  }
234
264
  }
235
- const detachedWorkspaces = [];
265
+ requireReleasedSession();
266
+ const targetHadSession = rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId);
267
+ const previousPath = registry.sessionPaths?.get(cleanSessionId);
268
+ const previousInvalidPath = getInvalidSessionState(registry.invalidSessionPaths, cleanSessionId);
269
+ const wasMissing = registry.missingSessionPaths?.has(cleanSessionId) ?? false;
270
+ const attemptedSources = [];
236
271
  let targetAttached = false;
237
272
  let stageFile = null;
238
273
  let sourceDirMoved = false;
@@ -250,23 +285,15 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
250
285
  registry.sessionPaths?.set(cleanSessionId, targetPath);
251
286
  registry.headers?.set(cleanSessionId, targetHeader);
252
287
  registry.invalidSessionPaths?.delete(cleanSessionId);
253
- if (liveSession !== void 0) {
254
- try {
255
- if ("header" in liveSession && liveSession.header) {
256
- liveSession.header.cwd = targetPath;
257
- }
258
- if ("cwd" in liveSession) {
259
- liveSession.cwd = targetPath;
260
- }
261
- } catch {
262
- }
288
+ await targetWorkspace.attachSession(cleanSessionId);
289
+ if (!rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId)) {
290
+ throw new Error("\u76EE\u6807\u5DE5\u4F5C\u533A\u672A\u4FDD\u7559\u4F1A\u8BDD\u5173\u8054\uFF0C\u5DF2\u53D6\u6D88\u79FB\u52A8\u3002");
263
291
  }
292
+ targetAttached = true;
264
293
  for (const ws of sourceWorkspaces) {
294
+ attemptedSources.push(ws);
265
295
  await ws.detachSession(cleanSessionId);
266
- detachedWorkspaces.push(ws);
267
296
  }
268
- await targetWorkspace.attachSession(cleanSessionId);
269
- targetAttached = true;
270
297
  if (ctx.sessionProjectionCache && typeof ctx.sessionProjectionCache.coldSnapshot === "function") {
271
298
  try {
272
299
  if (ctx.sessionProjectionCache.coldSnapshot.length === 1) {
@@ -291,21 +318,7 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
291
318
  }
292
319
  }
293
320
  } catch (error) {
294
- if (header) {
295
- registry.headers?.set(cleanSessionId, header);
296
- if (header.cwd) registry.sessionPaths?.set(cleanSessionId, header.cwd);
297
- }
298
- if (liveSession !== void 0 && header) {
299
- try {
300
- if ("header" in liveSession && liveSession.header) {
301
- liveSession.header.cwd = header.cwd;
302
- }
303
- if ("cwd" in liveSession && header.cwd) {
304
- liveSession.cwd = header.cwd;
305
- }
306
- } catch {
307
- }
308
- }
321
+ const rollbackErrors = [];
309
322
  if (stageFile) await rm(stageFile, { force: true }).catch(() => {
310
323
  });
311
324
  await rm(targetSessionDir, { recursive: true, force: true }).catch(() => {
@@ -315,16 +328,37 @@ async function moveSessionToWorkspace(ctx, sessionId, targetWorkspaceId) {
315
328
  await mkdir(dirname(sourceSessionDir), { recursive: true });
316
329
  await rename(backupDir, sourceSessionDir);
317
330
  } catch (restoreErr) {
318
- throw new AggregateError([error, restoreErr], `\u79FB\u52A8\u5931\u8D25\u4E14\u539F\u5DE5\u4EF6\u56DE\u6EDA\u5F02\u5E38\uFF0C\u5907\u4EFD\u6682\u5B58\u4E8E: ${backupDir}`);
331
+ rollbackErrors.push(restoreErr);
332
+ }
333
+ }
334
+ if (header) {
335
+ registry.headers?.set(cleanSessionId, header);
336
+ }
337
+ if (previousPath === void 0) registry.sessionPaths?.delete(cleanSessionId);
338
+ else registry.sessionPaths?.set(cleanSessionId, previousPath);
339
+ restoreInvalidSessionState(registry.invalidSessionPaths, cleanSessionId, previousInvalidPath);
340
+ if (!wasMissing) registry.missingSessionPaths?.delete(cleanSessionId);
341
+ else registry.missingSessionPaths?.add(cleanSessionId);
342
+ for (const ws of attemptedSources) {
343
+ if (rawWorkspaceSessionIds(ws).includes(cleanSessionId)) continue;
344
+ try {
345
+ await ws.attachSession(cleanSessionId);
346
+ if (!rawWorkspaceSessionIds(ws).includes(cleanSessionId)) {
347
+ throw new Error(`\u539F\u5DE5\u4F5C\u533A\u672A\u6062\u590D\u4F1A\u8BDD\u5173\u8054\uFF1A${ws.id}`);
348
+ }
349
+ } catch (restoreError) {
350
+ rollbackErrors.push(restoreError);
319
351
  }
320
352
  }
321
- if (targetAttached) {
322
- await targetWorkspace.detachSession(cleanSessionId).catch(() => {
323
- });
353
+ if (!rollbackErrors.length && targetAttached && !targetHadSession && rawWorkspaceSessionIds(targetWorkspace).includes(cleanSessionId)) {
354
+ try {
355
+ await targetWorkspace.detachSession(cleanSessionId);
356
+ } catch (restoreError) {
357
+ rollbackErrors.push(restoreError);
358
+ }
324
359
  }
325
- for (const ws of detachedWorkspaces) {
326
- await ws.attachSession(cleanSessionId).catch(() => {
327
- });
360
+ if (rollbackErrors.length) {
361
+ throw new AggregateError([error, ...rollbackErrors], `\u7269\u7406\u79FB\u52A8\u5931\u8D25\uFF0C\u4E14\u672A\u80FD\u5B8C\u6574\u6062\u590D\u5DE5\u4EF6\u6216\u5DE5\u4F5C\u533A\u5173\u8054\uFF1A${String(error)}`);
328
362
  }
329
363
  throw error;
330
364
  }
package/package.json CHANGED
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "@michengai/dsh-archive-manager",
3
- "version": "1.3.7",
3
+ "version": "1.4.1",
4
4
  "description": "归档、恢复、收藏和永久删除会话,并诊断损坏的会话日志。 · Archive, restore, favorite, and permanently delete sessions, and diagnose damaged session logs.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@11.22.0",
7
7
  "engines": {
8
8
  "node": "^22.19.0 || >=24.0.0",
9
- "dsh": ">=0.2.0-rc.1 <0.3.0-0"
9
+ "dsh": ">=0.2.2-rc.1 <0.3.0-0"
10
10
  },
11
11
  "main": "lib/index.js",
12
12
  "exports": {
@@ -52,21 +52,21 @@
52
52
  },
53
53
  "peerDependencies": {
54
54
  "@deepseek-ai/cordis": ">=4.0.1 <5.0.0",
55
- "@deepseek-ai/dsh-client-connection": ">=0.2.0-rc.1 <0.3.0-0",
56
- "@deepseek-ai/dsh-client-locale": ">=0.2.0-rc.1 <0.3.0-0",
57
- "@deepseek-ai/dsh-client-ui-conversation": ">=0.2.0-rc.1 <0.3.0-0",
58
- "@deepseek-ai/dsh-client-ui-primitives": ">=0.2.0-rc.1 <0.3.0-0",
59
- "@deepseek-ai/dsh-client-ui-sidebar": ">=0.2.0-rc.1 <0.3.0-0",
60
- "@deepseek-ai/dsh-client-ui-workspace": ">=0.2.0-rc.1 <0.3.0-0",
61
- "@deepseek-ai/dsh-host-webserver": ">=0.2.0-rc.1 <0.3.0-0",
62
- "@deepseek-ai/dsh-session": ">=0.2.0-rc.1 <0.3.0-0",
63
- "@deepseek-ai/dsh-session-format-catalog": ">=0.2.0-rc.1 <0.3.0-0",
64
- "@deepseek-ai/dsh-session-projection-cache": ">=0.2.0-rc.1 <0.3.0-0",
65
- "@deepseek-ai/dsh-spill-local": ">=0.2.0-rc.1 <0.3.0-0",
66
- "@deepseek-ai/dsh-storage-domain": ">=0.2.0-rc.1 <0.3.0-0",
67
- "@deepseek-ai/dsh-typert-protocol": ">=0.2.0-rc.1 <0.3.0-0",
68
- "@deepseek-ai/dsh-typert-registry": ">=0.2.0-rc.1 <0.3.0-0",
69
- "@deepseek-ai/dsh-workspace": ">=0.2.0-rc.1 <0.3.0-0"
55
+ "@deepseek-ai/dsh-client-connection": ">=0.2.2-rc.1 <0.3.0-0",
56
+ "@deepseek-ai/dsh-client-locale": ">=0.2.2-rc.1 <0.3.0-0",
57
+ "@deepseek-ai/dsh-client-ui-conversation": ">=0.2.2-rc.1 <0.3.0-0",
58
+ "@deepseek-ai/dsh-client-ui-primitives": ">=0.2.2-rc.1 <0.3.0-0",
59
+ "@deepseek-ai/dsh-client-ui-sidebar": ">=0.2.2-rc.1 <0.3.0-0",
60
+ "@deepseek-ai/dsh-client-ui-workspace": ">=0.2.2-rc.1 <0.3.0-0",
61
+ "@deepseek-ai/dsh-host-webserver": ">=0.2.2-rc.1 <0.3.0-0",
62
+ "@deepseek-ai/dsh-session": ">=0.2.2-rc.1 <0.3.0-0",
63
+ "@deepseek-ai/dsh-session-format-catalog": ">=0.2.2-rc.1 <0.3.0-0",
64
+ "@deepseek-ai/dsh-session-projection-cache": ">=0.2.2-rc.1 <0.3.0-0",
65
+ "@deepseek-ai/dsh-spill-local": ">=0.2.2-rc.1 <0.3.0-0",
66
+ "@deepseek-ai/dsh-storage-domain": ">=0.2.2-rc.1 <0.3.0-0",
67
+ "@deepseek-ai/dsh-typert-protocol": ">=0.2.2-rc.1 <0.3.0-0",
68
+ "@deepseek-ai/dsh-typert-registry": ">=0.2.2-rc.1 <0.3.0-0",
69
+ "@deepseek-ai/dsh-workspace": ">=0.2.2-rc.1 <0.3.0-0"
70
70
  },
71
71
  "peerDependenciesMeta": {
72
72
  "@deepseek-ai/dsh-session-format-catalog": {