dsh-rig 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/lib/http/routes.js +14 -0
- package/lib/lab/constants.js +2 -0
- package/lib/lab/orphans.js +89 -0
- package/lib/lab/port.js +34 -0
- package/lib/lab/registry.js +5 -1
- package/lib/lab/spawn-plan.js +3 -3
- package/lib/lab/supervisor.js +18 -18
- package/lib/lab/versioning.js +18 -0
- package/lib/tools/lab-tool.js +17 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,23 @@
|
|
|
3
3
|
本项目的所有重要改动都会记在这里。格式参考 [Keep a Changelog](https://keepachangelog.com/zh-CN/1.1.0/),
|
|
4
4
|
版本号遵循 [SemVer](https://semver.org/lang/zh-CN/)。
|
|
5
5
|
|
|
6
|
+
## [0.1.3] - 2026-11-21
|
|
7
|
+
|
|
8
|
+
### Changed
|
|
9
|
+
|
|
10
|
+
- 实验室改用**固定端口**(从 46000 起找空位并落盘),不再每次启动让系统随机分配:
|
|
11
|
+
端口一变就是换 origin,浏览器会把登录态与页面状态全部丢掉,内嵌视图因此每次重启都要重来。
|
|
12
|
+
端口被占时自动换一个并写回记录
|
|
13
|
+
|
|
14
|
+
## [0.1.2] - 2026-11-21
|
|
15
|
+
|
|
16
|
+
### Added
|
|
17
|
+
|
|
18
|
+
- `plugin_lab` 新增 `orphans` / `reap`:列出并回收宿主被硬杀后残留的实验室进程
|
|
19
|
+
- HTTP 边新增 `GET /orphans` 与 `POST /orphans/reap`(只列不杀是默认,回收要显式请求)
|
|
20
|
+
- 回收的判据只有一条:命令行带 `--profile rig-<已知 lab>` 但当前没在管理的进程,
|
|
21
|
+
不按端口或进程名杀
|
|
22
|
+
|
|
6
23
|
## [0.1.1] - 2026-11-21
|
|
7
24
|
|
|
8
25
|
### Fixed
|
package/lib/http/routes.js
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
* 这不是强安全边界,是防「别的网页顺手打你的实验室」。真正的边界是「它只在本机」。
|
|
12
12
|
*/
|
|
13
13
|
import { API_PREFIX } from '../lab/constants.js'
|
|
14
|
+
import { reapOrphans } from '../lab/orphans.js'
|
|
14
15
|
|
|
15
16
|
const MAX_BODY_BYTES = 64 * 1024
|
|
16
17
|
|
|
@@ -193,6 +194,19 @@ export function mountRoutes(ctx, { supervisor, versions, frameSig }) {
|
|
|
193
194
|
json(res, 200, { result, labs: supervisor.list() })
|
|
194
195
|
return
|
|
195
196
|
}
|
|
197
|
+
// 孤儿实验室:宿主被硬杀时留下的进程。只列不杀,除非显式要求回收。
|
|
198
|
+
if (method === 'GET' && tail === '/orphans') {
|
|
199
|
+
const tracked = supervisor.list().filter((lab) => lab.status === 'running' || lab.status === 'starting')
|
|
200
|
+
const result = await reapOrphans({ trackedProfileNames: tracked.map((lab) => lab.profile), dryRun: true })
|
|
201
|
+
json(res, 200, { orphans: result.found })
|
|
202
|
+
return
|
|
203
|
+
}
|
|
204
|
+
if (method === 'POST' && tail === '/orphans/reap') {
|
|
205
|
+
const tracked = supervisor.list().filter((lab) => lab.status === 'running' || lab.status === 'starting')
|
|
206
|
+
const result = await reapOrphans({ trackedProfileNames: tracked.map((lab) => lab.profile) })
|
|
207
|
+
json(res, 200, result)
|
|
208
|
+
return
|
|
209
|
+
}
|
|
196
210
|
// 界面驱动:让智能体像人一样看/拍/点/打(面板与工具共用这条)
|
|
197
211
|
const reportTarget = /^\/labs\/([a-z0-9-]+)\/report$/u.exec(tail)
|
|
198
212
|
if (method === 'GET' && reportTarget) {
|
package/lib/lab/constants.js
CHANGED
|
@@ -20,6 +20,8 @@ export const DEFAULTS = {
|
|
|
20
20
|
logRingLines: 2_000,
|
|
21
21
|
/** 同时存在的实验室上限(每个都是一个 node 进程 + 一份 DSH_HOME)。 */
|
|
22
22
|
maxLabs: 8,
|
|
23
|
+
/** 实验室的端口从这里开始找空位:固定端口 = 固定 origin,浏览器登录态与内嵌视图才不会每次重启作废。 */
|
|
24
|
+
basePort: 46_000,
|
|
23
25
|
/** 远端版本清单缓存时长。 */
|
|
24
26
|
versionsCacheMs: 60 * 60 * 1_000,
|
|
25
27
|
/** 复制宿主凭据到实验室(实验室里的智能体才有模型可用)。 */
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 孤儿回收(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 为什么需要它:宿主被硬杀(崩溃、任务管理器结束、`taskkill /F`)时,插件没有机会跑
|
|
5
|
+
* `disposeAll`,于是它派出去的实验室进程会活下来——2026-11-21 桌面版崩过一次之后,
|
|
6
|
+
* 就留下一个 307 MB、还占着端口的实验室进程。
|
|
7
|
+
*
|
|
8
|
+
* 判据只有一条:**命令行里带 `--profile rig-<已知 lab id>` 但我们当前没在管理的进程**,
|
|
9
|
+
* 就是孤儿。不按端口杀、不按进程名杀 —— 那些会误伤。
|
|
10
|
+
*
|
|
11
|
+
* Windows 上没有 `/proc`,只能问 WMI。用固定脚本(不经用户输入拼接),失败返回空数组:
|
|
12
|
+
* 回收是「尽力而为」的事,不该因为它失败就影响插件启动。
|
|
13
|
+
*/
|
|
14
|
+
import { execFile } from 'node:child_process'
|
|
15
|
+
import { promisify } from 'node:util'
|
|
16
|
+
|
|
17
|
+
const run = promisify(execFile)
|
|
18
|
+
|
|
19
|
+
/** 只认这些 profile 前缀:与 naming.js 里的 PROFILE_PREFIX 保持一致。 */
|
|
20
|
+
const PROFILE_PATTERN = /--profile\s+(rig-[a-z0-9-]+)/u
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 列出所有跑着 rig 实验室的进程。
|
|
24
|
+
* @returns {Promise<Array<{pid: number, profile: string, cmd: string}>>}
|
|
25
|
+
*/
|
|
26
|
+
export async function listLabProcesses() {
|
|
27
|
+
if (process.platform !== 'win32') return []
|
|
28
|
+
const script = [
|
|
29
|
+
'$out = @()',
|
|
30
|
+
'Get-CimInstance Win32_Process -Filter "Name=\'node.exe\'" | ForEach-Object {',
|
|
31
|
+
' if ($_.CommandLine -match "--profile rig-") { $out += [pscustomobject]@{ pid = $_.ProcessId; cmd = $_.CommandLine } }',
|
|
32
|
+
'}',
|
|
33
|
+
'Get-CimInstance Win32_Process -Filter "Name=\'DeepSeek Harness.exe\'" | ForEach-Object {',
|
|
34
|
+
' if ($_.CommandLine -match "--profile rig-") { $out += [pscustomobject]@{ pid = $_.ProcessId; cmd = $_.CommandLine } }',
|
|
35
|
+
'}',
|
|
36
|
+
'$out | ConvertTo-Json -Compress',
|
|
37
|
+
].join('\n')
|
|
38
|
+
|
|
39
|
+
try {
|
|
40
|
+
const { stdout } = await run(
|
|
41
|
+
'pwsh',
|
|
42
|
+
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', script],
|
|
43
|
+
{ timeout: 20_000, windowsHide: true, maxBuffer: 2 * 1024 * 1024 },
|
|
44
|
+
)
|
|
45
|
+
const text = stdout.trim()
|
|
46
|
+
if (!text || text === 'null') return []
|
|
47
|
+
const parsed = JSON.parse(text)
|
|
48
|
+
const rows = Array.isArray(parsed) ? parsed : [parsed]
|
|
49
|
+
return rows
|
|
50
|
+
.map((row) => {
|
|
51
|
+
const match = PROFILE_PATTERN.exec(String(row.cmd || ''))
|
|
52
|
+
return match ? { pid: Number(row.pid), profile: match[1], cmd: String(row.cmd) } : null
|
|
53
|
+
})
|
|
54
|
+
.filter(Boolean)
|
|
55
|
+
} catch {
|
|
56
|
+
return []
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** 杀掉一棵进程树(只用于我们确认过的孤儿 pid)。 */
|
|
61
|
+
export async function killOrphan(pid) {
|
|
62
|
+
if (!Number.isInteger(pid) || pid <= 0) throw new Error(`非法 pid:${pid}`)
|
|
63
|
+
try {
|
|
64
|
+
await run('taskkill', ['/PID', String(pid), '/T', '/F'], { timeout: 15_000, windowsHide: true })
|
|
65
|
+
return true
|
|
66
|
+
} catch {
|
|
67
|
+
return false
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* 回收:把「跑着 rig 实验室、但不在 tracked 名单里」的进程杀掉。
|
|
73
|
+
*
|
|
74
|
+
* @param {{trackedProfileNames: string[], dryRun?: boolean}} options
|
|
75
|
+
* @returns {Promise<{found: Array<object>, killed: Array<object>, failed: Array<object>}>}
|
|
76
|
+
*/
|
|
77
|
+
export async function reapOrphans({ trackedProfileNames, dryRun = false }) {
|
|
78
|
+
const tracked = new Set(trackedProfileNames || [])
|
|
79
|
+
const all = await listLabProcesses()
|
|
80
|
+
const orphans = all.filter((item) => !tracked.has(item.profile))
|
|
81
|
+
const killed = []
|
|
82
|
+
const failed = []
|
|
83
|
+
if (dryRun) return { found: orphans, killed, failed }
|
|
84
|
+
for (const orphan of orphans) {
|
|
85
|
+
if (await killOrphan(orphan.pid)) killed.push(orphan)
|
|
86
|
+
else failed.push(orphan)
|
|
87
|
+
}
|
|
88
|
+
return { found: orphans, killed, failed }
|
|
89
|
+
}
|
package/lib/lab/port.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 端口(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 为什么值得单独管:实验室原来用 `--port 0` 让系统随机分配,于是**每启动一次都换一个 origin**。
|
|
5
|
+
* 对浏览器来说换 origin 就是换一个世界:localStorage / sessionStorage / cookie 全部重来,
|
|
6
|
+
* 而 DSH 的网页端会把登录态记在自己的 origin 下 —— 这就是「重启之后内嵌视图要我重新登录」的根源。
|
|
7
|
+
* 固定端口把它变成同一个 origin,重启只是刷一次页面,不再丢登录态。
|
|
8
|
+
*/
|
|
9
|
+
import { createServer } from 'node:net'
|
|
10
|
+
|
|
11
|
+
/** 这个端口现在能不能占(bind 一下再释放)。 */
|
|
12
|
+
export function isPortFree(port) {
|
|
13
|
+
return new Promise((resolve) => {
|
|
14
|
+
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
|
15
|
+
resolve(false)
|
|
16
|
+
return
|
|
17
|
+
}
|
|
18
|
+
const server = createServer()
|
|
19
|
+
server.once('error', () => resolve(false))
|
|
20
|
+
server.once('listening', () => server.close(() => resolve(true)))
|
|
21
|
+
server.listen(port, '127.0.0.1')
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* 从 start 开始找一个空闲端口(最多试 span 个)。
|
|
27
|
+
* @returns {Promise<number|null>} 找不到返回 null,由调用方回落 `--port 0`
|
|
28
|
+
*/
|
|
29
|
+
export async function pickFreePort(start, span = 40) {
|
|
30
|
+
for (let port = start; port < start + span; port += 1) {
|
|
31
|
+
if (await isPortFree(port)) return port
|
|
32
|
+
}
|
|
33
|
+
return null
|
|
34
|
+
}
|
package/lib/lab/registry.js
CHANGED
|
@@ -37,6 +37,8 @@ export function parseState(text) {
|
|
|
37
37
|
name: typeof lab.name === 'string' ? lab.name : lab.id,
|
|
38
38
|
version: typeof lab.version === 'string' ? lab.version : 'host',
|
|
39
39
|
profile: typeof lab.profile === 'string' ? lab.profile : profileNameFor(lab.id),
|
|
40
|
+
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
41
|
+
ownerSessionId: typeof lab.ownerSessionId === 'string' ? lab.ownerSessionId : null,
|
|
40
42
|
createdAt: typeof lab.createdAt === 'string' ? lab.createdAt : null,
|
|
41
43
|
}))
|
|
42
44
|
return { state: { schemaVersion: SCHEMA_VERSION, labs }, problem: null }
|
|
@@ -59,6 +61,7 @@ export function addLab(state, lab) {
|
|
|
59
61
|
name: lab.name,
|
|
60
62
|
version: lab.version,
|
|
61
63
|
profile: lab.profile,
|
|
64
|
+
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
62
65
|
createdAt: lab.createdAt || new Date().toISOString(),
|
|
63
66
|
})
|
|
64
67
|
return state
|
|
@@ -79,11 +82,12 @@ export function describeLab(lab, runtime) {
|
|
|
79
82
|
profile: lab.profile,
|
|
80
83
|
createdAt: lab.createdAt,
|
|
81
84
|
status: live.status || STATUS.created,
|
|
82
|
-
port: live.port ?? null,
|
|
85
|
+
port: live.port ?? lab.port ?? null,
|
|
83
86
|
url: live.url || null,
|
|
84
87
|
token: live.token || null,
|
|
85
88
|
pid: live.pid ?? null,
|
|
86
89
|
startedAt: live.startedAt || null,
|
|
87
90
|
exit: live.exit || null,
|
|
91
|
+
ownerSessionId: lab.ownerSessionId || null,
|
|
88
92
|
}
|
|
89
93
|
}
|
package/lib/lab/spawn-plan.js
CHANGED
|
@@ -74,7 +74,7 @@ export function sanitizeEnv(source, extra = {}) {
|
|
|
74
74
|
* @param {Record<string,string|undefined>} p.env 宿主 env
|
|
75
75
|
* @param {string} [p.version] 记入日志用,不进 argv
|
|
76
76
|
*/
|
|
77
|
-
export function buildStartPlan({ execPath, isElectron, coreBin, profile, cwd, home, env }) {
|
|
77
|
+
export function buildStartPlan({ execPath, isElectron, coreBin, profile, cwd, home, env, port = 0 }) {
|
|
78
78
|
if (!execPath) throw new Error('buildStartPlan: 缺少 execPath')
|
|
79
79
|
if (!coreBin) throw new Error('buildStartPlan: 缺少 coreBin')
|
|
80
80
|
if (!profile) throw new Error('buildStartPlan: 缺少 profile')
|
|
@@ -82,8 +82,8 @@ export function buildStartPlan({ execPath, isElectron, coreBin, profile, cwd, ho
|
|
|
82
82
|
// ⚠️ 不能写 `web` 子命令:`dsh web` 本身就是 `--profile web` 的别名,
|
|
83
83
|
// 再叠一个 `--profile rig-xxx` 会被 CLI 判成「select a profile only once」并直接退出
|
|
84
84
|
// (2026-11-21 实测:exit 1,面板显示「已崩溃」)。
|
|
85
|
-
//
|
|
86
|
-
const args = [coreBin, '--profile', profile, '--port',
|
|
85
|
+
// 端口默认给固定的(见 port.js):origin 稳定,浏览器登录态与面板内嵌才不会每次重启都作废。
|
|
86
|
+
const args = [coreBin, '--profile', profile, '--port', String(port || 0), '--no-open', '--host', '127.0.0.1']
|
|
87
87
|
const extra = {
|
|
88
88
|
DSH_HOME: home,
|
|
89
89
|
DSH_TELEMETRY_DISABLED: '1',
|
package/lib/lab/supervisor.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-rig · 进程看管(IO 层 · 唯一写盘点)
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* 这里只负责把它们串起来,并保证**所有落盘与杀进程都只发生在本文件**。
|
|
4
|
+
* 只做三件事:把实验室拉起来、看着它、把它收干净。拼命令在 `spawn-plan.js`、判隔离在
|
|
5
|
+
* `naming.js`、日志在 `log-buffer.js`、瞬时状态在 `runtime.js`、端口在 `port.js`、探针在
|
|
6
|
+
* `probe.js`、一次性命令在 `seed.js`、界面驱动在 `ui-session.js`、版本切换在 `versioning.js`。
|
|
8
7
|
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* 常驻轮询会让「卡住」和「在忙」长得一样,反而更难判断。
|
|
12
|
-
* - 不持久化端口/token/pid:端口每次由系统分配、token 每次现生成。
|
|
8
|
+
* 刻意不做:常驻健康轮询(只在启动阶段探到活,之后靠 `exit` 判死 —— 否则「卡住」和「在忙」
|
|
9
|
+
* 长得一样);不持久化 token / pid(端口固定并落盘,为了 origin 稳定)。
|
|
13
10
|
*/
|
|
14
11
|
import { mkdir, readFile, writeFile, rename, rm } from 'node:fs/promises'
|
|
15
12
|
import { join } from 'node:path'
|
|
@@ -18,7 +15,9 @@ import { labPaths } from './layout.js'
|
|
|
18
15
|
import { assertIsolated, isValidLabId, nextLabId, profileNameFor } from './naming.js'
|
|
19
16
|
import { buildSeedPlan, buildStartPlan, parseLabUrl } from './spawn-plan.js'
|
|
20
17
|
import { buildDiagnostics } from './diagnostics.js'
|
|
18
|
+
import { isPortFree, pickFreePort } from './port.js'
|
|
21
19
|
import { resolveCoreBin } from './core-bin.js'
|
|
20
|
+
import { planVersionChange } from './versioning.js'
|
|
22
21
|
import { copyCredentials, runOnce } from './seed.js'
|
|
23
22
|
import { attachLines, killTree, spawnChild } from './process.js'
|
|
24
23
|
import { probeOnce } from './probe.js'
|
|
@@ -152,10 +151,7 @@ export class LabSupervisor {
|
|
|
152
151
|
return next
|
|
153
152
|
}
|
|
154
153
|
|
|
155
|
-
/**
|
|
156
|
-
* 看 / 拍 / 点 / 打 / 跑任务。不碰实验室内部服务,只驱动它的界面。
|
|
157
|
-
* 这样它对界面改版是**脆**的 —— 所以按文字找元素、找不到就报候选,而不是点空。
|
|
158
|
-
*/
|
|
154
|
+
/** 看 / 拍 / 点 / 打 / 跑任务。只驱动界面,不碰实验室内部服务 —— 所以按文字找元素、找不到报候选。 */
|
|
159
155
|
async ui(id, action, args = {}) {
|
|
160
156
|
if (!this.#uiMode) throw new Error('智能体操作界面未开启:先在面板里打开「允许智能体操作」')
|
|
161
157
|
const lab = findLab(this.#state, id)
|
|
@@ -186,6 +182,7 @@ export class LabSupervisor {
|
|
|
186
182
|
name: typeof name === 'string' && name.trim() !== '' ? name.trim().slice(0, 40) : `实验台 ${id}`,
|
|
187
183
|
version: typeof version === 'string' ? version : 'host',
|
|
188
184
|
profile: profileNameFor(id),
|
|
185
|
+
port: await pickFreePort(this.#config.basePort + this.#state.labs.length), // 固定端口 = 固定 origin(见 port.js)
|
|
189
186
|
createdAt: new Date().toISOString(),
|
|
190
187
|
}
|
|
191
188
|
this.#rt.of(id)
|
|
@@ -250,6 +247,12 @@ export class LabSupervisor {
|
|
|
250
247
|
await mkdir(paths.home, { recursive: true })
|
|
251
248
|
await mkdir(paths.workspace, { recursive: true })
|
|
252
249
|
|
|
250
|
+
// 端口被占了就换一个并落盘 —— 只把「确认空闲的端口」交给子进程,解析出来的端口才稳定等于这条记录。
|
|
251
|
+
if (!lab.port || !(await isPortFree(lab.port))) {
|
|
252
|
+
lab.port = await pickFreePort(this.#config.basePort)
|
|
253
|
+
await this.#save()
|
|
254
|
+
}
|
|
255
|
+
|
|
253
256
|
const plan = buildStartPlan({
|
|
254
257
|
execPath: this.#execPath,
|
|
255
258
|
isElectron: this.#isElectron,
|
|
@@ -258,6 +261,7 @@ export class LabSupervisor {
|
|
|
258
261
|
cwd: paths.workspace,
|
|
259
262
|
home: paths.home,
|
|
260
263
|
env: this.#env,
|
|
264
|
+
port: lab.port || 0,
|
|
261
265
|
})
|
|
262
266
|
|
|
263
267
|
rt.exit = null
|
|
@@ -278,8 +282,7 @@ export class LabSupervisor {
|
|
|
278
282
|
rt.child = child
|
|
279
283
|
rt.pid = child.pid ?? null
|
|
280
284
|
|
|
281
|
-
// 注意签名:
|
|
282
|
-
// 2026-11-21 重构时这里错成柯里化,结果实验室的输出一行都没被接收,看着像「启动卡死」。
|
|
285
|
+
// 注意签名:attachLines 回调是 (stream, line),不是柯里化 —— 2026-11-21 错配导致实验室输出全被吞。
|
|
283
286
|
attachLines(child, (stream, line) => {
|
|
284
287
|
if (!this.#rt.pushLog(id, stream, line)) return
|
|
285
288
|
const parsed = parseLabUrl(line)
|
|
@@ -370,10 +373,7 @@ export class LabSupervisor {
|
|
|
370
373
|
async setVersion(id, version) {
|
|
371
374
|
const lab = findLab(this.#state, id)
|
|
372
375
|
if (!lab) throw new Error(`没有实验室 ${id}`)
|
|
373
|
-
const next =
|
|
374
|
-
// 切到某个具体版本前先确认它真的装着:否则重启时才发现,等于把失败推给下一次启动。
|
|
375
|
-
if (next !== 'host') await resolveCoreBin({ version: next, root: this.#root, env: this.#env })
|
|
376
|
-
lab.version = next
|
|
376
|
+
const next = await planVersionChange({ lab, version, root: this.#root, env: this.#env })
|
|
377
377
|
await this.#save()
|
|
378
378
|
this.#rt.emit({ type: 'labs', at: new Date().toISOString() })
|
|
379
379
|
this.#rt.pushLog(id, 'rig', `版本已切换为 ${next}(重启后生效)`)
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 版本切换(薄逻辑)
|
|
3
|
+
*
|
|
4
|
+
* 单独成文件的原因:它要「先确认那个版本真的装着,再改记录」,是**先后顺序**的约束,
|
|
5
|
+
* 跟生命周期不是一类东西;留在看管者里会把那个本就顶到行数上限的文件继续撑大。
|
|
6
|
+
*/
|
|
7
|
+
import { resolveCoreBin } from './core-bin.js'
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 决定并写回 lab.version。装不上的版本在这里就抛,别把失败推给下一次启动。
|
|
11
|
+
* @returns {Promise<string>} 生效的版本号(`host` 表示跟随宿主)
|
|
12
|
+
*/
|
|
13
|
+
export async function planVersionChange({ lab, version, root, env }) {
|
|
14
|
+
const next = typeof version === 'string' && version.trim() !== '' ? version.trim() : 'host'
|
|
15
|
+
if (next !== 'host') await resolveCoreBin({ version: next, root, env })
|
|
16
|
+
lab.version = next
|
|
17
|
+
return next
|
|
18
|
+
}
|
package/lib/tools/lab-tool.js
CHANGED
|
@@ -14,7 +14,7 @@ import { renderReport } from '../lab/report.js'
|
|
|
14
14
|
|
|
15
15
|
const text = (s) => [{ type: 'text', text: String(s ?? '') }]
|
|
16
16
|
|
|
17
|
-
const ACTIONS = ['list', 'create', 'start', 'stop', 'restart', 'destroy', 'status', 'logs', 'report', 'versions', 'set_version']
|
|
17
|
+
const ACTIONS = ['list', 'create', 'start', 'stop', 'restart', 'destroy', 'status', 'logs', 'report', 'versions', 'set_version', 'orphans', 'reap']
|
|
18
18
|
|
|
19
19
|
function renderLab(lab) {
|
|
20
20
|
const head = `[${lab.id}] ${lab.name} · ${lab.status}${lab.port ? ` · :${lab.port}` : ''}`
|
|
@@ -100,6 +100,22 @@ export function createLabTool(supervisor, versionsApi) {
|
|
|
100
100
|
)
|
|
101
101
|
return { ok: true, text: lines.join('\n') }
|
|
102
102
|
}
|
|
103
|
+
if (action === 'orphans' || action === 'reap') {
|
|
104
|
+
const tracked = supervisor.list().filter((lab) => lab.status === 'running' || lab.status === 'starting')
|
|
105
|
+
const result = await reapOrphans({
|
|
106
|
+
trackedProfileNames: tracked.map((lab) => lab.profile),
|
|
107
|
+
dryRun: action === 'orphans',
|
|
108
|
+
})
|
|
109
|
+
if (result.found.length === 0) return { ok: true, text: '没有残留的实验室进程。' }
|
|
110
|
+
const lines = result.found.map((item) => `${item.pid} · ${item.profile}`)
|
|
111
|
+
if (action === 'orphans') {
|
|
112
|
+
return { ok: true, text: `发现 ${result.found.length} 个残留实验室进程(未处理):\n${lines.join('\n')}\n要清掉就把 action 换成 reap。` }
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
ok: true,
|
|
116
|
+
text: `回收完成:清掉 ${result.killed.length} 个${result.failed.length ? `,失败 ${result.failed.length} 个` : ''}\n${lines.join('\n')}`,
|
|
117
|
+
}
|
|
118
|
+
}
|
|
103
119
|
if (action === 'set_version') {
|
|
104
120
|
if (!args?.version) return { ok: false, error: 'set_version 需要 version(host 表示跟随宿主)' }
|
|
105
121
|
const lab = await supervisor.setVersion(id, String(args.version))
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-rig",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "Right-sidebar plugin lab for DeepSeek Harness: a real, process-isolated DSH you can start, restart, version-switch, lock or take over — crashes stay inside the lab.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|