dsh-session-relocate 0.2.5 → 0.2.7

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
@@ -274,8 +274,13 @@ node --test
274
274
  菜单那条路始终不受影响,`dragMove: false` 可以一键全关。
275
275
  - **嵌套工作区**(工作区套工作区)时,父分组里若渲染了子分组,父分组的会话行不再算投放目标
276
276
  —— 这是保守取舍:宁可少接管,也不猜错工作区。
277
- - **正在跑活的那一轮不会换工作目录**:会话在内存里的 header 是 deep-frozen 的,改不了;
278
- 文件与归属都搬走了,但那一轮的工具仍然用旧目录,**下次打开**才真正在新目录里跑。
277
+ - **正在跑活的那一轮不会换工作目录**:那一轮手里的工具工作目录是启动时定下的;
278
+ 文件、归属、以及会话的 header 都已经搬到新目录,但**这一轮**的工具仍用旧目录,**下次打开**才真正在新目录里跑。
279
+ (修复后内存里那条 live 会话的 header 也会跟着换 —— 参见下一条。)
280
+ - **原地接管会同时换两处 header**:写句柄的 `header`(决定后续写入落到哪)和内存里
281
+ `Session.header`(`session-query` 会拿它跟磁盘 header 逐字段比对)。只换前者的话,
282
+ `/api/session/list` 会因 `session source headers conflict` 整体报错、侧栏全空,直到重启 DSH
283
+ (issue #1)。换不了内存 header 时插件**整体放弃**这次接管(`handoff-failed`),一个字节都不动。
279
284
  - **正在跑活的接管依赖私有内部结构**(`tracker.writers`、写句柄的 `header` 槽位、`enqueueChain`、
280
285
  以及注册表的 `entity.mutate`)。DSH 换 rc 版本时这些都可能变,所以每一处都 feature-detect:
281
286
  探测不到就整体放弃(`handoff-failed`),绝不半途而废。
package/docs/DESIGN.md CHANGED
@@ -132,6 +132,13 @@ C:\ -> --C--- (只剥前导,所以尾部留下分
132
132
 
133
133
  ## B. 就地接管:正在跑的会话怎么搬
134
134
 
135
+ > **两处 header 必须一起换**(issue #1):写句柄的 `handle.header` 决定后续写入落到哪;
136
+ > 内存里 `Session.header` 则是 `dsh-session-query` 的 `assertSessionHeadersCompatible()` 拿来
137
+ > 与磁盘 artifact header 逐字段比对的那一份(`id / createdAt / cwd / parentSession / isSeeded /
138
+ > delegationDepth`)。只换前者会让 `session/list` 抛 `SESSION_QUERY_SOURCE_CONFLICT`,
139
+ > 而 `dsh-api-session-controller` 的 `list()` 没有逐条兜错 → 整个列表失败、侧栏全空,重启才恢复。
140
+ > `Session.header` 是普通字段(不是 getter,实例没被冻),换引用即可;换不了就整体放弃(`handoff-failed`)。
141
+
135
142
  **为什么 attachSession 不行**:它读 header 时优先取内存里的活会话,而活会话的 header 被
136
143
  `validateSessionHeader` 末尾的 `return deepFreeze(record)` 冻住了 —— cwd 改不了,
137
144
  校验必然失败。所以接管这条路要绕开它,直接走注册表自己的写路径。
@@ -92,6 +92,10 @@ body.dshsm-selecting [class*="_footArea"] > *:not([class*="_footerActions"]){dis
92
92
  /* 别家插件的页脚按钮让位 */
93
93
  body.dshsm-selecting [data-slot="sidebar.footer.action"] > [data-dsh-plugin]{display:none}
94
94
 
95
+ /* 按钮型入口一律让位(.dhns-footer-btn 这类没带 data-dsh-plugin 的)
96
+ 槽入口无包装层,'> button' 精确命中按钮型入口;:not([role="toolbar"]) 保证藏不到底栏自己 */
97
+ body.dshsm-selecting [data-slot="sidebar.footer.action"] > button:not([role="toolbar"]){display:none}
98
+
95
99
  /* entryRow 那个容器不带 data-dsh-plugin,单独点名 */
96
100
  body.dshsm-selecting [data-slot="sidebar.footer.action"] > [class*="_entryRow"]{display:none}
97
101
  ```
package/lib/client.js CHANGED
@@ -57,6 +57,12 @@ window.__ModuleLoader__.load({
57
57
  'body.dshsm-selecting [class*="_footArea"] > *:not([class*="_footerActions"]){display:none}',
58
58
  'body.dshsm-selecting [data-slot="sidebar.footer.action"] > [data-dsh-plugin]{display:none}',
59
59
  'body.dshsm-selecting [data-slot="sidebar.footer.action"] > [class*="_entryRow"]{display:none}',
60
+ // 页脚里的「按钮型」入口也要让位。槽入口是**无包装层**的直接子节点
61
+ // (renderer 的 SlotOutlet 是 <div data-slot style="display:contents">,入口组件直接挂成子节点),
62
+ // 所以 '> button' 精确命中按钮型入口 —— 例如 dsh-sidebar-declutter 的 .dhns-footer-btn,
63
+ // 它没带 data-dsh-plugin,上面两条规则都漏了它,多选时会和底栏挤在同一行。
64
+ // :not([role="toolbar"]) 保证永远藏不到底栏自己(两家底栏都是 div + role="toolbar")。
65
+ 'body.dshsm-selecting [data-slot="sidebar.footer.action"] > button:not([role="toolbar"]){display:none}',
60
66
  '.dshsm-picker-backdrop{position:fixed;inset:0;z-index:2147483602;display:flex;align-items:center;justify-content:center;background:rgba(0,0,0,.42)}',
61
67
  '.dshsm-picker{width:min(440px,92vw);max-height:70vh;overflow:auto;display:flex;flex-direction:column;gap:10px;padding:16px;border-radius:14px;border:1px solid transparent;box-shadow:0 18px 48px rgba(0,0,0,.36)}',
62
68
  '.dshsm-picker.dshsm-dark{background:#1f232b;color:#f2f3f5;border-color:rgba(255,255,255,.18);color-scheme:dark}',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-session-relocate",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "跨工作区移动 DSH 对话:把某个 Session 的日志文件与工作区归属整体搬到另一个工作区(可自动注册新工作区)。带日志级崩溃恢复的安全事务,并且会先安全释放「已打开但空闲」的会话 —— 所以正在打开的对话也能直接搬,不需要重启 DSH。",
5
5
  "type": "module",
6
6
  "main": "src/plugin.js",
package/scripts/e2e.mjs CHANGED
@@ -65,6 +65,35 @@ function seed() {
65
65
  process.stdout.write('seeded ' + SESSION_ID + ' -> ' + dir + '\n');
66
66
  }
67
67
 
68
+ /**
69
+ * 查一次 DSH 自己的 `session/list`(issue #1 的回归点)。
70
+ *
71
+ * dsh-session-query 会把**内存里 live Session 的 header** 和**磁盘 artifact 的 header**
72
+ * 逐字段比对;任何一处分叉都会让这个接口整体报 `SESSION_QUERY_SOURCE_CONFLICT`,
73
+ * 于是刷新后侧栏全空(重启才恢复)。这个 profile 没挂该接口时只提示、不判定失败。
74
+ */
75
+ async function assertSessionListHealthy(stage) {
76
+ const response = await fetch('http://127.0.0.1:' + PORT + '/api/session/list', {
77
+ method: 'POST',
78
+ headers: { 'content-type': 'application/json' },
79
+ body: JSON.stringify({
80
+ type: 'client-request',
81
+ rpcId: 'e2e-' + String(Date.now()),
82
+ method: 'session/list',
83
+ payload: { args: { _request: {} } },
84
+ }),
85
+ });
86
+ if (response.status === 404) {
87
+ process.stdout.write('list : 跳过(这个 profile 没挂 /api/session/list)\n');
88
+ return;
89
+ }
90
+ const payload = await response.json().catch(() => null);
91
+ if (payload === null || payload.ok === false) {
92
+ throw new Error('session/list 在「' + stage + '」之后不可用(header 分叉):' + JSON.stringify(payload === null ? response.status : payload.error));
93
+ }
94
+ process.stdout.write('list : 正常(' + stage + '之后)\n');
95
+ }
96
+
68
97
  async function post(path, body) {
69
98
  const response = await fetch(BASE + path, {
70
99
  method: 'POST',
@@ -182,6 +211,9 @@ async function live() {
182
211
  if (after.session?.live !== true) throw new Error('就地接管之后会话必须还活着');
183
212
  if (after.session?.unreadable === true) throw new Error('DSH 读不懂改写后的日志:' + String(after.session.message));
184
213
 
214
+ // issue #1 的回归点:迁移后 dsh 自己的会话列表必须还能用(header 内存/磁盘不许分叉)。
215
+ await assertSessionListHealthy('就地接管');
216
+
185
217
  const srcDir = join(HOME, 'sessions', projectKey(srcWs), encodeSegment(sessionId));
186
218
  const dstDir = join(HOME, 'sessions', projectKey(dstWs), encodeSegment(sessionId));
187
219
  if (existsSync(srcDir)) throw new Error('源目录还在:' + srcDir);
@@ -197,7 +229,7 @@ async function live() {
197
229
  process.stdout.write('drop : ' + JSON.stringify(dropped) + '\n');
198
230
  if (dropped.ok !== true) throw new Error('清理失败');
199
231
  if (existsSync(dstDir)) throw new Error('清理后目标目录还在');
200
- process.stdout.write('OK:就地接管(写链内搬 → 换 header → 释放锁 → 会话仍活 → 旧目录不复活 → 清理)全部正确\n');
232
+ process.stdout.write('OK:就地接管(写链内搬 → 换写句柄 + live Session 两处 header → 释放锁 → 会话仍活 → session/list 仍可用 → 旧目录不复活 → 清理)全部正确\n');
201
233
  }
202
234
 
203
235
 
package/src/plugin.js CHANGED
@@ -258,6 +258,37 @@ export function apply(ctx, config) {
258
258
  });
259
259
 
260
260
  /** 组装一次迁移需要的 DSH 运行时能力。 */
261
+ /**
262
+ * 内存里那条 live Session(SessionStore.get(id) 返回的就是 Session 本身)。
263
+ * 拿不到(老版本 / 没有 sessions 服务)就返回 undefined。
264
+ */
265
+ function liveSessionOf(sessionId) {
266
+ const sessions = ctx.get('sessions');
267
+ if (sessions === undefined || sessions === null || typeof sessions.get !== 'function') return undefined;
268
+ const session = sessions.get(sessionId);
269
+ return session === null || session === undefined ? undefined : session;
270
+ }
271
+
272
+ /**
273
+ * 把 Session.header 整体换成「同内容、只改 cwd」的新对象。
274
+ *
275
+ * 为什么必须做:dsh-session-query 的 assertSessionHeadersCompatible() 会逐字段比对
276
+ * **live Session 的 header** 与 **磁盘 artifact 的 header**(id / createdAt / cwd /
277
+ * parentSession / isSeeded / delegationDepth),不一致即抛 SESSION_QUERY_SOURCE_CONFLICT;
278
+ * 而 /api/session/list 没有逐条兜错,一条冲突就让整个列表报错(issues#1:刷新后侧栏全空)。
279
+ *
280
+ * 可行性:Session.header 是普通字段(`header;`),不是 getter,实例也没被冻;
281
+ * 被 deepFreeze 的是 header 那个对象本身。header 全是标量字段,
282
+ * 所以「新建对象 + 浅冻结」与深冻结等价。
283
+ */
284
+ function retargetSessionHeader(session, cwd) {
285
+ const previous = session.header;
286
+ if (previous === null || typeof previous !== 'object') throw new Error('live session 没有可用的 header');
287
+ const next = { ...previous, cwd };
288
+ session.header = Object.freeze(next);
289
+ return previous;
290
+ }
291
+
261
292
  function buildHost() {
262
293
  const registry = ctx.workspaceRegistry;
263
294
  return {
@@ -357,6 +388,16 @@ export function apply(ctx, config) {
357
388
  // 的坏状态(下一次写入就把旧目录重建出来)。整体放弃,让调用方改走释放路径。
358
389
  return { ok: false, code: 'no-writer', detail: '找不到这个会话的 live 写句柄' };
359
390
  }
391
+ // 内存里那条 live Session 的 header 必须能一起换:只换写句柄的话,磁盘 header
392
+ // 与内存 header 就分叉了,session/list 会整体报错(issues#1)。改不了就整体放弃,
393
+ // 绝不搬完文件再留一个「列表不可用」的状态。
394
+ const liveSession = liveSessionOf(sessionId);
395
+ if (liveSession === undefined) {
396
+ return { ok: false, code: 'no-live-session', detail: '拿不到内存里的 live Session,放弃接管(避免 session/list header 冲突)' };
397
+ }
398
+ if (liveSession.header === null || typeof liveSession.header !== 'object') {
399
+ return { ok: false, code: 'no-session-header', detail: 'live Session 没有 header,放弃接管(避免 session/list header 冲突)' };
400
+ }
360
401
  if (typeof handle.drainLive === 'function') await handle.drainLive();
361
402
  if (typeof handle.flush === 'function') await handle.flush();
362
403
 
@@ -365,20 +406,31 @@ export function apply(ctx, config) {
365
406
  const transaction = await handle.enqueueChain(async () => {
366
407
  if (typeof handle.ensureLease === 'function') await handle.ensureLease();
367
408
  const originalHeader = handle.header;
409
+ let originalSessionHeader;
368
410
  try {
369
411
  if (handle.lease !== undefined && handle.lease !== null && typeof handle.lease.release === 'function') {
370
412
  await handle.lease.release();
371
413
  }
372
414
  handle.lease = null;
373
415
  handle.header = { ...originalHeader, cwd: targetCwd };
416
+ // 内存 header 跟着换(否则 session-query 会认为磁盘与内存冲突)。
417
+ originalSessionHeader = retargetSessionHeader(liveSession, targetCwd);
374
418
  const result = await runTransaction();
375
- detail = '写链已接管:header.cwd -> ' + targetCwd;
419
+ detail = '写链已接管:header.cwd -> ' + targetCwd + '(写句柄 + live Session 同步)';
376
420
  result.writerRetargeted = true;
421
+ result.sessionHeaderRetargeted = true;
377
422
  return result;
378
423
  } catch (error) {
379
- // 回滚:恢复 header 与锁状态,让后续写入继续落在原路径。
424
+ // 回滚:恢复写句柄 header、内存 header 与锁状态,让后续写入继续落在原路径。
380
425
  handle.header = originalHeader;
381
426
  handle.lease = null;
427
+ if (originalSessionHeader !== undefined) {
428
+ try {
429
+ liveSession.header = originalSessionHeader;
430
+ } catch (rollbackError) {
431
+ log('warn', '回滚内存 header 失败:' + String(rollbackError && rollbackError.message));
432
+ }
433
+ }
382
434
  throw error;
383
435
  }
384
436
  });