dsh-session-relocate 0.2.2 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -81,6 +81,12 @@ curl http://127.0.0.1:3080/api/dsh-session-relocate/status
81
81
 
82
82
  想只搬父对话:**Plugins → dsh-session-relocate → 配置** 里把「连同子对话一起移动」关掉(存在浏览器本地,默认开)。关掉后子会话留在旧工作区 —— 旧工作区先别删,子会话日志还在里面。
83
83
 
84
+ ### 目录已经在目标里、但没归属(未分组)
85
+
86
+ 如果会话的 `cwd` 已经在某个工作区的目录下,但注册表里没有它的归属(侧栏显示「未分组」),
87
+ 「移动」不会再返回 `already-there` —— 插件会把归属补写进目标工作区(回执 `applied: "attached"`),
88
+ 文件一个字节都不动。菜单也不会再把这种会话当成「已经在目标里」,目标列表会正常给出那个工作区。
89
+
84
90
  ### 正在回答的对话也能搬
85
91
 
86
92
  **包括正在回答、正跑子代理、正跑后台任务的那个。** 插件会先拿到它的写所有权,再动文件:
package/docs/DESIGN.md CHANGED
@@ -89,6 +89,13 @@ C:\ -> --C--- (只剥前导,所以尾部留下分
89
89
  因此本插件在 attach 前调用私有的 `replaceHeaderIndex(listStoredHeaders())`(feature-detect,
90
90
  拿不到就降级为「下次启动补齐归属」)。
91
91
 
92
+ **already-there 的判定**:目录相同(`cwd == target.path`)**不等于**已在目标工作区 —— DSH 的分组
93
+ 依据是 `workspace.json` 的 `sessionIds`。目录在、归属不在(孤儿/未分组)时,`relocateSession` 走
94
+ `attachExisting`:优先 `host.adoptSession`,老宿主退回 `adoptSessions + detachSessions`,补写归属后
95
+ 返回 `applied: "attached"`,不动文件、不写迁移日志。`/describe` 的 `currentWorkspaceId` 也**只认归属**,
96
+ 并额外返回 `memberOfWorkspaceId` / `cwdMatchesWorkspaceId`,避免 UI 把孤儿会话当成「已在目标工作区」
97
+ 而不给补归属的入口。
98
+
92
99
  ## Zstandard 容器
93
100
 
94
101
  - 日志是**一串彼此独立的 Zstandard 帧**:第一帧**恰好只有 header 一行**,后续每批一帧。
package/lib/client.js CHANGED
@@ -128,6 +128,7 @@ window.__ModuleLoader__.load({
128
128
  moving: '正在移动…',
129
129
  done: '已移动',
130
130
  pending: '文件已搬移;工作区归属会在下次启动 DSH 时自动补齐',
131
+ attached: '已补上「{ws}」的工作区归属',
131
132
  cannot: '这个对话无法移动',
132
133
  failed: '移动失败',
133
134
  needsReload: '(刷新页面后生效)',
@@ -163,6 +164,7 @@ window.__ModuleLoader__.load({
163
164
  moving: 'Moving…',
164
165
  done: 'Moved',
165
166
  pending: 'Files moved; the workspace accounting is completed on the next DSH start',
167
+ attached: 'Workspace membership restored for “{ws}”',
166
168
  cannot: 'This conversation cannot be moved',
167
169
  failed: 'Move failed',
168
170
  needsReload: ' (reload the page to apply)',
@@ -378,9 +380,14 @@ window.__ModuleLoader__.load({
378
380
  return button('error', strings.failed + ':' + state.message, { disabled: true, className: 'dshsm-item dshsm-bad' });
379
381
  }
380
382
  if (state.phase === 'done') {
381
- var suffix = state.applied === 'pending' ? strings.pending : (state.applied === 'live' ? '' : strings.needsReload);
383
+ var doneText = state.applied === 'attached'
384
+ ? strings.attached.replace('{ws}', String(state.title))
385
+ : strings.done + ' ' + String(state.title);
386
+ var suffix = state.applied === 'pending'
387
+ ? strings.pending
388
+ : (state.applied === 'live' || state.applied === 'attached' ? '' : strings.needsReload);
382
389
  var note = typeof state.note === 'string' ? state.note : '';
383
- return button('done', strings.done + ' ' + String(state.title) + note + (suffix.length > 0 ? ';' + suffix : ''), { disabled: true });
390
+ return button('done', doneText + note + (suffix.length > 0 ? ';' + suffix : ''), { disabled: true });
384
391
  }
385
392
  // picking
386
393
  var nodes = [button('caption', strings.pick + (state.targets.length === 0 ? ' ' + strings.noTargets : ''), { disabled: true })];
@@ -645,7 +652,10 @@ window.__ModuleLoader__.load({
645
652
  if (result !== null && result !== undefined && result.ok === true) {
646
653
  var title = result.targetTitle;
647
654
  var label = typeof title === 'string' && title.length > 0 ? title : targetWorkspaceId;
648
- toast(strings.dragMoved.replace('{ws}', label) + receiptNotes(result));
655
+ var summary = result.applied === 'attached'
656
+ ? strings.attached.replace('{ws}', label)
657
+ : strings.dragMoved.replace('{ws}', label);
658
+ toast(summary + receiptNotes(result));
649
659
  if (bulkState.selecting === true) exitBulk();
650
660
  else if (hostSelected === true) dismissHostSelection();
651
661
  return;
@@ -877,8 +887,13 @@ window.__ModuleLoader__.load({
877
887
  if (result !== null && typeof result === 'object' && result.ok === true) {
878
888
  if (result.verification === 'sampled') sampled = true;
879
889
  if (Number.isSafeInteger(result.releaseAttempts) && result.releaseAttempts > releaseRetries) releaseRetries = result.releaseAttempts;
880
- if (twoPhase) deferred.push(sessionId);
881
- else {
890
+ if (result.applied === 'attached') {
891
+ // 目录本来就在目标、只补了归属:没有可提交的文件事务,直接算已搬。
892
+ moved += 1;
893
+ record({ sessionId: sessionId, status: 'moved', targetWorkspaceId: targetWorkspaceId, applied: 'attached' });
894
+ } else if (twoPhase) {
895
+ deferred.push(sessionId);
896
+ } else {
882
897
  moved += 1;
883
898
  record({ sessionId: sessionId, status: 'moved', targetWorkspaceId: targetWorkspaceId });
884
899
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-relocate",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "跨工作区移动 DSH 对话:把某个 Session 的日志文件与工作区归属整体搬到另一个工作区(可自动注册新工作区)。带日志级崩溃恢复的安全事务,并且会先安全释放「已打开但空闲」的会话 —— 所以正在打开的对话也能直接搬,不需要重启 DSH。",
5
5
  "type": "module",
6
6
  "main": "src/plugin.js",
package/src/plugin.js CHANGED
@@ -582,7 +582,8 @@ export function apply(ctx, config) {
582
582
  /** 描述当前状态:可选带一个 sessionId,用来告诉界面「能不能搬、为什么不能」。 */
583
583
  async function describe(body) {
584
584
  const registry = ctx.workspaceRegistry;
585
- const workspaces = registry.list().map((entity) => ({ id: entity.id, title: entity.title, path: entity.path }));
585
+ const entities = registry.list();
586
+ const workspaces = entities.map((entity) => ({ id: entity.id, title: entity.title, path: entity.path }));
586
587
  const payload = {
587
588
  ok: true,
588
589
  version: PLUGIN_VERSION,
@@ -616,9 +617,12 @@ export function apply(ctx, config) {
616
617
  canonical = undefined;
617
618
  }
618
619
  }
619
- const current = canonical === undefined
620
- ? undefined
621
- : workspaces.find((workspace) => samePath(workspace.path, canonical));
620
+ // 「当前工作区」只认注册表归属;cwd 相同但没归属的孤儿,必须留在目标列表里(补归属入口)。
621
+ const member = entities.find((entity) => Array.isArray(entity.sessionIds) && entity.sessionIds.indexOf(sessionId) >= 0);
622
+ const cwdMatch = canonical === undefined ? undefined : entities.find((entity) => samePath(entity.path, canonical));
623
+ const memberId = member === undefined ? null : member.id;
624
+ const cwdMatchId = cwdMatch === undefined ? null : cwdMatch.id;
625
+ const currentId = memberId;
622
626
  const activity = await host.listActivity(sessionId);
623
627
  const live = host.isSessionLive(sessionId);
624
628
  // 就地接管可用时,连「正在跑活」都能搬(换掉 live 写句柄的 header,turn 不断)。
@@ -634,7 +638,10 @@ export function apply(ctx, config) {
634
638
  id: sessionId,
635
639
  cwd: cwd ?? null,
636
640
  canonicalCwd: canonical ?? null,
637
- currentWorkspaceId: current === undefined ? null : current.id,
641
+ currentWorkspaceId: currentId,
642
+ memberOfWorkspaceId: memberId,
643
+ cwdMatchesWorkspaceId: cwdMatchId,
644
+ currentWorkspaceBy: member !== undefined ? 'membership' : null,
638
645
  live,
639
646
  willHandoff: handoffAvailable && reason === null,
640
647
  willRelease: live && !handoffAvailable && reason === null,
@@ -645,7 +652,9 @@ export function apply(ctx, config) {
645
652
  };
646
653
  payload.workspaces = workspaces.map((workspace) => ({
647
654
  ...workspace,
648
- current: workspace.id === payload.session.currentWorkspaceId,
655
+ current: workspace.id === currentId,
656
+ member: workspace.id === memberId,
657
+ cwdMatches: workspace.id === cwdMatchId,
649
658
  }));
650
659
  return payload;
651
660
  }
package/src/relocate.js CHANGED
@@ -347,6 +347,28 @@ export function newOpId() {
347
347
  * ``releaseLive`` 为 false 时恢复旧行为(打开中的会话直接拒绝)。
348
348
  * @returns 迁移回执。
349
349
  */
350
+ /**
351
+ * 会话目录已经在目标工作区的 path 上、但注册表里没有这条归属时,补写归属(不动文件)。
352
+ *
353
+ * 「cwd 相同」不等于「已在目标工作区」:DSH 的分组依据是 workspace.json 的 sessionIds,
354
+ * 两者可以不一致(本次的孤儿/未分组会话)。优先走 host.adoptSession(live/frozen header
355
+ * 的旁路),老宿主退回 adoptSessions + detachSessions。
356
+ */
357
+ async function attachExisting(host, sessionId, target, source) {
358
+ if (typeof host.adoptSession === 'function') {
359
+ return await host.adoptSession(sessionId, target, source, target.path);
360
+ }
361
+ if (typeof host.adoptSessions === 'function') {
362
+ const adopted = await host.adoptSessions([sessionId], target, target.path);
363
+ if (adopted !== null && adopted !== undefined && adopted.applied === true
364
+ && source !== undefined && source.id !== target.id && typeof host.detachSessions === 'function') {
365
+ await host.detachSessions([sessionId], source);
366
+ }
367
+ return adopted;
368
+ }
369
+ return { applied: false, reason: '宿主没有提供补写归属的接口' };
370
+ }
371
+
350
372
  export async function relocateSession(host, store, request) {
351
373
  const sessionId = request.sessionId;
352
374
  if (typeof sessionId !== 'string' || sessionId.length === 0) {
@@ -383,7 +405,33 @@ export async function relocateSession(host, store, request) {
383
405
  throw new MoveError('source-cwd-unresolved', '这个对话记录的目录已不存在:' + header.cwd + ',无法安全迁移。');
384
406
  }
385
407
  if (samePath(sourceCanonical, target.path)) {
386
- throw new MoveError('already-there', '这个对话已经在「' + target.title + '」里了。');
408
+ // 目录相同 ≠ 已在目标工作区:DSH 的分组依据是注册表 sessionIds。
409
+ // 目录在、归属不在(孤儿/未分组)时这是一次**可修复**的状态,不能当成终态。
410
+ const member = Array.isArray(target.sessionIds) && target.sessionIds.indexOf(sessionId) >= 0;
411
+ if (member) throw new MoveError('already-there', '这个对话已经在「' + target.title + '」里了。');
412
+ const source = host.findWorkspaceByPath(sourceCanonical);
413
+ const attached = await attachExisting(host, sessionId, target, source);
414
+ if (attached !== null && attached !== undefined && attached.applied === true) {
415
+ if (typeof host.invalidateProjection === 'function') await host.invalidateProjection(sessionId);
416
+ return {
417
+ ok: true,
418
+ sessionId,
419
+ from: sourceCanonical,
420
+ to: target.path,
421
+ targetWorkspaceId: target.id,
422
+ targetTitle: target.title,
423
+ applied: 'attached',
424
+ released: false,
425
+ handedOff: false,
426
+ verification: 'full',
427
+ };
428
+ }
429
+ throw new MoveError(
430
+ 'attach-failed',
431
+ '这个对话的目录已经在「' + target.title + '」里,但补写工作区归属失败:'
432
+ + String(attached !== null && attached !== undefined && attached.reason !== undefined ? attached.reason : '未知原因')
433
+ + '。请重启 DSH 后再试。',
434
+ );
387
435
  }
388
436
 
389
437
  // 三种情况: