@kabxx/dsh-session-move 0.0.0-stage → 0.1.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 +51 -2
- package/cordis.patch.yml +7 -0
- package/icon.svg +9 -0
- package/locale/en.json +6 -0
- package/locale/recovery/en.json +6 -0
- package/locale/recovery/zh.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +45 -4
- package/src/client.js +275 -0
- package/src/config.js +11 -0
- package/src/inbox.js +39 -0
- package/src/index.js +16 -0
- package/src/lease.js +91 -0
- package/src/ledger.js +323 -0
- package/src/move.js +160 -0
- package/src/paths.js +68 -0
- package/src/plan.js +163 -0
- package/src/platform-fs.js +253 -0
- package/src/recovery.js +161 -0
- package/src/sidebar.js +45 -0
- package/src/transaction.js +468 -0
- package/src/zstd-frame.js +140 -0
package/README.md
CHANGED
|
@@ -1,3 +1,52 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kabxx/dsh-session-move
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
面向 DeepSeek Harness(`0.2.2-rc.5`)的会话跨工作区拖拽移动插件,支持 Linux(含 WSL)、macOS 与原生 Windows 的默认 Zstandard JSONL 存储。
|
|
4
|
+
|
|
5
|
+
- 在侧栏将普通会话直接拖到目标工作区的文件夹标题行(折叠或空工作区同样支持),原工作区内的上下排序不受影响。
|
|
6
|
+
- 连同关联的子智能体会话、日志目录与工作目录整组迁移:仅重写首帧会话头 `cwd` 并追加工作目录变更事件,同步更新工作区账本、侧栏列表与全文搜索索引;若日志已位于目标目录则仅补全工作区登记。
|
|
7
|
+
- 移动前自动释放空闲的 Web 会话句柄并通过原生会话锁(Linux / macOS `flock`、Windows 命名信号量)校验独占状态;运行中、存在未消费消息、归档或单独拖动的子智能体会话会被提前拦截,中途异常中断时由 `/recovery` 子插件在启动或下次操作时自动回滚或补完清理。
|
|
8
|
+
|
|
9
|
+
## 安装
|
|
10
|
+
|
|
11
|
+
需要 Node.js `>= 24.9.0`(Linux 环境需包含 `/usr/bin/flock`):
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
dsh plugin --profile web add @kabxx/dsh-session-move
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
也可在 DSH 插件管理器中选择「添加插件」并输入 `@kabxx/dsh-session-move`(若使用其他配置档案,将 `--profile web` 替换为对应档案名)。本地调试源码时,直接填入克隆后的仓库目录绝对路径或执行 `dsh plugin --profile web add .` 即可。
|
|
18
|
+
|
|
19
|
+
## 开发
|
|
20
|
+
|
|
21
|
+
运行语法检查与测试(集成测试需将 `DSH_INSTALL_DIR` 与 `DSH_LEDGER_TEST_INSTALL` 指向本机 `@deepseek-ai/dsh` 安装目录):
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
export DSH_INSTALL_DIR="$(npm root -g)/@deepseek-ai/dsh"
|
|
25
|
+
export DSH_LEDGER_TEST_INSTALL="$DSH_INSTALL_DIR"
|
|
26
|
+
npm run check
|
|
27
|
+
npm test
|
|
28
|
+
npm pack --dry-run
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## 项目结构
|
|
32
|
+
|
|
33
|
+
| 路径 | 内容 |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| [`src/index.js`](src/index.js) | Host 入口,注册 `/api/session-move/session` 接口 |
|
|
36
|
+
| [`src/recovery.js`](src/recovery.js) | `/recovery` 子插件入口,负责启动时与按需的事务恢复及尾项清理 |
|
|
37
|
+
| [`src/move.js`](src/move.js) | 跨工作区移动主流程、空闲校验、事务提交与异常映射 |
|
|
38
|
+
| [`src/plan.js`](src/plan.js) | 移动前置校验、父子智能体会话组展开与目标路径检查 |
|
|
39
|
+
| [`src/transaction.js`](src/transaction.js) | 事务意图日志、整组备份暂存、原子目录切换与回滚 |
|
|
40
|
+
| [`src/zstd-frame.js`](src/zstd-frame.js) | Zstandard 会话容器帧扫描与首帧 `cwd` 原地重写 |
|
|
41
|
+
| [`src/ledger.js`](src/ledger.js) | 工作区 KV 账本原子切换、回滚与搜索索引刷新 |
|
|
42
|
+
| [`src/sidebar.js`](src/sidebar.js) | 迁移后刷新投影缓存与侧栏会话摘要事件 |
|
|
43
|
+
| [`src/lease.js`](src/lease.js) | 识别并安全释放空闲的 Web 会话写入句柄 |
|
|
44
|
+
| [`src/inbox.js`](src/inbox.js) | 重放校验会话持久收件箱是否存在未消费消息 |
|
|
45
|
+
| [`src/platform-fs.js`](src/platform-fs.js) | 跨平台会话锁(`flock` / Windows 信号量)、符号链接检查与原子替换 |
|
|
46
|
+
| [`src/paths.js`](src/paths.js) | 与原生 JSONL 存储一致的工作区目录编码计算 |
|
|
47
|
+
| [`src/config.js`](src/config.js) | 插件事务与历史版本恢复目录路径配置 |
|
|
48
|
+
| [`src/client.js`](src/client.js) | Web 侧栏文件夹行拖拽捕获、高亮反馈与 Toast 提示 |
|
|
49
|
+
| [`locale/`](locale/) | 主插件与 `/recovery` 子插件管理页中英文标题与描述 |
|
|
50
|
+
| [`test/`](test/) | 拖拽交互、Zstd 帧重写、账本切换与多阶段崩溃恢复测试 |
|
|
51
|
+
|
|
52
|
+
插件入口与注入配置见 [package.json](package.json) 和 [cordis.patch.yml](cordis.patch.yml)。
|
package/cordis.patch.yml
ADDED
package/icon.svg
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64" width="64" height="64" role="img" aria-label="Move session to another workspace">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#101820"/>
|
|
3
|
+
<path d="M12 22a4 4 0 0 1 4-4h9.2l3.4 4.2H48a4 4 0 0 1 4 4v17.6a4 4 0 0 1-4 4H16a4 4 0 0 1-4-4Z"
|
|
4
|
+
fill="none" stroke="#4cc2ff" stroke-width="3" stroke-linejoin="round"/>
|
|
5
|
+
<path d="M12 30.4h40" stroke="#4cc2ff" stroke-width="3" stroke-linecap="round" opacity="0.55"/>
|
|
6
|
+
<path d="M25 39.5h13" stroke="#7ee787" stroke-width="3" stroke-linecap="round"/>
|
|
7
|
+
<path d="M33.5 34.8 38.6 39.5l-5.1 4.7" fill="none" stroke="#7ee787" stroke-width="3"
|
|
8
|
+
stroke-linecap="round" stroke-linejoin="round"/>
|
|
9
|
+
</svg>
|
package/locale/en.json
ADDED
package/locale/zh.json
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kabxx/dsh-session-move",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Move sessions across workspaces by dragging them in the DeepSeek Harness sidebar.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/kabxx/dsh-session-move.git"
|
|
12
|
+
},
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/index.js",
|
|
15
|
+
"./recovery": "./src/recovery.js",
|
|
16
|
+
"./client": "./src/client.js",
|
|
17
|
+
"./locale/*.json": "./locale/*.json",
|
|
18
|
+
"./recovery/locale/*.json": "./locale/recovery/*.json",
|
|
19
|
+
"./icon": "./icon.svg",
|
|
20
|
+
"./recovery/icon": "./icon.svg"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"src/",
|
|
24
|
+
"cordis.patch.yml",
|
|
25
|
+
"locale/",
|
|
26
|
+
"icon.svg"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"check": "node -e \"for (const f of require('node:fs').readdirSync('src')) if (f.endsWith('.js')) require('node:child_process').execFileSync(process.execPath, ['--check', 'src/' + f], {stdio: 'inherit'})\"",
|
|
30
|
+
"test": "node --test \"test/*.test.js\""
|
|
31
|
+
},
|
|
32
|
+
"engines": {
|
|
33
|
+
"node": ">=24.9.0",
|
|
34
|
+
"dsh": "0.2.2-rc.5"
|
|
35
|
+
},
|
|
36
|
+
"dsh": {
|
|
37
|
+
"bundle": {
|
|
38
|
+
"patch": "./cordis.patch.yml"
|
|
39
|
+
},
|
|
40
|
+
"client": {
|
|
41
|
+
"platform": "web",
|
|
42
|
+
"inject": [
|
|
43
|
+
"@deepseek-ai/dsh-client-ui-workspace"
|
|
44
|
+
]
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/client.js
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: '@kabxx/dsh-session-move',
|
|
3
|
+
factory(require) {
|
|
4
|
+
const { useSyncExternalStore } = require('react')
|
|
5
|
+
const { createSnapshotStore } = require('@deepseek-ai/dsh-client-store')
|
|
6
|
+
const namespace = 'sessionMove'
|
|
7
|
+
const strings = {
|
|
8
|
+
zh: {
|
|
9
|
+
moved: '会话已移动到“{title}”',
|
|
10
|
+
pending: '会话已移动,后续清理待完成。',
|
|
11
|
+
'sidebar-pending': '会话已移动,侧栏暂未同步,请刷新页面。',
|
|
12
|
+
busy: '会话正在运行或还有待处理指令,暂时无法移动。',
|
|
13
|
+
locked: '会话正被占用,暂时无法移动。',
|
|
14
|
+
archived: '会话已归档,请先取消归档。',
|
|
15
|
+
subagent: '子智能体会话随父会话一起移动,无法单独移动。',
|
|
16
|
+
'unsafe-path': '会话存储路径异常,无法安全移动。',
|
|
17
|
+
'cross-device': '源会话与目标会话目录不在同一个文件系统,无法安全移动。',
|
|
18
|
+
restored: '移动失败,会话记录已恢复到原目录。',
|
|
19
|
+
forbidden: '当前连接无权移动会话。',
|
|
20
|
+
missing: '未找到该会话。',
|
|
21
|
+
'target-missing': '目标工作区目录不存在。',
|
|
22
|
+
'target-not-canonical': '目标工作区路径不是规范路径,无法移入会话。',
|
|
23
|
+
'same-workspace': '会话已经在该工作区中。',
|
|
24
|
+
'stale-source': '会话所在工作区已变更,请从侧栏新位置重新拖动。',
|
|
25
|
+
collision: '目标工作区中已存在同名会话记录。',
|
|
26
|
+
changed: '会话状态已变更,请重试。',
|
|
27
|
+
unsupported: '当前存储方式不支持移动会话。',
|
|
28
|
+
'restore-pending': '移动中断,会话待自动恢复,请稍后重试。',
|
|
29
|
+
'cleanup-pending': '上一次移动仍在清理中,请稍后重试。',
|
|
30
|
+
failed: '移动失败,请重试。',
|
|
31
|
+
},
|
|
32
|
+
en: {
|
|
33
|
+
moved: 'Session moved to “{title}”',
|
|
34
|
+
pending: 'Session moved; follow-up cleanup is still pending.',
|
|
35
|
+
'sidebar-pending': 'Session moved, but the sidebar did not update. Refresh the page.',
|
|
36
|
+
busy: 'Session is running or has pending instructions. It cannot be moved yet.',
|
|
37
|
+
locked: 'Session is in use and cannot be moved right now.',
|
|
38
|
+
archived: 'Unarchive this session before moving it.',
|
|
39
|
+
subagent: 'Subagent sessions move with their parent and cannot be moved separately.',
|
|
40
|
+
'unsafe-path': 'The session storage path cannot be moved safely.',
|
|
41
|
+
'cross-device': 'The source and destination session directories are on different filesystems.',
|
|
42
|
+
restored: 'The move failed. The session record was restored to its original location.',
|
|
43
|
+
forbidden: 'This connection cannot move sessions.',
|
|
44
|
+
missing: 'Session not found.',
|
|
45
|
+
'target-missing': 'The target workspace directory is missing.',
|
|
46
|
+
'target-not-canonical': 'The target workspace path is not canonical and cannot receive moved sessions.',
|
|
47
|
+
'same-workspace': 'The session is already in that workspace.',
|
|
48
|
+
'stale-source': 'The session’s workspace has changed. Drag it again from its new location.',
|
|
49
|
+
collision: 'The target workspace already holds a record for this session.',
|
|
50
|
+
changed: 'Session state changed. Try again.',
|
|
51
|
+
unsupported: 'Moving sessions is not supported for this storage.',
|
|
52
|
+
'restore-pending': 'The move was interrupted. Automatic restoration is pending; try again shortly.',
|
|
53
|
+
'cleanup-pending': 'A previous move is still finishing cleanup. Try again shortly.',
|
|
54
|
+
failed: 'Failed to move the session. Try again.',
|
|
55
|
+
},
|
|
56
|
+
}
|
|
57
|
+
const known = new Set([
|
|
58
|
+
'busy', 'locked', 'missing', 'target-missing', 'target-not-canonical', 'same-workspace', 'stale-source',
|
|
59
|
+
'collision', 'changed', 'unsupported', 'restore-pending', 'cleanup-pending',
|
|
60
|
+
'archived', 'subagent', 'unsafe-path', 'cross-device', 'restored', 'forbidden',
|
|
61
|
+
])
|
|
62
|
+
const ROW_KEY = 'data-row-key'
|
|
63
|
+
const WORKSPACE_PREFIX = 'workspace:'
|
|
64
|
+
const SESSION_PREFIX = 'session:'
|
|
65
|
+
|
|
66
|
+
return {
|
|
67
|
+
inject: ['locale', 'slots', 'workspaces'],
|
|
68
|
+
apply(ctx) {
|
|
69
|
+
const t = ctx.locale.bind(namespace)
|
|
70
|
+
const toast = createSnapshotStore(null)
|
|
71
|
+
const lifetime = new AbortController()
|
|
72
|
+
let sequence = 0
|
|
73
|
+
let dragging = null
|
|
74
|
+
let highlighted = null
|
|
75
|
+
let savedStyle = null
|
|
76
|
+
|
|
77
|
+
function notify(key, tone = 'warning', values) {
|
|
78
|
+
toast.set({ key, tone, values, seq: ++sequence })
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function clearHighlight() {
|
|
82
|
+
if (highlighted === null) return
|
|
83
|
+
highlighted.style.outline = savedStyle.outline
|
|
84
|
+
highlighted.style.outlineOffset = savedStyle.outlineOffset
|
|
85
|
+
highlighted = null
|
|
86
|
+
savedStyle = null
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function highlight(element) {
|
|
90
|
+
if (highlighted === element) return
|
|
91
|
+
clearHighlight()
|
|
92
|
+
highlighted = element
|
|
93
|
+
savedStyle = { outline: element.style.outline, outlineOffset: element.style.outlineOffset }
|
|
94
|
+
// Match the native insertion marker's color while outlining the drop target.
|
|
95
|
+
element.style.outline = '2px solid var(--dsw-alias-state-business-primary)'
|
|
96
|
+
element.style.outlineOffset = '-2px'
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function workspaceIdForSession(snapshot, sessionId) {
|
|
100
|
+
return snapshot.items.find((item) => item.sessionIds.includes(sessionId))?.workspaceId ?? null
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function closestRow(target) {
|
|
104
|
+
return target instanceof Element ? target.closest(`[${ROW_KEY}]`) : null
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function dropTarget(element, active) {
|
|
108
|
+
const row = closestRow(element)
|
|
109
|
+
const key = row?.getAttribute(ROW_KEY)
|
|
110
|
+
// WorkspaceBrowser renders each folder as its own row. A session row
|
|
111
|
+
// may follow nested folders in DOM order while belonging to the parent.
|
|
112
|
+
if (!key?.startsWith(WORKSPACE_PREFIX)) return null
|
|
113
|
+
const id = key.slice(WORKSPACE_PREFIX.length)
|
|
114
|
+
// The ungrouped bucket is represented by `workspace:` with no id.
|
|
115
|
+
if (!id || id === active.sourceWorkspaceId) return null
|
|
116
|
+
const snapshot = ctx.workspaces.list.getSnapshot()
|
|
117
|
+
if (snapshot.archivedSessionIds.includes(active.id)) return null
|
|
118
|
+
const workspace = snapshot.items.find((item) => item.workspaceId === id)
|
|
119
|
+
return workspace === undefined ? null : { row, workspace }
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function perform(sessionId, targetWorkspaceId, sourceWorkspaceId, title) {
|
|
123
|
+
try {
|
|
124
|
+
const response = await fetch('/api/session-move/session', {
|
|
125
|
+
method: 'POST',
|
|
126
|
+
credentials: 'same-origin',
|
|
127
|
+
signal: lifetime.signal,
|
|
128
|
+
headers: { 'Content-Type': 'application/json' },
|
|
129
|
+
body: JSON.stringify({ sessionId, targetWorkspaceId, sourceWorkspaceId }),
|
|
130
|
+
})
|
|
131
|
+
const result = await response.json().catch(() => null)
|
|
132
|
+
if (lifetime.signal.aborted) return
|
|
133
|
+
if (!response.ok || result?.error || result?.moved !== true) {
|
|
134
|
+
const code = result?.error
|
|
135
|
+
if (known.has(code)) throw new Error(code)
|
|
136
|
+
if (response.status === 401 || response.status === 403) throw new Error('forbidden')
|
|
137
|
+
throw new Error('failed')
|
|
138
|
+
}
|
|
139
|
+
if (result.sidebarPending) notify('sidebar-pending')
|
|
140
|
+
else if (result.cleanupPending) notify('pending')
|
|
141
|
+
else notify('moved', 'success', { title: title ?? '' })
|
|
142
|
+
} catch (error) {
|
|
143
|
+
if (lifetime.signal.aborted) return
|
|
144
|
+
notify(known.has(error.message) ? error.message : 'failed')
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function onDragStart(event) {
|
|
149
|
+
clearDrag()
|
|
150
|
+
const row = closestRow(event.target)
|
|
151
|
+
const key = row?.getAttribute(ROW_KEY)
|
|
152
|
+
if (!key?.startsWith(SESSION_PREFIX) || row.draggable !== true) return
|
|
153
|
+
const id = key.slice(SESSION_PREFIX.length)
|
|
154
|
+
const snapshot = ctx.workspaces.list.getSnapshot()
|
|
155
|
+
if (!id || snapshot.archivedSessionIds.includes(id)) return
|
|
156
|
+
// Preserve the source the user saw if another window moves the session
|
|
157
|
+
// during the gesture. The Host checks it before applying the move.
|
|
158
|
+
dragging = { id, sourceWorkspaceId: workspaceIdForSession(snapshot, id), row }
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function onDragOver(event) {
|
|
162
|
+
if (dragging === null) return
|
|
163
|
+
const target = dropTarget(event.target, dragging)
|
|
164
|
+
if (target === null) {
|
|
165
|
+
clearHighlight()
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
event.preventDefault()
|
|
169
|
+
if (event.dataTransfer) event.dataTransfer.dropEffect = 'move'
|
|
170
|
+
highlight(target.row)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function showDropEffect(event) {
|
|
174
|
+
if (dragging === null || !event.dataTransfer || dropTarget(event.target, dragging)) return
|
|
175
|
+
const key = closestRow(event.target)?.getAttribute(ROW_KEY)
|
|
176
|
+
if (key?.startsWith(SESSION_PREFIX)) {
|
|
177
|
+
const id = key.slice(SESSION_PREFIX.length)
|
|
178
|
+
if (workspaceIdForSession(ctx.workspaces.list.getSnapshot(), id) === dragging.sourceWorkspaceId) return
|
|
179
|
+
}
|
|
180
|
+
// The native document listener accepts all drags so it can finish
|
|
181
|
+
// its own sorting gesture. Keep the cross-workspace cursor accurate.
|
|
182
|
+
event.dataTransfer.dropEffect = 'none'
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function onDragLeave(event) {
|
|
186
|
+
if (highlighted === null || closestRow(event.target) !== highlighted) return
|
|
187
|
+
if (!(event.relatedTarget instanceof Node) || !highlighted.contains(event.relatedTarget)) clearHighlight()
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function cancelNativeSort(active) {
|
|
191
|
+
// DSH commits its last hover marker on dragend, even without a drop.
|
|
192
|
+
// A drop on the source row settles the native drag without reordering.
|
|
193
|
+
if (active.row.isConnected && active.row.getAttribute(ROW_KEY) === `${SESSION_PREFIX}${active.id}`) {
|
|
194
|
+
active.row.dispatchEvent(new MouseEvent('drop', { bubbles: true, cancelable: true }))
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function onDrop(event) {
|
|
199
|
+
const active = dragging
|
|
200
|
+
if (active === null) return
|
|
201
|
+
const target = dropTarget(event.target, active)
|
|
202
|
+
const wasHighlighted = highlighted !== null && closestRow(event.target) === highlighted
|
|
203
|
+
// Let DSH handle actual sort drops. Keep tracking the gesture so
|
|
204
|
+
// dragend can discard any marker left by a rejected drop instead.
|
|
205
|
+
if (target === null && !wasHighlighted) return
|
|
206
|
+
clearDrag()
|
|
207
|
+
event.preventDefault()
|
|
208
|
+
event.stopPropagation()
|
|
209
|
+
cancelNativeSort(active)
|
|
210
|
+
if (target === null) {
|
|
211
|
+
notify('changed')
|
|
212
|
+
return
|
|
213
|
+
}
|
|
214
|
+
const { workspace } = target
|
|
215
|
+
void perform(active.id, workspace.workspaceId, active.sourceWorkspaceId, workspace.title || workspace.path)
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function clearDrag() {
|
|
219
|
+
const active = dragging
|
|
220
|
+
dragging = null
|
|
221
|
+
clearHighlight()
|
|
222
|
+
return active
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function onDragEnd() {
|
|
226
|
+
const active = clearDrag()
|
|
227
|
+
// Capture runs before DSH's row-level dragend. If a valid sort drop
|
|
228
|
+
// already committed, DSH ignores the redundant source-row drop.
|
|
229
|
+
if (active !== null) cancelNativeSort(active)
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function MoveToast({ t, renderFactorySlot }) {
|
|
233
|
+
const current = useSyncExternalStore(toast.subscribe, toast.getSnapshot)
|
|
234
|
+
if (!current) return null
|
|
235
|
+
return renderFactorySlot('workspace.toast.content', {
|
|
236
|
+
seq: current.seq,
|
|
237
|
+
text: t(current.key, current.values),
|
|
238
|
+
tone: current.tone,
|
|
239
|
+
onDone: () => {
|
|
240
|
+
if (toast.getSnapshot() === current) toast.set(null)
|
|
241
|
+
},
|
|
242
|
+
})
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
ctx.effect(() => () => lifetime.abort())
|
|
246
|
+
ctx.effect(() => ctx.locale.register(namespace, strings))
|
|
247
|
+
|
|
248
|
+
ctx.effect(() => {
|
|
249
|
+
const listeners = [
|
|
250
|
+
['dragstart', onDragStart, true],
|
|
251
|
+
['dragover', onDragOver, true],
|
|
252
|
+
['dragleave', onDragLeave, true],
|
|
253
|
+
['drop', onDrop, true],
|
|
254
|
+
['dragend', onDragEnd, true],
|
|
255
|
+
]
|
|
256
|
+
for (const [type, listener, capture] of listeners) {
|
|
257
|
+
document.addEventListener(type, listener, capture)
|
|
258
|
+
}
|
|
259
|
+
window.addEventListener('dragover', showDropEffect)
|
|
260
|
+
return () => {
|
|
261
|
+
window.removeEventListener('dragover', showDropEffect)
|
|
262
|
+
for (const [type, listener, capture] of listeners) {
|
|
263
|
+
document.removeEventListener(type, listener, capture)
|
|
264
|
+
}
|
|
265
|
+
clearDrag()
|
|
266
|
+
}
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
ctx.effect(() => ctx.slots.inject('shell.overlay', () => ctx.slots.register({
|
|
270
|
+
name: 'shell.overlay', id: 'session-move.toast', locale: namespace,
|
|
271
|
+
}, MoveToast)))
|
|
272
|
+
},
|
|
273
|
+
}
|
|
274
|
+
},
|
|
275
|
+
})
|
package/src/config.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import manifest from '../package.json' with { type: 'json' }
|
|
2
|
+
|
|
3
|
+
export function storagePaths(ctx) {
|
|
4
|
+
const home = (...segments) => ctx.dshHomePath('plugins', '@kabxx', 'dsh-session-move', ...segments)
|
|
5
|
+
return {
|
|
6
|
+
root: ctx.dshHomePath('sessions'),
|
|
7
|
+
base: home(`v${manifest.version}`, 'transactions'),
|
|
8
|
+
previous: ['v0.1.1', 'v0.1.2'].map((version) => home(version)),
|
|
9
|
+
legacy: home('v0.1.0'),
|
|
10
|
+
}
|
|
11
|
+
}
|
package/src/inbox.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { fail } from './plan.js'
|
|
2
|
+
|
|
3
|
+
export async function readMoveEvents(ctx, { id, oldCwd }) {
|
|
4
|
+
let file
|
|
5
|
+
try {
|
|
6
|
+
file = await ctx.sessionPersistence.open(id, 'read')
|
|
7
|
+
if (file.header.id !== id || file.header.cwd !== oldCwd) throw fail('changed')
|
|
8
|
+
const { events } = await file.read()
|
|
9
|
+
assertNoPendingInbox(ctx, file.header, events, file.inheritedEventCount)
|
|
10
|
+
return events
|
|
11
|
+
} catch (error) {
|
|
12
|
+
if (error.code === 'busy' || error.code === 'changed') throw error
|
|
13
|
+
throw fail('unsupported', error)
|
|
14
|
+
} finally {
|
|
15
|
+
await file?.close()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Check the full durable inbox at the caller's quiescent, write-locked cut. */
|
|
20
|
+
export function assertNoPendingInbox(ctx, header, events, inheritedEventCount) {
|
|
21
|
+
let inbox
|
|
22
|
+
try {
|
|
23
|
+
if (!Array.isArray(events) || !Number.isSafeInteger(inheritedEventCount)
|
|
24
|
+
|| inheritedEventCount < 0 || inheritedEventCount > events.length) {
|
|
25
|
+
throw new Error('Invalid session replay cut')
|
|
26
|
+
}
|
|
27
|
+
// An empty checkpoint forces DSH to replay from the origin instead of
|
|
28
|
+
// taking a potentially stale cached inbox or opening a live agent.
|
|
29
|
+
const restored = ctx.sessionProjections.restore({}, events, 0, header, inheritedEventCount)
|
|
30
|
+
inbox = restored.snapshot.values.inbox
|
|
31
|
+
if (!Array.isArray(inbox?.['next-turn']) || !Array.isArray(inbox['next-step'])) {
|
|
32
|
+
throw new Error('DSH did not restore the standard inbox projection')
|
|
33
|
+
}
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw fail('unsupported', error)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (inbox['next-turn'].length || inbox['next-step'].length) throw fail('busy')
|
|
39
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createSessionMove } from './move.js'
|
|
2
|
+
import { storagePaths } from './config.js'
|
|
3
|
+
|
|
4
|
+
export const inject = [
|
|
5
|
+
'connection', 'workspaceRegistry', 'sessions', 'agents', 'sessionController', 'sessionPersistence', 'sessionProjections', 'dshHomePath', 'sessionMoveRecovery',
|
|
6
|
+
]
|
|
7
|
+
|
|
8
|
+
export function apply(ctx) {
|
|
9
|
+
const move = createSessionMove(ctx, storagePaths(ctx))
|
|
10
|
+
ctx.effect(() => ctx.connection.fetch.register({
|
|
11
|
+
path: '/api/session-move/session',
|
|
12
|
+
methods: ['POST'],
|
|
13
|
+
requestBody: 'buffered',
|
|
14
|
+
fetch: move,
|
|
15
|
+
}))
|
|
16
|
+
}
|
package/src/lease.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Release the Web session controller's long-lived write handle.
|
|
3
|
+
*
|
|
4
|
+
* `dsh-agent-loop` opens the session log for write and keeps the handle on the
|
|
5
|
+
* Session Controller fiber for as long as the session stays resident in the
|
|
6
|
+
* GUI. Moving the log directory while that handle is open would leave the old
|
|
7
|
+
* file descriptor writing into a moved-away path, so a resident session must
|
|
8
|
+
* be torn down first.
|
|
9
|
+
*
|
|
10
|
+
* The teardown is reached through the `cordis.effect` symbols rather than a
|
|
11
|
+
* public API, because DSH exposes no "close this session" entry point. The
|
|
12
|
+
* exact-effect identity check below is what makes this safe: if the pinned
|
|
13
|
+
* release ever changes its fiber layout, this refuses instead of disposing the
|
|
14
|
+
* wrong thing.
|
|
15
|
+
*
|
|
16
|
+
* Adapted from the author's `dsh-session-delete` plugin, which proved this
|
|
17
|
+
* path against the same DSH release.
|
|
18
|
+
*
|
|
19
|
+
* @module dsh-session-move/lease
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
const effect = Symbol.for('cordis.effect')
|
|
23
|
+
const original = Symbol.for('cordis.original')
|
|
24
|
+
|
|
25
|
+
function refuse(code) {
|
|
26
|
+
throw Object.assign(new Error(`Session writer (${code})`), { code })
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function uniqueEffect(fiber, label) {
|
|
30
|
+
const effects = fiber?._disposables
|
|
31
|
+
if (!effects?.[Symbol.iterator]) return undefined
|
|
32
|
+
const matches = [...effects].filter((entry) => typeof entry === 'function' && entry[effect]?.label === label)
|
|
33
|
+
if (matches.length === 1) return matches[0]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function assertNoWork(agent) {
|
|
37
|
+
if (agent.status === 'running' || agent.phase?.kind === 'maintenance' || agent.inbox?.hasPending === true) refuse('busy')
|
|
38
|
+
if (agent.status !== 'idle' || agent.phase?.kind !== 'idle' || agent.inbox?.hasPending !== false) refuse('locked')
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Describe the teardown needed to release one resident session's writer.
|
|
43
|
+
* @param ctx - the plugin context.
|
|
44
|
+
* @param session - the live session to release.
|
|
45
|
+
* @returns the teardown handle; `dispose` is absent when nothing is resident.
|
|
46
|
+
*/
|
|
47
|
+
export function idleWebWriter(ctx, session) {
|
|
48
|
+
const agent = ctx.get('agents')?.get(session.id)
|
|
49
|
+
if (agent?.session !== session) refuse('locked')
|
|
50
|
+
const subagents = ctx.get('subagents')
|
|
51
|
+
const manager = (subagents?.[original] ?? subagents)?.manager
|
|
52
|
+
const activation = manager?.resident?.get(session.id)
|
|
53
|
+
if (session.header.origin === 'subagent' && activation?.kind === 'local' && activation.handle?.agent === agent) {
|
|
54
|
+
// The subagent manager owns settlement here and may notify the parent on
|
|
55
|
+
// close; wait for that native transition instead of tearing it down.
|
|
56
|
+
if (!activation.accepted || agent.inbox?.hasPending !== false || agent.status !== 'idle') refuse('busy')
|
|
57
|
+
if (activation.closing === undefined && agent.phase?.kind !== 'idle') refuse('busy')
|
|
58
|
+
return { session, agent, settling: true }
|
|
59
|
+
}
|
|
60
|
+
assertNoWork(agent)
|
|
61
|
+
if (agent.lifetimeSignal?.aborted !== false || session.header.origin === 'subagent') refuse('locked')
|
|
62
|
+
|
|
63
|
+
const service = ctx.get('sessionController')
|
|
64
|
+
const controller = service?.[original] ?? service
|
|
65
|
+
const owner = controller?.agents?.ctx?.fiber
|
|
66
|
+
const scope = agent.scope?.rawDispose?.[effect]
|
|
67
|
+
const scopeEffect = uniqueEffect(agent.loopCtx?.fiber, `agentLoop.scope(${session.id})`)
|
|
68
|
+
const dispose = uniqueEffect(owner, `agentLoop.lifecycle(${session.id})`)
|
|
69
|
+
if (!owner || owner !== controller.ctx?.fiber || !scope
|
|
70
|
+
|| !scopeEffect?.[effect].children.includes(scope) || !dispose) refuse('locked')
|
|
71
|
+
return { session, agent, dispose }
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Run every teardown after re-checking that each one is still the current one.
|
|
76
|
+
* @param ctx - the plugin context.
|
|
77
|
+
* @param writers - handles from {@link idleWebWriter}.
|
|
78
|
+
*/
|
|
79
|
+
export async function releaseIdleWebWriters(ctx, writers) {
|
|
80
|
+
for (const writer of writers) {
|
|
81
|
+
const current = idleWebWriter(ctx, writer.session)
|
|
82
|
+
if (ctx.sessions.get(writer.session.id) !== writer.session
|
|
83
|
+
|| current.agent !== writer.agent || current.dispose !== writer.dispose) refuse('changed')
|
|
84
|
+
}
|
|
85
|
+
// Check the entire family before starting any native teardown. Every
|
|
86
|
+
// teardown must settle successfully before the caller takes a backup.
|
|
87
|
+
const outcome = await Promise.allSettled(writers.map(async ({ dispose }) => { await dispose() }))
|
|
88
|
+
for (const result of outcome) {
|
|
89
|
+
if (result.status === 'rejected') throw result.reason
|
|
90
|
+
}
|
|
91
|
+
}
|