dsh-rig 0.1.1 → 0.1.2

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 CHANGED
@@ -3,6 +3,15 @@
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.2] - 2026-11-21
7
+
8
+ ### Added
9
+
10
+ - `plugin_lab` 新增 `orphans` / `reap`:列出并回收宿主被硬杀后残留的实验室进程
11
+ - HTTP 边新增 `GET /orphans` 与 `POST /orphans/reap`(只列不杀是默认,回收要显式请求)
12
+ - 回收的判据只有一条:命令行带 `--profile rig-<已知 lab>` 但当前没在管理的进程,
13
+ 不按端口或进程名杀
14
+
6
15
  ## [0.1.1] - 2026-11-21
7
16
 
8
17
  ### Fixed
@@ -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) {
@@ -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
+ }
@@ -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.1",
3
+ "version": "0.1.2",
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",