@psyduck25/dsh-session-manager 0.4.1 → 0.6.0
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 +49 -1
- package/docs/DETAIL.md +14 -0
- package/docs/DETAIL.zh.md +22 -0
- package/lib/client.js +231 -2
- package/lib/index.js +222 -0
- package/lib/migrate.js +323 -0
- package/lib/state.js +24 -2
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -25,7 +25,10 @@ DSH 本身没有「删除单个会话」:左侧列表只能归档会话,要
|
|
|
25
25
|
## 能做什么
|
|
26
26
|
|
|
27
27
|
- **按工作区分组列出所有会话**,带工作区名称、路径、会话标题、最后活动时间;正在运行的会话和日志缺失的会话都有标记。
|
|
28
|
-
-
|
|
28
|
+
- **锁定**:点锁定后该行的删除和迁移都变灰,服务端也会拒绝(不是只靠界面禁用)。再次点击解锁。
|
|
29
|
+
- **迁移到别的工作区**:每行一个下拉框,列出当前 DSH 的所有工作区;选中后行内弹出确认条,确认即迁。
|
|
30
|
+
这是**真迁移**——磁盘上的日志目录会搬到新工作区对应的项目目录,会话头里的 `cwd` 也一起改写,
|
|
31
|
+
否则 DSH 下次按 cwd 过滤时会把你搬过的会话摘掉。详见下面的「迁移到底做了什么」。
|
|
29
32
|
- **删除要二次确认**:点删除先在行内展开确认条,写明这个会话会进缓冲站、多少小时内可回档;缓冲时长为 0 时确认文案直接说「无法撤销」。
|
|
30
33
|
- **删除缓冲站**:二次确认通过的会话落到页面底部,同时**从左侧工作区列表消失**。
|
|
31
34
|
- **回档**:缓冲期内点「回档」恢复原样——日志文件搬回原目录、解除归档,所以标题、位置、工作区归属都不变。
|
|
@@ -58,6 +61,46 @@ DSH 本身没有「删除单个会话」:左侧列表只能归档会话,要
|
|
|
58
61
|
缓冲站里每行还有 **彻底删除**:点一下先展开一行确认(「彻底删除」紧挨着「回档」,
|
|
59
62
|
误点的代价是日志直接没了,所以照样要确认一次),确认后立即销毁日志,不等到期。
|
|
60
63
|
|
|
64
|
+
## 迁移到底做了什么
|
|
65
|
+
|
|
66
|
+
DSH 的工作区归属**不是**一个可以随便改的字段,它完全由会话头里的 `cwd` 推算:
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
// @deepseek-ai/dsh-workspace
|
|
70
|
+
get sessionIds() {
|
|
71
|
+
return this.record.sessionIds.filter((id) => this.host.candidateMatches(id, this.record.path))
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`candidateMatches` 只认「会话头 cwd 的 realpath == 工作区 path」,`attachSession` 在写入前
|
|
76
|
+
也会显式校验并在不匹配时抛错。而 cwd 又决定了日志文件在磁盘上的位置:
|
|
77
|
+
|
|
78
|
+
```
|
|
79
|
+
$DSH_HOME/sessions/<projectKey(cwd)>/<会话 id>/session.v4.jsonl.zstd
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
所以「把会话挪到另一个工作区」必然包含两件磁盘操作,少一件都不成立:
|
|
83
|
+
|
|
84
|
+
1. **改写会话头里的 cwd**。日志是多帧 zstd,会话头在第 1 帧。插件只重压第 1 帧,
|
|
85
|
+
后面的帧原样保留——文件其余部分一个字节都不动。
|
|
86
|
+
2. **把会话目录搬到新 cwd 对应的项目目录**。
|
|
87
|
+
|
|
88
|
+
顺序是刻意安排的:**先复制、再验证、最后才删原件**。副本要能读回新的 cwd、
|
|
89
|
+
且帧数和原件一致,才算成功;任何一步失败都会清掉半成品,原会话保持原样。
|
|
90
|
+
|
|
91
|
+
**需要重启 DSH 才看得到效果**:工作区的内存索引是启动时建立的一次性快照,
|
|
92
|
+
它不会感知磁盘上的 cwd 变了。所以迁移成功后左侧列表暂时不变;插件会把这个会话记进
|
|
93
|
+
`pending`,**下次启动时自动补登记**,不用你再点一次。
|
|
94
|
+
|
|
95
|
+
**权限不够时**:插件不去偷偷改权限,而是把「要做什么」和「源目录 / 目标目录的真实路径」
|
|
96
|
+
摆在一个对话框里,由你决定——重试,或者「只改变归属」。
|
|
97
|
+
|
|
98
|
+
> **「只改变归属」为什么不生效**:cwd 没变,DSH 下次按 cwd 过滤时会把它摘掉、
|
|
99
|
+
> 放回「未分组」。这是 DSH 的设计,不是插件偷懒。插件会把这件事如实告诉你,
|
|
100
|
+
> 而不是让你以为迁移成功了。
|
|
101
|
+
|
|
102
|
+
被锁定的会话不允许迁移,这条和删除一样由宿主校验。
|
|
103
|
+
|
|
61
104
|
## 数据放在哪
|
|
62
105
|
|
|
63
106
|
`$DSH_HOME/session-manager/state.json`(默认 `~/.dsh/session-manager/state.json`):
|
|
@@ -103,6 +146,7 @@ DSH 本身没有「删除单个会话」:左侧列表只能归档会话,要
|
|
|
103
146
|
| `POST /api/dsh-session-manager/restore` | `{ sessionId }` |
|
|
104
147
|
| `POST /api/dsh-session-manager/purge` | `{ sessionId }`,缓冲站里立即彻底删除 |
|
|
105
148
|
| `POST /api/dsh-session-manager/adopt` | `{ sessionId }`,把未分组会话按 cwd 挂回工作区 |
|
|
149
|
+
| `POST /api/dsh-session-manager/migrate` | `{ sessionId, workspaceId, mode }`,`mode` = `preview` / `full`(默认)/ `registrationOnly` |
|
|
106
150
|
|
|
107
151
|
### 「未分组」里的会话可以用「归位」挂回工作区
|
|
108
152
|
|
|
@@ -122,6 +166,10 @@ DSH 本身没有「删除单个会话」:左侧列表只能归档会话,要
|
|
|
122
166
|
## 已知限制
|
|
123
167
|
|
|
124
168
|
- 会话标题来自日志里的标题事件;从未生成过标题的会话显示「未命名」。
|
|
169
|
+
- **迁移要重启 DSH 才会在左侧列表生效**:工作区索引是启动时的快照,不感知磁盘上 cwd 变了。
|
|
170
|
+
迁移成功后插件会把它记进 `pending`,下次启动自动补登记。
|
|
171
|
+
- **迁移拒绝正在跑的会话**(日志正在被追加,这时搬文件会丢对话),也拒绝缓冲站里的会话。
|
|
172
|
+
- 迁移只搬这个会话自己的目录;工作区目录本身、以及会话里引用的其他文件都不动。
|
|
125
173
|
- 正在运行的会话删不掉:插件会先拒绝(`running`),等那一轮结束再删。
|
|
126
174
|
- 打开着但没在跑的会话是「推迟搬运」的:先归档让它从列表消失,等它退出内存后
|
|
127
175
|
由巡检补搬日志文件;`stopActivity: true` 只是停掉它的活动,并不会替你保存正在跑的回合。
|
package/docs/DETAIL.md
CHANGED
|
@@ -47,6 +47,20 @@ atomic (temp file + rename), and a corrupted file falls back to defaults instead
|
|
|
47
47
|
Each trash directory also carries a `meta.json` with the original directory and file list, so the buffer
|
|
48
48
|
can still be recovered from disk if `state.json` is lost.
|
|
49
49
|
|
|
50
|
+
## Moving a session to another workspace
|
|
51
|
+
|
|
52
|
+
Each row has a picker listing every workspace in DSH; choosing one shows an inline confirm bar, and confirming performs the move.
|
|
53
|
+
|
|
54
|
+
This is a real move. Workspace membership in DSH is derived entirely from the session header's `cwd` (`workspace.sessionIds` filters by it, and `attachSession` throws on a mismatch), and the cwd is what decides where the log file sits on disk. So the plugin does two things: it rewrites the header's cwd (recompressing only the first zstd frame and leaving every later frame byte-identical), and it relocates the session folder into the project directory for the new cwd.
|
|
55
|
+
|
|
56
|
+
The order is copy, verify, and only then delete the original: the copy has to read back with the new cwd and the same frame count, and any failure removes the half-made copy while leaving the original untouched.
|
|
57
|
+
|
|
58
|
+
**It takes effect in the sidebar after DSH restarts**, because the workspace index is a snapshot built at startup. The plugin records the move as `pending` and completes the registration on the next start.
|
|
59
|
+
|
|
60
|
+
When the system refuses a file operation, the plugin does not try to change permissions. It shows every step and both real paths, and you choose between retrying and changing the registration only. The latter leaves the cwd alone, so DSH filters the session back into No workspace on the next start — and the panel says so instead of pretending the move worked.
|
|
61
|
+
|
|
62
|
+
Locked sessions cannot be moved.
|
|
63
|
+
|
|
50
64
|
## Known limitations
|
|
51
65
|
|
|
52
66
|
- A session that is **running** cannot be deleted; the plugin refuses and asks you to stop it first.
|
package/docs/DETAIL.zh.md
CHANGED
|
@@ -10,6 +10,7 @@ DSH 本身能归档会话,但删不掉单个会话——想彻底清掉一条
|
|
|
10
10
|
## 能做什么
|
|
11
11
|
|
|
12
12
|
- **按工作区分组列出所有会话**:工作区名称、路径、会话标题、最后活动时间;正在运行的会话和日志缺失的会话都有标记。
|
|
13
|
+
- **迁移到别的工作区**:每行一个下拉框,选中目标工作区后行内确认即可。真迁移——磁盘上的日志目录一起搬,会话头里的 `cwd` 也改写。
|
|
13
14
|
- **锁定**:锁住的会话删不掉,宿主也会拒绝(不是只靠界面禁用)。再点一次解锁。
|
|
14
15
|
- **删除要二次确认**:点删除后行内展开确认条,明确写出会发生什么、多少小时内可以回档;
|
|
15
16
|
缓冲时长为 0 时文案直接告诉你无法撤销。
|
|
@@ -43,6 +44,27 @@ DSH 本身能归档会话,但删不掉单个会话——想彻底清掉一条
|
|
|
43
44
|
每个回收站目录里还有一份 `meta.json`,记着原目录和文件清单——`state.json` 万一丢了,
|
|
44
45
|
靠它也能还原。
|
|
45
46
|
|
|
47
|
+
## 迁移到别的工作区
|
|
48
|
+
|
|
49
|
+
每行有一个下拉框,列出当前 DSH 的所有工作区;选中后行内出现确认条,确认即迁。
|
|
50
|
+
|
|
51
|
+
这是**真迁移**:DSH 的工作区归属完全由会话头里的 `cwd` 推算(workspace.sessionIds
|
|
52
|
+
是个按 cwd 过滤的过滤器,ttachSession 还会在不匹配时抛错),而 cwd 又决定日志文件
|
|
53
|
+
在磁盘上的位置。所以插件做两件事:改写会话头的 cwd(只重压第 1 帧,其余帧逐字节保留),
|
|
54
|
+
并把会话目录搬到新 cwd 对应的项目目录。
|
|
55
|
+
|
|
56
|
+
顺序是**先复制、再验证、最后才删原件**:副本要能读回新的 cwd、帧数与原件一致才算成功,
|
|
57
|
+
任何一步失败都清掉半成品、原会话保持原样。
|
|
58
|
+
|
|
59
|
+
**要重启 DSH 才在左侧列表生效**,因为工作区索引是启动时的快照。插件会把这次迁移记进
|
|
60
|
+
`pending`,下次启动自动补登记。
|
|
61
|
+
|
|
62
|
+
**权限不够时**不会偷偷改权限,而是把要做的每一步和两个真实路径摆进对话框,由你选择重试
|
|
63
|
+
还是「只改变归属」。后者只改登记、不动磁盘——因为 cwd 没变,DSH 下次按 cwd 过滤时会把
|
|
64
|
+
会话摘回「未分组」,插件会如实说明这一点。
|
|
65
|
+
|
|
66
|
+
被锁定的会话不允许迁移。
|
|
67
|
+
|
|
46
68
|
## 已知限制
|
|
47
69
|
|
|
48
70
|
- **正在运行的会话删不掉**:插件会直接拒绝并让你先停掉它。
|
package/lib/client.js
CHANGED
|
@@ -63,6 +63,25 @@ window.__ModuleLoader__.load({
|
|
|
63
63
|
'action.adopt': 'File it',
|
|
64
64
|
'adopt.hint': 'Its folder belongs to {workspace}',
|
|
65
65
|
'notice.adopted': '“{title}” is filed under {workspace}.',
|
|
66
|
+
'migrate.label': 'Move to workspace',
|
|
67
|
+
'migrate.choose': 'Move to…',
|
|
68
|
+
'migrate.current': 'current',
|
|
69
|
+
'migrate.confirm': 'Move “{title}” from {from} to {to}?',
|
|
70
|
+
'migrate.confirmHint': 'Its log folder on disk moves with it. This takes effect after DSH restarts.',
|
|
71
|
+
'migrate.yes': 'Move',
|
|
72
|
+
'migrate.no': 'Cancel',
|
|
73
|
+
'notice.migrated': '“{title}” now belongs to {workspace}. Restart DSH to see it in the sidebar.',
|
|
74
|
+
'notice.migratedNoRestart': '“{title}” now belongs to {workspace}.',
|
|
75
|
+
'notice.registrationOnly': 'Only the registration changed. The log still sits under the old folder, so DSH will put “{title}” back under No workspace on the next restart.',
|
|
76
|
+
'notice.migrateNeedRestart': 'Moved on disk. Restart DSH to finish the move in the sidebar.',
|
|
77
|
+
'perm.title': 'Permission needed to move files',
|
|
78
|
+
'perm.body': 'Moving “{title}” rewrites its session header and moves its log folder. The system refused one of those operations, so nothing was changed.',
|
|
79
|
+
'perm.paths': 'From: {from}',
|
|
80
|
+
'perm.pathsTo': 'To: {to}',
|
|
81
|
+
'perm.cannotElevate': 'DSH cannot raise its own privileges. If the folders are read-only or owned by another account, fix that first — then try again.',
|
|
82
|
+
'perm.allow': 'I understand, try again',
|
|
83
|
+
'perm.registerOnly': 'Change registration only',
|
|
84
|
+
'perm.cancel': 'Cancel',
|
|
66
85
|
'notice.noWorkspaceForCwd': 'no workspace owns that folder',
|
|
67
86
|
'notice.noCwd': 'the session records no folder',
|
|
68
87
|
'notice.noHeader': 'the session log cannot be read',
|
|
@@ -109,6 +128,15 @@ window.__ModuleLoader__.load({
|
|
|
109
128
|
'reason.network': 'the Host could not be reached',
|
|
110
129
|
'reason.timeout': 'the Host did not answer in time; try again',
|
|
111
130
|
'reason.detachFailed': 'the workspace still lists it; try again',
|
|
131
|
+
'reason.noRegistry': 'this profile has no workspace registry',
|
|
132
|
+
'reason.noWorkspace': 'that workspace no longer exists',
|
|
133
|
+
'reason.alreadyThere': 'it already belongs to that workspace',
|
|
134
|
+
'reason.buffered': 'it is in the recycle bin; restore it first',
|
|
135
|
+
'reason.noHeader': 'its stored header could not be read',
|
|
136
|
+
'reason.noCwd': 'its header records no working directory, so it cannot be moved',
|
|
137
|
+
'reason.migrateFailed': 'the move did not finish',
|
|
138
|
+
'reason.needPermission': 'the system refused the file operation',
|
|
139
|
+
'reason.noDirectory': 'the session folder on disk is missing',
|
|
112
140
|
'time.minutes': '{n}m',
|
|
113
141
|
'time.hours': '{n}h',
|
|
114
142
|
'time.days': '{n}d',
|
|
@@ -151,6 +179,25 @@ window.__ModuleLoader__.load({
|
|
|
151
179
|
'action.adopt': '归位',
|
|
152
180
|
'adopt.hint': '它的目录属于 {workspace}',
|
|
153
181
|
'notice.adopted': '「{title}」已归位到 {workspace}。',
|
|
182
|
+
'migrate.label': '迁移到工作区',
|
|
183
|
+
'migrate.choose': '迁到…',
|
|
184
|
+
'migrate.current': '当前',
|
|
185
|
+
'migrate.confirm': '把「{title}」从 {from} 迁到 {to}?',
|
|
186
|
+
'migrate.confirmHint': '磁盘上的日志目录会一起搬过去。需要重启 DSH 才会生效。',
|
|
187
|
+
'migrate.yes': '迁移',
|
|
188
|
+
'migrate.no': '取消',
|
|
189
|
+
'notice.migrated': '「{title}」现在属于 {workspace}。重启 DSH 后左侧列表会跟着变。',
|
|
190
|
+
'notice.migratedNoRestart': '「{title}」现在属于 {workspace}。',
|
|
191
|
+
'notice.registrationOnly': '只改了归属。日志还在旧目录里,所以 DSH 下次重启后会把它放回「未分组」。',
|
|
192
|
+
'notice.migrateNeedRestart': '磁盘已搬完,重启 DSH 后左侧列表才会更新。',
|
|
193
|
+
'perm.title': '需要权限才能搬文件',
|
|
194
|
+
'perm.body': '迁移「{title}」要改写会话头、并把日志目录搬到新位置。系统拒绝了其中一步,所以什么都没改。',
|
|
195
|
+
'perm.paths': '从:{from}',
|
|
196
|
+
'perm.pathsTo': '到:{to}',
|
|
197
|
+
'perm.cannotElevate': 'DSH 没法给自己提权。如果那两个目录是只读的、或者属于别的账户,请先解决,再试一次。',
|
|
198
|
+
'perm.allow': '我知道了,再试一次',
|
|
199
|
+
'perm.registerOnly': '只改变归属',
|
|
200
|
+
'perm.cancel': '取消',
|
|
154
201
|
'notice.noWorkspaceForCwd': '没有工作区认领它的目录',
|
|
155
202
|
'notice.noCwd': '这个会话没有记录目录',
|
|
156
203
|
'notice.noHeader': '读不到这个会话的日志',
|
|
@@ -197,6 +244,15 @@ window.__ModuleLoader__.load({
|
|
|
197
244
|
'reason.network': '连不上宿主',
|
|
198
245
|
'reason.timeout': '宿主迟迟没有响应,请重试',
|
|
199
246
|
'reason.detachFailed': '工作区里还挂着它,请重试',
|
|
247
|
+
'reason.noRegistry': '这个配置没有挂工作区登记表',
|
|
248
|
+
'reason.noWorkspace': '那个工作区已经不在了',
|
|
249
|
+
'reason.alreadyThere': '它已经属于那个工作区了',
|
|
250
|
+
'reason.buffered': '它在删除缓冲站里,先回档再迁移',
|
|
251
|
+
'reason.noHeader': '读不到它的会话头',
|
|
252
|
+
'reason.noCwd': '会话头里没有工作目录,没法迁移',
|
|
253
|
+
'reason.migrateFailed': '迁移没有完成',
|
|
254
|
+
'reason.needPermission': '系统拒绝了文件操作',
|
|
255
|
+
'reason.noDirectory': '磁盘上的会话目录不见了',
|
|
200
256
|
'error.prefix': '请求失败:',
|
|
201
257
|
'time.minutes': '{n}分',
|
|
202
258
|
'time.hours': '{n}小时',
|
|
@@ -443,6 +499,41 @@ window.__ModuleLoader__.load({
|
|
|
443
499
|
gap: 5,
|
|
444
500
|
},
|
|
445
501
|
buttonDisabled: { opacity: 0.45, cursor: 'not-allowed' },
|
|
502
|
+
// 迁移下拉:和 ghost 按钮同高,跟着主题走,不自绘箭头。
|
|
503
|
+
select: {
|
|
504
|
+
height: 30,
|
|
505
|
+
maxWidth: 150,
|
|
506
|
+
padding: '0 8px',
|
|
507
|
+
fontSize: 12.5,
|
|
508
|
+
boxSizing: 'border-box',
|
|
509
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
510
|
+
background: 'var(--dsw-alias-bg-layer-2)',
|
|
511
|
+
border: '1px solid var(--dsw-alias-border-l2)',
|
|
512
|
+
borderRadius: 'var(--dsw-radius-md)',
|
|
513
|
+
cursor: 'pointer',
|
|
514
|
+
},
|
|
515
|
+
dialog: {
|
|
516
|
+
padding: '14px 16px 16px',
|
|
517
|
+
borderRadius: 'var(--dsw-radius-xl)',
|
|
518
|
+
border: '1px solid var(--dsw-alias-state-warn-primary)',
|
|
519
|
+
background: 'var(--dsw-alias-settings-card-fill)',
|
|
520
|
+
},
|
|
521
|
+
dialogTitle: { margin: '0 0 6px', fontSize: 13, fontWeight: 600, color: 'var(--dsw-alias-state-warn-primary)' },
|
|
522
|
+
dialogBody: { margin: '0 0 8px', fontSize: 12.5, lineHeight: 1.6 },
|
|
523
|
+
dialogHint: { margin: '0 0 10px', fontSize: 11.5, lineHeight: 1.55, color: 'var(--dsw-alias-label-secondary)' },
|
|
524
|
+
pathBox: {
|
|
525
|
+
margin: '0 0 10px',
|
|
526
|
+
padding: '8px 10px',
|
|
527
|
+
fontSize: 11.5,
|
|
528
|
+
fontFamily: mono,
|
|
529
|
+
lineHeight: 1.6,
|
|
530
|
+
overflowX: 'auto',
|
|
531
|
+
whiteSpace: 'pre',
|
|
532
|
+
color: 'var(--dsw-alias-label-primary)',
|
|
533
|
+
background: 'var(--dsw-alias-bg-layer-2)',
|
|
534
|
+
border: '1px solid var(--dsw-alias-border-l1)',
|
|
535
|
+
borderRadius: 'var(--dsw-radius-md)',
|
|
536
|
+
},
|
|
446
537
|
group: { marginTop: 12 },
|
|
447
538
|
groupHead: {
|
|
448
539
|
display: 'flex',
|
|
@@ -558,6 +649,10 @@ window.__ModuleLoader__.load({
|
|
|
558
649
|
const [busy, setBusy] = useState(null)
|
|
559
650
|
const [notice, setNotice] = useState(null)
|
|
560
651
|
const [confirming, setConfirming] = useState(null)
|
|
652
|
+
// 迁移确认:{ sessionId, workspaceId, title },选定目标工作区后出现。
|
|
653
|
+
const [migrateConfirm, setMigrateConfirm] = useState(null)
|
|
654
|
+
// 权限申请:{ sessionId, workspaceId, title, plan },宿主报 needPermission 时出现。
|
|
655
|
+
const [migrateAsk, setMigrateAsk] = useState(null)
|
|
561
656
|
const [reloading, setReloading] = useState(false)
|
|
562
657
|
const [now, setNow] = useState(() => Date.now())
|
|
563
658
|
const aliveRef = useRef(true)
|
|
@@ -611,7 +706,7 @@ window.__ModuleLoader__.load({
|
|
|
611
706
|
}, [dueSignature, refresh])
|
|
612
707
|
|
|
613
708
|
const run = useCallback(
|
|
614
|
-
async (key, action, payload, onDone) => {
|
|
709
|
+
async (key, action, payload, onDone, onFail) => {
|
|
615
710
|
if (busy !== null) return
|
|
616
711
|
setBusy(key)
|
|
617
712
|
setNotice(null)
|
|
@@ -619,6 +714,9 @@ window.__ModuleLoader__.load({
|
|
|
619
714
|
if (!aliveRef.current) return
|
|
620
715
|
setBusy(null)
|
|
621
716
|
if (result.ok !== true) {
|
|
717
|
+
// 有些失败不是错误,而是需要用户先做一个决定(比如「要不要为此申请权限」)。
|
|
718
|
+
// onFail 返回 true 表示它已经接管了这条结果。
|
|
719
|
+
if (onFail !== undefined && onFail(result) === true) return
|
|
622
720
|
setNotice({ tone: 'error', text: describeError(t, result) })
|
|
623
721
|
return
|
|
624
722
|
}
|
|
@@ -641,6 +739,61 @@ window.__ModuleLoader__.load({
|
|
|
641
739
|
),
|
|
642
740
|
}, notice.text)
|
|
643
741
|
|
|
742
|
+
// 权限申请框:只有真去搬文件、而系统拒绝了那一步时才出现。
|
|
743
|
+
// 这里说的「申请权限」不是提权(DSH 提不了权),而是把要做的每一步
|
|
744
|
+
// 和真实路径摆清楚,让用户明白自己在同意什么,然后再试一次。
|
|
745
|
+
const permissionNode = migrateAsk === null
|
|
746
|
+
? null
|
|
747
|
+
: h('div', { style: style.dialog },
|
|
748
|
+
h('h3', { style: style.dialogTitle }, t('perm.title')),
|
|
749
|
+
h('p', { style: style.dialogBody }, t('perm.body', { title: migrateAsk.title })),
|
|
750
|
+
h('div', { style: style.pathBox },
|
|
751
|
+
t('perm.paths', { from: migrateAsk.plan?.fromDir ?? migrateAsk.plan?.fromCwd ?? '?' }),
|
|
752
|
+
'\n',
|
|
753
|
+
t('perm.pathsTo', { to: migrateAsk.plan?.toDir ?? migrateAsk.plan?.toCwd ?? '?' }),
|
|
754
|
+
),
|
|
755
|
+
h('p', { style: style.dialogHint }, t('perm.cannotElevate')),
|
|
756
|
+
h('div', { style: style.row },
|
|
757
|
+
h('button', {
|
|
758
|
+
type: 'button',
|
|
759
|
+
style: Object.assign({}, style.buttonPrimary, busy !== null ? style.buttonDisabled : null),
|
|
760
|
+
disabled: busy !== null,
|
|
761
|
+
onClick: () => {
|
|
762
|
+
const ask = migrateAsk
|
|
763
|
+
setMigrateAsk(null)
|
|
764
|
+
void run(`migrate:${ask.sessionId}`, 'migrate', {
|
|
765
|
+
sessionId: ask.sessionId,
|
|
766
|
+
workspaceId: ask.workspaceId,
|
|
767
|
+
mode: 'full',
|
|
768
|
+
}, (result) => {
|
|
769
|
+
const to = (view.workspaces.find((item) => item.workspaceId === ask.workspaceId) ?? {}).title ?? ''
|
|
770
|
+
if (result.mode === 'full') setNotice({ tone: 'ok', text: t('notice.migrated', { title: ask.title, workspace: to }) })
|
|
771
|
+
else setNotice({ tone: 'ok', text: t('notice.registrationOnly', { title: ask.title }) })
|
|
772
|
+
}, (failure) => {
|
|
773
|
+
if (failure.code !== 'needPermission') return false
|
|
774
|
+
setMigrateAsk({ ...ask, plan: failure.plan ?? ask.plan, detail: failure.detail ?? '' })
|
|
775
|
+
return true
|
|
776
|
+
})
|
|
777
|
+
},
|
|
778
|
+
}, t('perm.allow')),
|
|
779
|
+
h('button', {
|
|
780
|
+
type: 'button',
|
|
781
|
+
style: Object.assign({}, style.buttonGhost, busy !== null ? style.buttonDisabled : null),
|
|
782
|
+
disabled: busy !== null,
|
|
783
|
+
onClick: () => {
|
|
784
|
+
const ask = migrateAsk
|
|
785
|
+
setMigrateAsk(null)
|
|
786
|
+
void run(`migrate:${ask.sessionId}`, 'migrate', {
|
|
787
|
+
sessionId: ask.sessionId,
|
|
788
|
+
workspaceId: ask.workspaceId,
|
|
789
|
+
mode: 'registrationOnly',
|
|
790
|
+
}, () => setNotice({ tone: 'ok', text: t('notice.registrationOnly', { title: ask.title }) }))
|
|
791
|
+
},
|
|
792
|
+
}, t('perm.registerOnly')),
|
|
793
|
+
h('button', { type: 'button', style: style.buttonGhost, onClick: () => setMigrateAsk(null) }, t('perm.cancel')),
|
|
794
|
+
),
|
|
795
|
+
)
|
|
796
|
+
|
|
644
797
|
if (view === null) {
|
|
645
798
|
// 首次读取失败必须能看见也能重试,否则只会永远停在「正在读取会话…」。
|
|
646
799
|
return h('div', { style: style.page },
|
|
@@ -716,6 +869,7 @@ window.__ModuleLoader__.load({
|
|
|
716
869
|
|
|
717
870
|
// 提示放在页头下面:动作可能来自列表中间,提示得在看得见的地方。
|
|
718
871
|
noticeNode,
|
|
872
|
+
permissionNode,
|
|
719
873
|
|
|
720
874
|
// 缓冲时长
|
|
721
875
|
h('div', { style: style.card },
|
|
@@ -799,7 +953,8 @@ window.__ModuleLoader__.load({
|
|
|
799
953
|
const title = session.title.trim() === '' ? t('sessions.unnamed') : session.title
|
|
800
954
|
const locked = session.locked
|
|
801
955
|
const isConfirming = confirming === session.sessionId
|
|
802
|
-
const
|
|
956
|
+
const isMigrating = migrateConfirm !== null && migrateConfirm.sessionId === session.sessionId
|
|
957
|
+
const rowStyle = isConfirming || isMigrating
|
|
803
958
|
? Object.assign({}, style.sessionRow, style.sessionRowConfirm)
|
|
804
959
|
: style.sessionRow
|
|
805
960
|
return h('div', { key: session.sessionId, style: rowStyle },
|
|
@@ -823,6 +978,34 @@ window.__ModuleLoader__.load({
|
|
|
823
978
|
),
|
|
824
979
|
),
|
|
825
980
|
h('div', { style: style.sessionActions },
|
|
981
|
+
// 迁移目标下拉:列出当前 DSH 的所有工作区,选中一个就弹确认条。
|
|
982
|
+
// 锁定的会话不允许迁移,这里直接禁用并说明原因。
|
|
983
|
+
h('select', {
|
|
984
|
+
style: Object.assign({}, style.select, locked || busy !== null ? style.buttonDisabled : null),
|
|
985
|
+
value: '',
|
|
986
|
+
disabled: locked || busy !== null,
|
|
987
|
+
title: locked ? t('sessions.locked') : t('migrate.label'),
|
|
988
|
+
'aria-label': t('migrate.label'),
|
|
989
|
+
onChange: (event) => {
|
|
990
|
+
const workspaceId = event.target.value
|
|
991
|
+
if (workspaceId === '') return
|
|
992
|
+
setConfirming(null)
|
|
993
|
+
setMigrateConfirm({ sessionId: session.sessionId, workspaceId, title })
|
|
994
|
+
// 让下拉回到占位项,否则选过一次就再也选不动同一个工作区。
|
|
995
|
+
event.target.value = ''
|
|
996
|
+
},
|
|
997
|
+
},
|
|
998
|
+
h('option', { value: '' }, t('migrate.choose')),
|
|
999
|
+
view.workspaces.map((workspace) =>
|
|
1000
|
+
h('option', {
|
|
1001
|
+
key: workspace.workspaceId,
|
|
1002
|
+
value: workspace.workspaceId,
|
|
1003
|
+
disabled: workspace.workspaceId === session.workspaceId,
|
|
1004
|
+
},
|
|
1005
|
+
`${workspace.title}${workspace.workspaceId === session.workspaceId ? ` (${t('migrate.current')})` : ''}`,
|
|
1006
|
+
),
|
|
1007
|
+
),
|
|
1008
|
+
),
|
|
826
1009
|
session.adoptIntoTitle
|
|
827
1010
|
? h('button', {
|
|
828
1011
|
type: 'button',
|
|
@@ -891,6 +1074,52 @@ window.__ModuleLoader__.load({
|
|
|
891
1074
|
}, t('confirm.no')),
|
|
892
1075
|
)
|
|
893
1076
|
: null,
|
|
1077
|
+
isMigrating
|
|
1078
|
+
? h('div', { style: Object.assign({}, style.confirmBar, { gridColumn: '1 / -1' }) },
|
|
1079
|
+
h('span', { style: style.confirmText },
|
|
1080
|
+
t('migrate.confirm', {
|
|
1081
|
+
title,
|
|
1082
|
+
from: group.title,
|
|
1083
|
+
to: (view.workspaces.find((item) => item.workspaceId === migrateConfirm.workspaceId) ?? {}).title ?? '?',
|
|
1084
|
+
}),
|
|
1085
|
+
' ',
|
|
1086
|
+
h('span', { style: { color: 'var(--dsw-alias-label-secondary)' } }, t('migrate.confirmHint')),
|
|
1087
|
+
),
|
|
1088
|
+
h('button', {
|
|
1089
|
+
type: 'button',
|
|
1090
|
+
style: Object.assign({}, style.buttonPrimary, busy !== null ? style.buttonDisabled : null),
|
|
1091
|
+
disabled: busy !== null,
|
|
1092
|
+
onClick: () => {
|
|
1093
|
+
const target = migrateConfirm
|
|
1094
|
+
setMigrateConfirm(null)
|
|
1095
|
+
void run(`migrate:${session.sessionId}`, 'migrate', {
|
|
1096
|
+
sessionId: session.sessionId,
|
|
1097
|
+
workspaceId: target.workspaceId,
|
|
1098
|
+
mode: 'full',
|
|
1099
|
+
}, (result) => {
|
|
1100
|
+
const to = (view.workspaces.find((item) => item.workspaceId === target.workspaceId) ?? {}).title ?? ''
|
|
1101
|
+
if (result.mode === 'full') {
|
|
1102
|
+
setNotice({ tone: 'ok', text: t('notice.migrated', { title, workspace: to }) })
|
|
1103
|
+
} else {
|
|
1104
|
+
setNotice({ tone: 'ok', text: t('notice.registrationOnly', { title }) })
|
|
1105
|
+
}
|
|
1106
|
+
}, (failure) => {
|
|
1107
|
+
// 权限不够:不是报错,而是先问用户。把将要做的每一步
|
|
1108
|
+
// 和真实的两个路径摆在对话框里,让他知道自己在同意什么。
|
|
1109
|
+
if (failure.code !== 'needPermission') return false
|
|
1110
|
+
setMigrateAsk({ ...target, plan: failure.plan ?? {}, detail: failure.detail ?? '' })
|
|
1111
|
+
return true
|
|
1112
|
+
})
|
|
1113
|
+
},
|
|
1114
|
+
}, t('migrate.yes')),
|
|
1115
|
+
h('button', {
|
|
1116
|
+
type: 'button',
|
|
1117
|
+
style: style.buttonGhost,
|
|
1118
|
+
disabled: busy !== null,
|
|
1119
|
+
onClick: () => setMigrateConfirm(null),
|
|
1120
|
+
}, t('migrate.no')),
|
|
1121
|
+
)
|
|
1122
|
+
: null,
|
|
894
1123
|
)
|
|
895
1124
|
}),
|
|
896
1125
|
),
|
package/lib/index.js
CHANGED
|
@@ -36,6 +36,7 @@ import {
|
|
|
36
36
|
writeState,
|
|
37
37
|
} from './state.js'
|
|
38
38
|
import { moveIntoTrash, purgeTrash, restoreFromTrash } from './trash.js'
|
|
39
|
+
import { isPermissionError, migrateOnDisk, sessionDirPath } from './migrate.js'
|
|
39
40
|
|
|
40
41
|
export const ROUTE_PREFIX = '/api/dsh-session-manager'
|
|
41
42
|
|
|
@@ -622,6 +623,188 @@ export function apply(ctx, config = {}) {
|
|
|
622
623
|
}
|
|
623
624
|
|
|
624
625
|
// 路由统一用错误码回话,界面按当前语言翻译;只有真正意外的异常才带原始消息。
|
|
626
|
+
/**
|
|
627
|
+
* 把会话迁到另一个工作区。
|
|
628
|
+
*
|
|
629
|
+
* 这里必须说清楚 DSH 的工作区归属是怎么算的,否则会以为「改登记」就够了:
|
|
630
|
+
* 归属**完全由会话头里的 cwd 决定**,`workspace.sessionIds` 是个过滤器,
|
|
631
|
+
* 只留下 cwd 的 realpath 等于工作区 path 的会话;`attachSession` 在写入前
|
|
632
|
+
* 也会显式校验并在不匹配时抛错。所以想让它真的出现在另一个工作区里,
|
|
633
|
+
* 只能改会话头 —— 而改写会话头,就意味着日志文件要换个项目目录。
|
|
634
|
+
*
|
|
635
|
+
* 三种结果:
|
|
636
|
+
* ok 磁盘真的搬完、头也改好了(重启 DSH 后左侧列表跟着变)
|
|
637
|
+
* needPermission 文件操作被系统拒绝,把将要做的每一步和真实路径回给界面,
|
|
638
|
+
* 让用户知道自己在同意什么;同意就带 mode:'full' 再来一次
|
|
639
|
+
* registrationOnly 用户拒绝了权限申请,只改登记。**必须如实告诉他这不生效**:
|
|
640
|
+
* DSH 下一次按 cwd 过滤时会把它摘掉,重启后它会回到「未分组」。
|
|
641
|
+
*
|
|
642
|
+
* 被锁定的会话一律不允许迁移 —— 锁的意思是「不许动」,不是「不许删」。
|
|
643
|
+
*/
|
|
644
|
+
async function migrateSession(sessionId, targetWorkspaceId, mode) {
|
|
645
|
+
const state = readState(home)
|
|
646
|
+
if (state.locks.includes(sessionId)) return { ok: false, code: 'locked' }
|
|
647
|
+
if (state.buffer.some((entry) => entry.sessionId === sessionId)) return { ok: false, code: 'buffered' }
|
|
648
|
+
|
|
649
|
+
const registry = ctx.get('workspaceRegistry')
|
|
650
|
+
if (registry === undefined) return { ok: false, code: 'noRegistry' }
|
|
651
|
+
const target = (registry.list?.() ?? []).find((workspace) => workspace.id === targetWorkspaceId)
|
|
652
|
+
if (target === undefined) return { ok: false, code: 'noWorkspace' }
|
|
653
|
+
|
|
654
|
+
const header = await readSessionHeader(sessionId)
|
|
655
|
+
if (header === undefined) return { ok: false, code: 'noHeader' }
|
|
656
|
+
const fromCwd = typeof header.cwd === 'string' ? header.cwd : ''
|
|
657
|
+
if (fromCwd === '') return { ok: false, code: 'noCwd' }
|
|
658
|
+
|
|
659
|
+
// 找它现在算哪个工作区:按 cwd 判断,和 DSH 的过滤器保持同一个口径。
|
|
660
|
+
const current = (registry.list?.() ?? []).find(
|
|
661
|
+
(workspace) => typeof workspace.path === 'string' && workspace.path === fromCwd,
|
|
662
|
+
)
|
|
663
|
+
if (current !== undefined && current.id === target.id) return { ok: false, code: 'alreadyThere' }
|
|
664
|
+
|
|
665
|
+
// 跑着回合的会话日志正在被追加,这时候搬文件会丢对话。
|
|
666
|
+
if (liveBlockers(sessionId).length > 0) return { ok: false, code: 'liveBlocked' }
|
|
667
|
+
|
|
668
|
+
const located = await locateSessionLog(sessionId)
|
|
669
|
+
const logFile = located.file
|
|
670
|
+
const fromDir = typeof logFile === 'string' ? path.dirname(logFile) : ''
|
|
671
|
+
const sessionsRoot = fromDir === '' ? '' : path.dirname(path.dirname(fromDir))
|
|
672
|
+
const plan = {
|
|
673
|
+
sessionId,
|
|
674
|
+
fromWorkspaceId: current?.id ?? '',
|
|
675
|
+
fromWorkspaceTitle: current?.title ?? '',
|
|
676
|
+
toWorkspaceId: target.id,
|
|
677
|
+
toWorkspaceTitle: target.title,
|
|
678
|
+
fromCwd,
|
|
679
|
+
toCwd: target.path,
|
|
680
|
+
fromDir,
|
|
681
|
+
toDir: sessionsRoot === '' ? '' : sessionDirPath(sessionsRoot, target.path, sessionId),
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
const detachFromSource = async () => {
|
|
685
|
+
if (current === undefined) return
|
|
686
|
+
try {
|
|
687
|
+
await current.detachSession(sessionId)
|
|
688
|
+
} catch (error) {
|
|
689
|
+
logger?.warn?.(`dsh-session-manager: 迁移时解除旧归属失败 ${messageOf(error)}`)
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// 预览:把真实路径算出来交给确认框,一个字节都不写。
|
|
694
|
+
if (mode === 'preview') return { ok: true, mode: 'preview', plan }
|
|
695
|
+
|
|
696
|
+
/** 只改登记:不碰磁盘。用户在权限申请里点了「不」。 */
|
|
697
|
+
const registrationOnly = async () => {
|
|
698
|
+
await detachFromSource()
|
|
699
|
+
try {
|
|
700
|
+
await target.attachSession(sessionId)
|
|
701
|
+
} catch (error) {
|
|
702
|
+
// 这条路本来就注定失败:cwd 没变,attachSession 会拒绝。
|
|
703
|
+
// 把它记下来,重启后按 cwd 重新对账时不会再错。
|
|
704
|
+
logger?.warn?.(`dsh-session-manager: 仅改归属未能登记(cwd 未变,DSH 会拒绝)${messageOf(error)}`)
|
|
705
|
+
}
|
|
706
|
+
return { ok: true, mode: 'registrationOnly', restartRequired: true, plan }
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
if (mode === 'registrationOnly') return await registrationOnly()
|
|
710
|
+
|
|
711
|
+
if (sessionsRoot === '' || fromDir === '') {
|
|
712
|
+
// 拿不到磁盘位置就没法真迁移,只能退到改登记,并如实说明。
|
|
713
|
+
logger?.warn?.(`dsh-session-manager: 找不到 ${sessionId} 的日志目录,迁移退化为改归属`)
|
|
714
|
+
return { ...(await registrationOnly()), code: 'noLocation' }
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
try {
|
|
718
|
+
// 搬之前先把内存里的事件刷进日志,否则搬过去就丢了这一段。
|
|
719
|
+
await flushSession(sessionId)
|
|
720
|
+
const moved = await migrateOnDisk({
|
|
721
|
+
sessionsRoot,
|
|
722
|
+
sessionId,
|
|
723
|
+
fromCwd,
|
|
724
|
+
toCwd: target.path,
|
|
725
|
+
})
|
|
726
|
+
// 磁盘已经搬完了,这一步之后登记失败也不能说迁移失败——文件在那边,
|
|
727
|
+
// 只是这个进程里的索引还是旧的。
|
|
728
|
+
let registered = true
|
|
729
|
+
await detachFromSource()
|
|
730
|
+
try {
|
|
731
|
+
await target.attachSession(sessionId)
|
|
732
|
+
} catch (error) {
|
|
733
|
+
registered = false
|
|
734
|
+
logger?.warn?.(`dsh-session-manager: 迁移已落盘,但本进程内登记要等重启 ${messageOf(error)}`)
|
|
735
|
+
}
|
|
736
|
+
if (!registered) {
|
|
737
|
+
// 记下来,重启后补登记。不记的话会话会掉进「未分组」再也回不来。
|
|
738
|
+
const now = readState(home)
|
|
739
|
+
const already = now.pending.some((entry) => entry.sessionId === sessionId)
|
|
740
|
+
writeState(home, {
|
|
741
|
+
...now,
|
|
742
|
+
pending: already
|
|
743
|
+
? now.pending.map((entry) =>
|
|
744
|
+
entry.sessionId === sessionId ? { ...entry, workspaceId: target.id, at: Date.now() } : entry,
|
|
745
|
+
)
|
|
746
|
+
: [
|
|
747
|
+
...now.pending,
|
|
748
|
+
{
|
|
749
|
+
sessionId,
|
|
750
|
+
workspaceId: target.id,
|
|
751
|
+
fromWorkspaceId: current?.id ?? '',
|
|
752
|
+
at: Date.now(),
|
|
753
|
+
},
|
|
754
|
+
],
|
|
755
|
+
})
|
|
756
|
+
}
|
|
757
|
+
return {
|
|
758
|
+
ok: true,
|
|
759
|
+
mode: 'full',
|
|
760
|
+
// 会话头的 cwd 变了,但工作区的内存索引还是启动时那份,重启才会重读。
|
|
761
|
+
restartRequired: true,
|
|
762
|
+
registered,
|
|
763
|
+
plan: { ...plan, toDir: moved.to, files: moved.files, bytes: moved.bytes },
|
|
764
|
+
}
|
|
765
|
+
} catch (error) {
|
|
766
|
+
if (isPermissionError(error)) {
|
|
767
|
+
return { ok: false, code: 'needPermission', detail: messageOf(error), plan }
|
|
768
|
+
}
|
|
769
|
+
return { ok: false, code: 'migrateFailed', detail: messageOf(error), plan }
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
|
|
773
|
+
/**
|
|
774
|
+
* 把上次「已搬完磁盘、但没来得及登记」的迁移补上。
|
|
775
|
+
*
|
|
776
|
+
* 只在启动后跑一次:那时候工作区索引是按磁盘上的新 cwd 重建的,
|
|
777
|
+
* `attachSession` 才会通过它的 cwd 校验。跑通了就从队列里删掉。
|
|
778
|
+
*/
|
|
779
|
+
async function replayPendingMigrations() {
|
|
780
|
+
const initial = readState(home)
|
|
781
|
+
if (initial.pending.length === 0) return
|
|
782
|
+
const registry = ctx.get('workspaceRegistry')
|
|
783
|
+
if (registry === undefined) return
|
|
784
|
+
for (const item of initial.pending) {
|
|
785
|
+
try {
|
|
786
|
+
const workspace = (registry.list?.() ?? []).find((candidate) => candidate.id === item.workspaceId)
|
|
787
|
+
if (workspace === undefined) {
|
|
788
|
+
// 目标工作区被删了,这条迁移没有落点,直接放弃。
|
|
789
|
+
writeState(home, {
|
|
790
|
+
...readState(home),
|
|
791
|
+
pending: readState(home).pending.filter((entry) => entry.sessionId !== item.sessionId),
|
|
792
|
+
})
|
|
793
|
+
continue
|
|
794
|
+
}
|
|
795
|
+
await workspace.attachSession(item.sessionId)
|
|
796
|
+
const now = readState(home)
|
|
797
|
+
writeState(home, { ...now, pending: now.pending.filter((entry) => entry.sessionId !== item.sessionId) })
|
|
798
|
+
logger?.info?.(`dsh-session-manager: 补登记迁移会话 ${item.sessionId} -> ${workspace.title}`)
|
|
799
|
+
} catch (error) {
|
|
800
|
+
// 还是登记不上就留着,下次启动再试;不能把记录删了,否则会话就永远掉队了。
|
|
801
|
+
logger?.warn?.(`dsh-session-manager: 补登记 ${item.sessionId} 失败 ${messageOf(error)}`)
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// ------------------------------------------------------------------ 路由
|
|
807
|
+
|
|
625
808
|
const routes = [
|
|
626
809
|
{
|
|
627
810
|
path: `${ROUTE_PREFIX}/state`,
|
|
@@ -878,6 +1061,35 @@ export function apply(ctx, config = {}) {
|
|
|
878
1061
|
}
|
|
879
1062
|
},
|
|
880
1063
|
},
|
|
1064
|
+
{
|
|
1065
|
+
/**
|
|
1066
|
+
* 迁移:把会话挪到另一个工作区。
|
|
1067
|
+
*
|
|
1068
|
+
* `mode: 'preview'` 只算一遍、不写任何东西,用来在确认框里显示真实的
|
|
1069
|
+
* 源目录/目标目录;`mode: 'full'`(默认)真搬;`mode: 'registrationOnly'`
|
|
1070
|
+
* 是用户在权限申请里点了「不」之后的降级,只改登记。
|
|
1071
|
+
*/
|
|
1072
|
+
path: `${ROUTE_PREFIX}/migrate`,
|
|
1073
|
+
handler: async (request, response) => {
|
|
1074
|
+
if (!isLoopbackRequest(request)) return writeJson(response, 403, { ok: false, code: 'notLoopback' })
|
|
1075
|
+
const body = await readJsonBody(request)
|
|
1076
|
+
if (body === undefined) return writeJson(response, 400, { ok: false, code: 'badBody' })
|
|
1077
|
+
const sessionId = typeof body.sessionId === 'string' ? body.sessionId : ''
|
|
1078
|
+
const workspaceId = typeof body.workspaceId === 'string' ? body.workspaceId : ''
|
|
1079
|
+
if (sessionId === '' || workspaceId === '') return writeJson(response, 400, { ok: false, code: 'badBody' })
|
|
1080
|
+
const mode = body.mode === 'preview' ? 'preview' : body.mode === 'registrationOnly' ? 'registrationOnly' : 'full'
|
|
1081
|
+
try {
|
|
1082
|
+
const outcome = await serialize(async () => {
|
|
1083
|
+
if (mode === 'preview') return await migrateSession(sessionId, workspaceId, 'preview')
|
|
1084
|
+
return await migrateSession(sessionId, workspaceId, mode)
|
|
1085
|
+
})
|
|
1086
|
+
if (outcome.ok === false) return writeJson(response, 200, outcome)
|
|
1087
|
+
writeJson(response, 200, { ...outcome, view: await readViewSafely() })
|
|
1088
|
+
} catch (error) {
|
|
1089
|
+
writeJson(response, 500, { ok: false, code: 'migrateFailed', error: messageOf(error) })
|
|
1090
|
+
}
|
|
1091
|
+
},
|
|
1092
|
+
},
|
|
881
1093
|
]
|
|
882
1094
|
|
|
883
1095
|
ctx.effect(() => {
|
|
@@ -930,6 +1142,16 @@ export function apply(ctx, config = {}) {
|
|
|
930
1142
|
}
|
|
931
1143
|
}, 'dsh-session-manager: recycle sweeper')
|
|
932
1144
|
|
|
1145
|
+
// 启动后补登记上次没来得及登记的迁移。等工作区索引重建完再来,
|
|
1146
|
+
// 否则 attachSession 会因为读到旧的 cwd 而拒绝。
|
|
1147
|
+
const replayTimer = setTimeout(() => {
|
|
1148
|
+
void replayPendingMigrations().catch((error) =>
|
|
1149
|
+
logger?.warn?.(`dsh-session-manager: 补登记迁移失败 ${messageOf(error)}`),
|
|
1150
|
+
)
|
|
1151
|
+
}, 3_000)
|
|
1152
|
+
replayTimer.unref?.()
|
|
1153
|
+
ctx.effect(() => () => clearTimeout(replayTimer), 'dsh-session-manager: migrate replay')
|
|
1154
|
+
|
|
933
1155
|
logger?.info?.(`dsh-session-manager: 设置路由挂在 ${ROUTE_PREFIX},状态文件 ${stateFile(home)}`)
|
|
934
1156
|
}
|
|
935
1157
|
|
package/lib/migrate.js
ADDED
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// 把一个会话从它所在的工作区迁移到另一个工作区。
|
|
2
|
+
//
|
|
3
|
+
// 为什么这件事不只是「改一下登记」:DSH 的工作区归属**完全由会话头里的 cwd 决定**。
|
|
4
|
+
// `@deepseek-ai/dsh-workspace` 的 `sessionIds` 是个过滤器:
|
|
5
|
+
//
|
|
6
|
+
// get sessionIds() {
|
|
7
|
+
// return this.record.sessionIds.filter((id) => this.host.candidateMatches(id, this.record.path))
|
|
8
|
+
// }
|
|
9
|
+
//
|
|
10
|
+
// `candidateMatches` 只认「会话头 cwd 的 realpath == 工作区 path」,而 `attachSession`
|
|
11
|
+
// 在写入前就显式校验并在不匹配时抛错。所以想让一个会话真的出现在另一个工作区里,
|
|
12
|
+
// 必须改会话头的 cwd —— 没有第二条路。
|
|
13
|
+
//
|
|
14
|
+
// 而会话头的 cwd 不是随便放的字段,它决定了日志文件在磁盘上的位置:
|
|
15
|
+
//
|
|
16
|
+
// $DSH_HOME/sessions/<projectKey(cwd)>/<encodeSegment(id)>/session.v4.jsonl.zstd
|
|
17
|
+
//
|
|
18
|
+
// 于是「真迁移」必然包含两件磁盘操作:改头 + 移目录。这个文件负责这两件事,
|
|
19
|
+
// 并且保证中间任何一步失败都不会让原会话变得更糟:先复制、复制品验证通过之后才删原件。
|
|
20
|
+
//
|
|
21
|
+
// 日志是**多帧 zstd**,每帧独立压缩(DSH 的读路径按魔数切帧、逐帧解压)。
|
|
22
|
+
// 会话头在第 1 帧里,所以只需解压第 1 帧、改掉 cwd、重新压回去,后面的帧原样保留 ——
|
|
23
|
+
// 文件其余部分一个字节都不动,比整份重压安全得多。
|
|
24
|
+
|
|
25
|
+
import { existsSync, mkdirSync } from 'node:fs'
|
|
26
|
+
import { copyFile, readFile, readdir, rm, stat, writeFile } from 'node:fs/promises'
|
|
27
|
+
import path from 'node:path'
|
|
28
|
+
import zlib from 'node:zlib'
|
|
29
|
+
|
|
30
|
+
/** zstd 帧魔数。DSH 的读路径也是靠它切帧的。 */
|
|
31
|
+
const ZSTD_MAGIC = Buffer.from([0x28, 0xb5, 0x2f, 0xfd])
|
|
32
|
+
|
|
33
|
+
/** 会话日志的代数与压缩后缀,对应 DSH 当前的存储格式。 */
|
|
34
|
+
const LOG_FILENAME = 'session.v4.jsonl.zstd'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 把路径编码成 `--xxx--` 形式的项目目录名。
|
|
38
|
+
*
|
|
39
|
+
* 逐字复刻自 `@deepseek-ai/dsh-session-persistence-jsonl` 的 `projectKey`:
|
|
40
|
+
* 分隔符(`/` `\` `:`)连成单个 `-`,不安全字符写成 `~XXXX`,首尾包 `--`,
|
|
41
|
+
* 超过 251 字符截断。**必须完全一致**,否则算出来的目录和 DSH 用的不是同一个。
|
|
42
|
+
* @param {string} cwd - 会话的工作目录。
|
|
43
|
+
* @returns {string} 项目目录名。
|
|
44
|
+
*/
|
|
45
|
+
export function projectKey(cwd) {
|
|
46
|
+
if (cwd.length === 0) throw new Error('cannot encode an empty project path')
|
|
47
|
+
let readable = ''
|
|
48
|
+
let separatorRun = false
|
|
49
|
+
for (let index = 0; index < cwd.length; index += 1) {
|
|
50
|
+
const code = cwd.charCodeAt(index)
|
|
51
|
+
const char = String.fromCharCode(code)
|
|
52
|
+
if (char === '/' || char === '\\' || char === ':') {
|
|
53
|
+
if (!separatorRun) readable += '-'
|
|
54
|
+
separatorRun = true
|
|
55
|
+
} else if (char !== '~' && /^[A-Za-z0-9._-]$/.test(char)) {
|
|
56
|
+
readable += char
|
|
57
|
+
separatorRun = false
|
|
58
|
+
} else {
|
|
59
|
+
readable += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
60
|
+
separatorRun = false
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return `--${(readable.replace(/^-+/, '') || 'root').slice(0, 251)}--`
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 把任意字符串编码成一个安全的路径段。
|
|
68
|
+
* @param {string} raw - 原始字符串(这里是会话 id)。
|
|
69
|
+
* @returns {string} 编码后的路径段。
|
|
70
|
+
*/
|
|
71
|
+
export function encodeSegment(raw) {
|
|
72
|
+
if (raw.length === 0) throw new Error('cannot encode an empty path segment')
|
|
73
|
+
if (raw === '.') return '~002E'
|
|
74
|
+
if (raw === '..') return '~002E~002E'
|
|
75
|
+
let out = ''
|
|
76
|
+
for (let index = 0; index < raw.length; index += 1) {
|
|
77
|
+
const code = raw.charCodeAt(index)
|
|
78
|
+
const char = String.fromCharCode(code)
|
|
79
|
+
if (char !== '~' && /^[A-Za-z0-9._-]$/.test(char)) out += char
|
|
80
|
+
else out += `~${code.toString(16).toUpperCase().padStart(4, '0')}`
|
|
81
|
+
}
|
|
82
|
+
return out
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 一个会话日志在磁盘上的位置。
|
|
87
|
+
* @param {string} sessionsRoot - `$DSH_HOME/sessions`。
|
|
88
|
+
* @param {string} cwd - 会话的 cwd。
|
|
89
|
+
* @param {string} sessionId - 会话 id。
|
|
90
|
+
* @returns {string} 日志文件的绝对路径。
|
|
91
|
+
*/
|
|
92
|
+
export function sessionLogPath(sessionsRoot, cwd, sessionId) {
|
|
93
|
+
return path.join(sessionsRoot, projectKey(cwd), encodeSegment(sessionId), LOG_FILENAME)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* 一个会话在磁盘上的目录。
|
|
98
|
+
* @param {string} sessionsRoot - `$DSH_HOME/sessions`。
|
|
99
|
+
* @param {string} cwd - 会话的 cwd。
|
|
100
|
+
* @param {string} sessionId - 会话 id。
|
|
101
|
+
* @returns {string} 目录的绝对路径。
|
|
102
|
+
*/
|
|
103
|
+
export function sessionDirPath(sessionsRoot, cwd, sessionId) {
|
|
104
|
+
return path.join(sessionsRoot, projectKey(cwd), encodeSegment(sessionId))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 在缓冲区里找出所有 zstd 帧的起点。
|
|
109
|
+
* @param {Buffer} bytes - 整个文件的内容。
|
|
110
|
+
* @returns {number[]} 每帧起始偏移(升序)。
|
|
111
|
+
*/
|
|
112
|
+
function frameStarts(bytes) {
|
|
113
|
+
const starts = []
|
|
114
|
+
for (let index = 0; index + 4 <= bytes.length; index += 1) {
|
|
115
|
+
if (
|
|
116
|
+
bytes[index] === ZSTD_MAGIC[0] &&
|
|
117
|
+
bytes[index + 1] === ZSTD_MAGIC[1] &&
|
|
118
|
+
bytes[index + 2] === ZSTD_MAGIC[2] &&
|
|
119
|
+
bytes[index + 3] === ZSTD_MAGIC[3]
|
|
120
|
+
) {
|
|
121
|
+
starts.push(index)
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return starts
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 解压一帧;失败就抛,交给调用方决定怎么报告。 */
|
|
128
|
+
function decompressFrame(frame) {
|
|
129
|
+
if (typeof zlib.zstdDecompressSync !== 'function') {
|
|
130
|
+
throw Object.assign(new Error('this Node build has no zstd support'), { code: 'noZstd' })
|
|
131
|
+
}
|
|
132
|
+
return zlib.zstdDecompressSync(frame)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** 压缩一帧。 */
|
|
136
|
+
function compressFrame(text) {
|
|
137
|
+
if (typeof zlib.zstdCompressSync !== 'function') {
|
|
138
|
+
throw Object.assign(new Error('this Node build has no zstd support'), { code: 'noZstd' })
|
|
139
|
+
}
|
|
140
|
+
return zlib.zstdCompressSync(Buffer.from(text, 'utf8'))
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 读一份日志里会话头记录的 cwd。
|
|
145
|
+
*
|
|
146
|
+
* 只解第 1 帧,找到 `"type":"session"` 那一行;找不到就往后面的帧找(正常情况下第一帧就够)。
|
|
147
|
+
* @param {Buffer} bytes - 整个文件的内容。
|
|
148
|
+
* @returns {string | undefined} cwd,或 undefined(头里没有 cwd)。
|
|
149
|
+
*/
|
|
150
|
+
export function readLogCwd(bytes) {
|
|
151
|
+
const starts = frameStarts(bytes)
|
|
152
|
+
if (starts.length === 0) throw Object.assign(new Error('session log holds no zstd frame'), { code: 'logUnreadable' })
|
|
153
|
+
// 头一定在最前面:先看第 1 帧,退化时才继续往后翻。
|
|
154
|
+
const limit = Math.min(starts.length, 4)
|
|
155
|
+
for (let index = 0; index < limit; index += 1) {
|
|
156
|
+
const end = index + 1 < starts.length ? starts[index + 1] : bytes.length
|
|
157
|
+
let text
|
|
158
|
+
try {
|
|
159
|
+
text = decompressFrame(bytes.subarray(starts[index], end)).toString('utf8')
|
|
160
|
+
} catch {
|
|
161
|
+
continue
|
|
162
|
+
}
|
|
163
|
+
for (const line of text.split('\n')) {
|
|
164
|
+
if (!line.includes('"type":"session"')) continue
|
|
165
|
+
try {
|
|
166
|
+
const record = JSON.parse(line)
|
|
167
|
+
if (record !== null && typeof record === 'object' && record.type === 'session') {
|
|
168
|
+
return typeof record.cwd === 'string' ? record.cwd : undefined
|
|
169
|
+
}
|
|
170
|
+
} catch {
|
|
171
|
+
// 这一行坏了,往后找
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return undefined
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* 把日志里会话头的 cwd 换成新的,返回新的完整字节。
|
|
180
|
+
*
|
|
181
|
+
* 只重压第 1 帧,其余帧原样拼回去 —— 帧之间互不依赖,所以这是安全的。
|
|
182
|
+
* 换值用**定点字符串替换**而不是 JSON 往返:往返会重排键序、改变字段写法,
|
|
183
|
+
* 而这是别人正在用的日志,能不动的字节就不动。
|
|
184
|
+
* @param {Buffer} bytes - 原文件内容。
|
|
185
|
+
* @param {string} oldCwd - 原来的 cwd。
|
|
186
|
+
* @param {string} nextCwd - 新的 cwd。
|
|
187
|
+
* @returns {Buffer} 新的文件内容。
|
|
188
|
+
*/
|
|
189
|
+
export function rewriteLogCwd(bytes, oldCwd, nextCwd) {
|
|
190
|
+
const starts = frameStarts(bytes)
|
|
191
|
+
if (starts.length === 0) throw Object.assign(new Error('session log holds no zstd frame'), { code: 'logUnreadable' })
|
|
192
|
+
const headEnd = starts.length > 1 ? starts[1] : bytes.length
|
|
193
|
+
const headText = decompressFrame(bytes.subarray(0, headEnd)).toString('utf8')
|
|
194
|
+
const lines = headText.split('\n')
|
|
195
|
+
|
|
196
|
+
const oldJson = JSON.stringify(oldCwd)
|
|
197
|
+
const nextJson = JSON.stringify(nextCwd)
|
|
198
|
+
let replaced = false
|
|
199
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
200
|
+
const line = lines[index]
|
|
201
|
+
if (!line.includes('"type":"session"')) continue
|
|
202
|
+
const needle = `"cwd":${oldJson}`
|
|
203
|
+
const at = line.indexOf(needle)
|
|
204
|
+
if (at < 0) continue
|
|
205
|
+
lines[index] = `${line.slice(0, at)}"cwd":${nextJson}${line.slice(at + needle.length)}`
|
|
206
|
+
replaced = true
|
|
207
|
+
break
|
|
208
|
+
}
|
|
209
|
+
if (!replaced) {
|
|
210
|
+
throw Object.assign(new Error(`session header carries no cwd "${oldCwd}" to rewrite`), { code: 'headerMismatch' })
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const nextHead = compressFrame(lines.join('\n'))
|
|
214
|
+
const tail = bytes.subarray(headEnd)
|
|
215
|
+
return Buffer.concat([nextHead, tail])
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 目录里所有文件(含子目录),返回相对路径列表。
|
|
220
|
+
* @param {string} dir - 目录。
|
|
221
|
+
* @returns {Promise<string[]>} 相对路径。
|
|
222
|
+
*/
|
|
223
|
+
async function listFiles(dir) {
|
|
224
|
+
const found = []
|
|
225
|
+
const walk = async (current, prefix) => {
|
|
226
|
+
for (const entry of await readdir(current, { withFileTypes: true })) {
|
|
227
|
+
const relative = prefix === '' ? entry.name : `${prefix}/${entry.name}`
|
|
228
|
+
if (entry.isDirectory()) await walk(path.join(current, entry.name), relative)
|
|
229
|
+
else found.push(relative)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
await walk(dir, '')
|
|
233
|
+
return found
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** 这些 errno 说明是「权限不够」,值得让用户确认后再来一次。 */
|
|
237
|
+
const PERMISSION_CODES = new Set(['EACCES', 'EPERM', 'EROFS', 'EBUSY'])
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* 判断一个错误是不是权限问题。
|
|
241
|
+
* @param {unknown} error - 捕获到的错误。
|
|
242
|
+
* @returns {boolean} 是权限问题就返回 true。
|
|
243
|
+
*/
|
|
244
|
+
export function isPermissionError(error) {
|
|
245
|
+
const code = error !== null && typeof error === 'object' ? error.code : undefined
|
|
246
|
+
return typeof code === 'string' && PERMISSION_CODES.has(code)
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* 真迁移:把会话的日志目录整体搬到新 cwd 对应的项目目录,并改写会话头里的 cwd。
|
|
251
|
+
*
|
|
252
|
+
* 顺序是刻意的 —— **先复制、再验证、最后才删原件**:
|
|
253
|
+
* 1. 复制整个会话目录到目标位置(原目录此刻完全没被动过)
|
|
254
|
+
* 2. 只改目标副本里的头
|
|
255
|
+
* 3. 把副本读回来确认 cwd 真的变了、帧也还完整
|
|
256
|
+
* 4. 上面都成立才删旧目录
|
|
257
|
+
* 任何一步抛错都清掉半成品,原会话保持原样。
|
|
258
|
+
* @param {object} options - 迁移参数。
|
|
259
|
+
* @param {string} options.sessionsRoot - `$DSH_HOME/sessions`。
|
|
260
|
+
* @param {string} options.sessionId - 会话 id。
|
|
261
|
+
* @param {string} options.fromCwd - 当前 cwd。
|
|
262
|
+
* @param {string} options.toCwd - 目标 cwd。
|
|
263
|
+
* @returns {Promise<{from: string, to: string, files: number, bytes: number}>} 迁移结果。
|
|
264
|
+
*/
|
|
265
|
+
export async function migrateOnDisk({ sessionsRoot, sessionId, fromCwd, toCwd }) {
|
|
266
|
+
const fromDir = sessionDirPath(sessionsRoot, fromCwd, sessionId)
|
|
267
|
+
const toDir = sessionDirPath(sessionsRoot, toCwd, sessionId)
|
|
268
|
+
if (fromDir === toDir) return { from: fromDir, to: toDir, files: 0, bytes: 0 }
|
|
269
|
+
if (!existsSync(fromDir)) {
|
|
270
|
+
throw Object.assign(new Error(`session directory is missing: ${fromDir}`), { code: 'noDirectory' })
|
|
271
|
+
}
|
|
272
|
+
if (existsSync(toDir)) {
|
|
273
|
+
throw Object.assign(new Error(`a session directory already exists at the destination: ${toDir}`), { code: 'targetExists' })
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
const files = await listFiles(fromDir)
|
|
277
|
+
let bytes = 0
|
|
278
|
+
let created = false
|
|
279
|
+
try {
|
|
280
|
+
mkdirSync(toDir, { recursive: true })
|
|
281
|
+
created = true
|
|
282
|
+
for (const relative of files) {
|
|
283
|
+
const source = path.join(fromDir, relative)
|
|
284
|
+
const destination = path.join(toDir, relative)
|
|
285
|
+
const info = await stat(source)
|
|
286
|
+
bytes += info.size
|
|
287
|
+
mkdirSync(path.dirname(destination), { recursive: true })
|
|
288
|
+
if (relative === LOG_FILENAME) {
|
|
289
|
+
// 日志要改头,不能直连复制。
|
|
290
|
+
const original = await readFile(source)
|
|
291
|
+
const next = rewriteLogCwd(original, fromCwd, toCwd)
|
|
292
|
+
await writeFile(destination, next)
|
|
293
|
+
} else {
|
|
294
|
+
await copyFile(source, destination)
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// 验证副本:cwd 必须是新值,帧数必须和原件一致。
|
|
299
|
+
const copiedLog = await readFile(path.join(toDir, LOG_FILENAME))
|
|
300
|
+
const copiedCwd = readLogCwd(copiedLog)
|
|
301
|
+
if (copiedCwd !== toCwd) {
|
|
302
|
+
throw Object.assign(new Error(`rewritten session header still reports cwd "${copiedCwd}"`), { code: 'verifyFailed' })
|
|
303
|
+
}
|
|
304
|
+
const originalFrames = frameStarts(await readFile(path.join(fromDir, LOG_FILENAME))).length
|
|
305
|
+
const copiedFrames = frameStarts(copiedLog).length
|
|
306
|
+
if (copiedFrames !== originalFrames) {
|
|
307
|
+
throw Object.assign(new Error(`frame count changed: ${originalFrames} -> ${copiedFrames}`), { code: 'verifyFailed' })
|
|
308
|
+
}
|
|
309
|
+
} catch (error) {
|
|
310
|
+
if (created) {
|
|
311
|
+
try {
|
|
312
|
+
await rm(toDir, { recursive: true, force: true })
|
|
313
|
+
} catch {
|
|
314
|
+
// 清不掉半成品也只能留着,但至少要报出原始错误
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
throw error
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// 副本已经验证过了,这时删原件才是安全的。
|
|
321
|
+
await rm(fromDir, { recursive: true, force: true })
|
|
322
|
+
return { from: fromDir, to: toDir, files: files.length, bytes }
|
|
323
|
+
}
|
package/lib/state.js
CHANGED
|
@@ -108,6 +108,25 @@ function normalizeEntry(entry) {
|
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
+
/**
|
|
112
|
+
* 一条「迁移已落盘、但本进程还没登记上」的记录。
|
|
113
|
+
*
|
|
114
|
+
* 会话头的 cwd 改了之后,工作区的内存索引要等下次启动才会重读,所以这一步
|
|
115
|
+
* 只能排队等到重启后再补。**不能省**:不补的话会话重启后会掉进「未分组」,
|
|
116
|
+
* 因为它已经不属于旧工作区、也还没被记进新工作区。
|
|
117
|
+
*/
|
|
118
|
+
function normalizePending(entry) {
|
|
119
|
+
if (!isRecord(entry)) return undefined
|
|
120
|
+
if (typeof entry.sessionId !== 'string' || entry.sessionId === '') return undefined
|
|
121
|
+
if (typeof entry.workspaceId !== 'string' || entry.workspaceId === '') return undefined
|
|
122
|
+
return {
|
|
123
|
+
sessionId: entry.sessionId,
|
|
124
|
+
workspaceId: entry.workspaceId,
|
|
125
|
+
fromWorkspaceId: typeof entry.fromWorkspaceId === 'string' ? entry.fromWorkspaceId : '',
|
|
126
|
+
at: Number.isFinite(entry.at) ? Number(entry.at) : Date.now(),
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
111
130
|
function normalize(candidate) {
|
|
112
131
|
const input = isRecord(candidate) ? candidate : {}
|
|
113
132
|
const settings = normalizeSettings(input.settings)
|
|
@@ -117,11 +136,14 @@ function normalize(candidate) {
|
|
|
117
136
|
const buffer = Array.isArray(input.buffer)
|
|
118
137
|
? input.buffer.map(normalizeEntry).filter((entry) => entry !== undefined)
|
|
119
138
|
: []
|
|
120
|
-
|
|
139
|
+
const pending = Array.isArray(input.pending)
|
|
140
|
+
? input.pending.map(normalizePending).filter((entry) => entry !== undefined)
|
|
141
|
+
: []
|
|
142
|
+
return { version: STATE_VERSION, settings, locks, buffer, pending, updatedAt: typeof input.updatedAt === 'string' ? input.updatedAt : '' }
|
|
121
143
|
}
|
|
122
144
|
|
|
123
145
|
export function emptyState() {
|
|
124
|
-
return { version: STATE_VERSION, settings: { ...DEFAULTS }, locks: [], buffer: [], updatedAt: '' }
|
|
146
|
+
return { version: STATE_VERSION, settings: { ...DEFAULTS }, locks: [], buffer: [], pending: [], updatedAt: '' }
|
|
125
147
|
}
|
|
126
148
|
|
|
127
149
|
/** 读状态文件;缺失或损坏时返回默认值。 */
|
package/locale/en.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "Session manager",
|
|
4
|
-
"description": "Lists every session by workspace in Settings > Session manager, with a lock toggle and a delete button on each row. Deleting asks for confirmation first, then moves the session into a recycle bin at the bottom of the page and out of the workspace list for good: the log file is moved into the recycle bin, so the session stays invisible even with archived sessions shown. Restore brings it back exactly as it was during a configurable window (24 hours by default, 0 deletes for good immediately)
|
|
4
|
+
"description": "Lists every session by workspace in Settings > Session manager, with a lock toggle, a move-to-workspace picker, and a delete button on each row. Deleting asks for confirmation first, then moves the session into a recycle bin at the bottom of the page and out of the workspace list for good: the log file is moved into the recycle bin, so the session stays invisible even with archived sessions shown. Restore brings it back exactly as it was during a configurable window (24 hours by default, 0 deletes for good immediately). The picker moves a session to another workspace, which really relocates the log folder on disk and rewrites the session header's cwd; when the system refuses a file operation the panel lists every step and both real paths first, and you choose between retrying and changing the registration only. Locked sessions can be neither deleted nor moved, and both refusals come from the Host rather than from a disabled button.",
|
|
5
5
|
"detail": "docs/DETAIL.md"
|
|
6
6
|
}
|
|
7
|
-
}
|
|
7
|
+
}
|
package/locale/zh.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"meta": {
|
|
3
3
|
"title": "会话删除管理",
|
|
4
|
-
"description": "在「设置 →
|
|
4
|
+
"description": "在「设置 → 会话删除管理」里按工作区列出所有会话,每行带锁定、迁移和删除。删除要先二次确认,确认后会话进入页面底部的删除缓冲站并立即从左侧工作区列表消失(不是变灰,是看不见):日志被移进回收站,所以在打开「显示归档会话」时也不会出现。缓冲期内点「回档」能原样恢复(默认 24 小时,填 0 表示确认后立即彻底删除)。每行的下拉框能把会话迁到另一个工作区,迁移会真的搬动磁盘上的日志目录并改写会话头里的 cwd;系统拒绝文件操作时先把要做的每一步和真实路径告诉你,由你决定是重试还是只改归属。被锁定的会话既删不掉也迁不动,这两条都由宿主校验,不只是界面禁用。",
|
|
5
5
|
"detail": "docs/DETAIL.zh.md"
|
|
6
6
|
}
|
|
7
|
-
}
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@psyduck25/dsh-session-manager",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Delete a single session with a two-step confirmation and a recycle bin: browse every session by workspace, lock the ones to keep, and recover deleted sessions until the configurable buffer window expires.",
|
|
6
6
|
"keywords": [
|