dsh-rig 0.3.0 → 0.4.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/CHANGELOG.md +16 -0
- package/lib/client.js +43 -1
- package/lib/http/routes.js +18 -1
- package/lib/lab/constants.js +15 -0
- package/lib/lab/limit-guard.js +59 -0
- package/lib/lab/limits.js +42 -0
- package/lib/lab/registry.js +4 -0
- package/lib/lab/runtime.js +6 -0
- package/lib/lab/supervisor.js +17 -17
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
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.4.0] - 2026-11-24
|
|
7
|
+
|
|
8
|
+
### Added
|
|
9
|
+
|
|
10
|
+
- **内存上限滑块**(面板上,512 MB – 16 GB,步长 256 MB,默认 2 GB)。拖完停手 250ms 才落盘,
|
|
11
|
+
下次启动沿用;旁边实时显示当前占用(`内存上限 [──●──] 2048 MB · 现 273 MB`)。
|
|
12
|
+
- 宿主每 5 秒采样一次实验室进程的常驻内存(Windows `tasklist` / POSIX `ps`,零新依赖),
|
|
13
|
+
**超过上限先记日志再停掉实验室** —— 这是「实验室能把宿主拖死」唯一没防住的那条路。
|
|
14
|
+
- 采样值走运行时表(不落盘),随 `/state` 返回。
|
|
15
|
+
|
|
16
|
+
### Verified
|
|
17
|
+
|
|
18
|
+
- 真机:实验室起来后采样到 273 MB;把上限压到 100 MB,一轮巡检内被停,
|
|
19
|
+
日志写明「内存 273 MB 超过 100 MB,已停;面板滑块可放宽」。
|
|
20
|
+
- `npm test` 68 条全绿。
|
|
21
|
+
|
|
6
22
|
## [0.3.0] - 2026-11-24
|
|
7
23
|
|
|
8
24
|
### Changed
|
package/lib/client.js
CHANGED
|
@@ -136,7 +136,7 @@ window.__ModuleLoader__.load({
|
|
|
136
136
|
|
|
137
137
|
// ── 5. 页级 store:一条 SSE 常驻,面板只是订阅者 ─────────────────
|
|
138
138
|
function createStore() {
|
|
139
|
-
const state = { labs: [], logs: {}, connected: false, error: '', uiMode: false, ui: null, frameSig: '', frameGen: 0 }
|
|
139
|
+
const state = { labs: [], logs: {}, connected: false, error: '', uiMode: false, ui: null, memory: null, frameSig: '', frameGen: 0 }
|
|
140
140
|
const listeners = new Set()
|
|
141
141
|
let controller = null
|
|
142
142
|
let retry = null
|
|
@@ -155,6 +155,7 @@ window.__ModuleLoader__.load({
|
|
|
155
155
|
...(typeof data.uiMode === 'boolean' ? { uiMode: data.uiMode } : {}),
|
|
156
156
|
...(data.ui ? { ui: data.ui } : {}),
|
|
157
157
|
...(typeof data.frameSig === 'string' ? { frameSig: data.frameSig } : {}),
|
|
158
|
+
...(data.memory ? { memory: data.memory } : {}),
|
|
158
159
|
})
|
|
159
160
|
const pushLog = (id, entry) => {
|
|
160
161
|
const list = (state.logs[id] || []).concat(entry)
|
|
@@ -310,6 +311,34 @@ window.__ModuleLoader__.load({
|
|
|
310
311
|
}
|
|
311
312
|
|
|
312
313
|
// ── 7. 三带组件 ────────────────────────────────────────────────
|
|
314
|
+
/** 内存上限:滑块改了就落盘;越过上限时宿主先记日志再停掉实验室。 */
|
|
315
|
+
function MemoryBar({ value, rss, range, onChange }) {
|
|
316
|
+
const min = (range && range.min) || 512
|
|
317
|
+
const max = (range && range.max) || 16384
|
|
318
|
+
const now = typeof rss === 'number' ? ` · 现 ${rss} MB` : ''
|
|
319
|
+
return h('div', { style: S.band },
|
|
320
|
+
h('span', { style: S.note }, '内存上限'),
|
|
321
|
+
h('input', {
|
|
322
|
+
type: 'range', min, max, step: 256, value,
|
|
323
|
+
style: { flex: '1 1 80px', minWidth: 60, accentColor: 'var(--dsw-alias-brand-primary)' },
|
|
324
|
+
onChange: (event) => onChange(Number(event.currentTarget.value)),
|
|
325
|
+
title: `超过 ${value} MB 会自动停掉实验室`,
|
|
326
|
+
}),
|
|
327
|
+
h('span', { style: S.note }, `${value} MB${now}`))
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** 日志常驻区:占满剩余高度,自动滚到最新。这面板没有画布了,日志就是它的主体。 */
|
|
331
|
+
function LogPanel({ entries }) {
|
|
332
|
+
const ref = useRef(null)
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
const el = ref.current
|
|
335
|
+
if (el) el.scrollTop = el.scrollHeight
|
|
336
|
+
}, [entries.length])
|
|
337
|
+
if (!entries.length) return h('div', { ref, style: S.logbox }, h('div', { style: S.note }, T.logs.empty))
|
|
338
|
+
return h('div', { ref, style: S.logbox },
|
|
339
|
+
...entries.slice(-LOG_TAIL).map((entry) => h('div', { key: entry.seq, style: { ...S.line, ...(entry.stream === 'err' ? S.lineErr : {}) } }, entry.text)))
|
|
340
|
+
}
|
|
341
|
+
|
|
313
342
|
function EmptyCard({ busy, onCreate }) {
|
|
314
343
|
return h('div', { style: S.viewport },
|
|
315
344
|
h('div', { style: S.idle },
|
|
@@ -328,6 +357,8 @@ window.__ModuleLoader__.load({
|
|
|
328
357
|
const byId = props && typeof props.useSessions === 'function' ? props.useSessions((state) => state.byId) : null
|
|
329
358
|
const [busy, setBusy] = useState('')
|
|
330
359
|
const [confirming, setConfirming] = useState(false)
|
|
360
|
+
const [draftLimit, setDraftLimit] = useState(null)
|
|
361
|
+
const limitTimer = useRef(null)
|
|
331
362
|
|
|
332
363
|
const lab = view.labs.length > 0 ? view.labs[0] : null
|
|
333
364
|
const entries = lab ? view.logs[lab.id] || [] : []
|
|
@@ -338,6 +369,16 @@ window.__ModuleLoader__.load({
|
|
|
338
369
|
const foreign = Boolean(lab && lab.foreign)
|
|
339
370
|
const mine = !foreign && (!lab || !lab.ownerSessionId || sessionId === '' || lab.ownerSessionId === sessionId)
|
|
340
371
|
const canView = Boolean(lab && lab.url)
|
|
372
|
+
const fallbackLimit = (view.memory && view.memory.fallback) || 2048
|
|
373
|
+
const shownLimit = draftLimit ?? (lab && lab.memoryLimitMb) ?? fallbackLimit
|
|
374
|
+
/** 滑块拖动时先本地反馈,停手 250ms 才落盘(每一格都发请求会打爆宿主)。 */
|
|
375
|
+
const commitLimit = (mb) => {
|
|
376
|
+
setDraftLimit(mb)
|
|
377
|
+
if (limitTimer.current) clearTimeout(limitTimer.current)
|
|
378
|
+
limitTimer.current = setTimeout(() => {
|
|
379
|
+
void run('限额', () => post(`/labs/${lab.id}/limit`, { memoryMb: mb, sessionId }))
|
|
380
|
+
}, 250)
|
|
381
|
+
}
|
|
341
382
|
|
|
342
383
|
const run = async (label, fn) => {
|
|
343
384
|
setBusy(label)
|
|
@@ -407,6 +448,7 @@ window.__ModuleLoader__.load({
|
|
|
407
448
|
}, view.uiMode ? T.agent.on : T.agent.off),
|
|
408
449
|
h('span', { style: S.grow }),
|
|
409
450
|
lab ? h('span', { style: S.note }, `${T.logs.title} · ${entries.length}`) : null),
|
|
451
|
+
lab ? h(MemoryBar, { value: shownLimit, rss: lab.rssMb, range: view.memory, onChange: commitLimit }) : null,
|
|
410
452
|
view.error ? h('div', { style: S.error }, view.error) : null,
|
|
411
453
|
lab ? h(LogPanel, { entries }) : h(EmptyCard, { busy, onCreate: create }),
|
|
412
454
|
)
|
package/lib/http/routes.js
CHANGED
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
* - 服务只绑 127.0.0.1(宿主本来就只监听回环)。
|
|
11
11
|
* 这不是强安全边界,是防「别的网页顺手打你的实验室」。真正的边界是「它只在本机」。
|
|
12
12
|
*/
|
|
13
|
-
import { API_PREFIX } from '../lab/constants.js'
|
|
13
|
+
import { API_PREFIX, DEFAULTS, clampMemoryMb } from '../lab/constants.js'
|
|
14
14
|
import { reapOrphans } from '../lab/orphans.js'
|
|
15
15
|
import { claimOwner, releaseOwner } from '../lab/ownership.js'
|
|
16
16
|
import { guardDrive, isDriveAction } from '../lab/drive-guard.js'
|
|
@@ -198,6 +198,7 @@ export function mountRoutes(ctx, { supervisor, frameSig, titleOf }) {
|
|
|
198
198
|
ui,
|
|
199
199
|
uiMode: supervisor.uiMode(),
|
|
200
200
|
frameSig,
|
|
201
|
+
memory: { min: DEFAULTS.memoryLimitMinMb, max: DEFAULTS.memoryLimitMaxMb, fallback: DEFAULTS.memoryLimitDefaultMb },
|
|
201
202
|
})
|
|
202
203
|
return
|
|
203
204
|
}
|
|
@@ -283,6 +284,22 @@ export function mountRoutes(ctx, { supervisor, frameSig, titleOf }) {
|
|
|
283
284
|
json(res, 200, { report: await supervisor.report(reportTarget[1]) })
|
|
284
285
|
return
|
|
285
286
|
}
|
|
287
|
+
const limitTarget = /^\/labs\/([a-z0-9-]+)\/limit$/u.exec(tail)
|
|
288
|
+
if (method === 'POST' && limitTarget) {
|
|
289
|
+
const body = await readBody(req)
|
|
290
|
+
if (!(await requireDrive(res, limitTarget[1], body))) return
|
|
291
|
+
const mb = clampMemoryMb(body.memoryMb)
|
|
292
|
+
if (mb === null) {
|
|
293
|
+
json(res, 400, { error: 'memoryMb 需要是数字' })
|
|
294
|
+
return
|
|
295
|
+
}
|
|
296
|
+
const lab = await supervisor.mutate(limitTarget[1], (record) => {
|
|
297
|
+
record.memoryLimitMb = mb
|
|
298
|
+
return record
|
|
299
|
+
})
|
|
300
|
+
json(res, 200, { lab, labs: describe(supervisor.list()) })
|
|
301
|
+
return
|
|
302
|
+
}
|
|
286
303
|
const uiTarget = /^\/labs\/([a-z0-9-]+)\/ui$/u.exec(tail)
|
|
287
304
|
if (method === 'POST' && uiTarget) {
|
|
288
305
|
const body = await readBody(req)
|
package/lib/lab/constants.js
CHANGED
|
@@ -22,6 +22,14 @@ export const DEFAULTS = {
|
|
|
22
22
|
maxLabs: 8,
|
|
23
23
|
/** 实验室的端口从这里开始找空位:固定端口 = 固定 origin,浏览器登录态与内嵌视图才不会每次重启作废。 */
|
|
24
24
|
basePort: 46_000,
|
|
25
|
+
/** 内存上限默认值(MB)。超了先记日志再停 —— 实验室吃满内存时宿主会跟着卡。 */
|
|
26
|
+
memoryLimitDefaultMb: 2_048,
|
|
27
|
+
/** 面板上滑块的下限(再低 DSH 起不来)。 */
|
|
28
|
+
memoryLimitMinMb: 512,
|
|
29
|
+
/** 面板上滑块的上限。 */
|
|
30
|
+
memoryLimitMaxMb: 16_384,
|
|
31
|
+
/** 采样间隔:5 秒一次,足够早发现,又不至于一直开子进程。 */
|
|
32
|
+
limitCheckMs: 5_000,
|
|
25
33
|
/** 远端版本清单缓存时长。 */
|
|
26
34
|
versionsCacheMs: 60 * 60 * 1_000,
|
|
27
35
|
/** 复制宿主凭据到实验室(实验室里的智能体才有模型可用)。 */
|
|
@@ -68,3 +76,10 @@ export const STATUS_LABEL = {
|
|
|
68
76
|
stopped: '已停止',
|
|
69
77
|
crashed: '已崩溃',
|
|
70
78
|
}
|
|
79
|
+
|
|
80
|
+
/** 把面板传来的内存上限收进合法区间(滑块与输入框都可能给越界值)。 */
|
|
81
|
+
export function clampMemoryMb(value, min = DEFAULTS.memoryLimitMinMb, max = DEFAULTS.memoryLimitMaxMb) {
|
|
82
|
+
const n = Number(value)
|
|
83
|
+
if (!Number.isFinite(n)) return null
|
|
84
|
+
return Math.min(max, Math.max(min, Math.round(n)))
|
|
85
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 限额巡检(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 定时问一次「实验室现在吃多少内存」,超了就让看管者停掉它。
|
|
5
|
+
*
|
|
6
|
+
* 为什么这件事必须有:隔离做到了进程、`DSH_HOME`、端口、工作区,但**没做到内存** ——
|
|
7
|
+
* 实验室是同一个人的进程,它把内存吃满时宿主会跟着卡死。那是唯一一条「实验室能弄死宿主」的路。
|
|
8
|
+
*
|
|
9
|
+
* 为什么放在单独文件:supervisor 已经贴着行数上限,而巡检是个可以整个删掉不影响生命周期的附加件。
|
|
10
|
+
* 它只认 `targets()` 给的快照与两个回调,不认识实验室内部。
|
|
11
|
+
*/
|
|
12
|
+
import { DEFAULTS } from './constants.js'
|
|
13
|
+
import { rssMb } from './limits.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} p
|
|
17
|
+
* @param {() => Array<{id: string, pid: number|null, memoryLimitMb: number|null}>} p.targets 此刻在跑的实验室
|
|
18
|
+
* @param {(id: string, mb: number) => void} p.onSample 采样成功(给面板显示当前占用)
|
|
19
|
+
* @param {(lab: object, mb: number, limitMb: number) => Promise<void>} p.onExceed 超限
|
|
20
|
+
* @param {(line: string) => void} p.log
|
|
21
|
+
* @param {number} [p.intervalMs]
|
|
22
|
+
*/
|
|
23
|
+
export function startLimitGuard({ targets, onSample, onExceed, log, intervalMs = DEFAULTS.limitCheckMs }) {
|
|
24
|
+
let timer = null
|
|
25
|
+
let running = false
|
|
26
|
+
|
|
27
|
+
async function tick() {
|
|
28
|
+
// 上一轮还没跑完就跳过:`tasklist` 在慢机器上可能超过一个间隔。
|
|
29
|
+
if (running) return
|
|
30
|
+
running = true
|
|
31
|
+
try {
|
|
32
|
+
for (const lab of targets()) {
|
|
33
|
+
if (!lab || !lab.pid) continue
|
|
34
|
+
const mb = await rssMb(lab.pid)
|
|
35
|
+
if (mb === null) continue
|
|
36
|
+
onSample(lab.id, mb)
|
|
37
|
+
const limit = lab.memoryLimitMb || DEFAULTS.memoryLimitDefaultMb
|
|
38
|
+
if (mb > limit) await onExceed(lab, mb, limit)
|
|
39
|
+
}
|
|
40
|
+
} catch (err) {
|
|
41
|
+
log(`[dsh-rig] 限额巡检失败:${err?.message ?? err}`)
|
|
42
|
+
} finally {
|
|
43
|
+
running = false
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
timer = setInterval(() => {
|
|
48
|
+
void tick()
|
|
49
|
+
}, intervalMs)
|
|
50
|
+
// 宿主退出时别让这个定时器拖住进程。
|
|
51
|
+
if (timer && typeof timer.unref === 'function') timer.unref()
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
stop() {
|
|
55
|
+
if (timer) clearInterval(timer)
|
|
56
|
+
timer = null
|
|
57
|
+
},
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 资源采样(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 只回答一个问题:这个进程现在占多少常驻内存(MB)。
|
|
5
|
+
*
|
|
6
|
+
* 为什么不引第三方包:`tasklist` / `ps` 各一行就够,而去掉一个依赖比多写十行划算 ——
|
|
7
|
+
* 这个插件已经够重了,不该为一次采样再挂个 native addon。
|
|
8
|
+
*
|
|
9
|
+
* 采样失败一律返回 null:进程刚好退出、权限不够、命令行区域设置变了,都不该影响主流程。
|
|
10
|
+
*/
|
|
11
|
+
import { execFile } from 'node:child_process'
|
|
12
|
+
import { promisify } from 'node:util'
|
|
13
|
+
|
|
14
|
+
const execFileAsync = promisify(execFile)
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {number} pid
|
|
18
|
+
* @returns {Promise<number|null>} 常驻内存(MB),取不到为 null
|
|
19
|
+
*/
|
|
20
|
+
export async function rssMb(pid) {
|
|
21
|
+
if (!Number.isInteger(pid) || pid <= 0) return null
|
|
22
|
+
try {
|
|
23
|
+
if (process.platform === 'win32') {
|
|
24
|
+
const { stdout } = await execFileAsync('tasklist', ['/FI', `PID eq ${pid}`, '/FO', 'CSV', '/NH'], {
|
|
25
|
+
windowsHide: true,
|
|
26
|
+
timeout: 8_000,
|
|
27
|
+
})
|
|
28
|
+
// "node.exe","1234","Console","1","123,456 K"
|
|
29
|
+
// 内存列的分隔符随区域设置变(逗号 / 空格 / 点),所以整段数字一次抓完再清分隔符。
|
|
30
|
+
const match = /"[^"]*","(\d+)","[^"]*","[^"]*","([\d\s.,]+)\s*K"/u.exec(stdout)
|
|
31
|
+
if (!match) return null
|
|
32
|
+
const kb = Number(match[2].replace(/[\s.,]/gu, ''))
|
|
33
|
+
return Number.isFinite(kb) && kb > 0 ? Math.round(kb / 1024) : null
|
|
34
|
+
}
|
|
35
|
+
const { stdout } = await execFileAsync('ps', ['-o', 'rss=', '-p', String(pid)], { timeout: 8_000 })
|
|
36
|
+
const kb = Number(String(stdout).trim())
|
|
37
|
+
return Number.isFinite(kb) && kb > 0 ? Math.round(kb / 1024) : null
|
|
38
|
+
} catch {
|
|
39
|
+
return null
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
package/lib/lab/registry.js
CHANGED
|
@@ -54,6 +54,7 @@ export function parseState(text) {
|
|
|
54
54
|
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
55
55
|
pid: Number.isInteger(lab.pid) && lab.pid > 0 ? lab.pid : null,
|
|
56
56
|
pidStartedAt: typeof lab.pidStartedAt === 'string' ? lab.pidStartedAt : null,
|
|
57
|
+
memoryLimitMb: Number.isInteger(lab.memoryLimitMb) && lab.memoryLimitMb > 0 ? lab.memoryLimitMb : null,
|
|
57
58
|
ownerSessionId: typeof lab.ownerSessionId === 'string' ? lab.ownerSessionId : null,
|
|
58
59
|
ownerSince: typeof lab.ownerSince === 'string' ? lab.ownerSince : null,
|
|
59
60
|
hostId: typeof lab.hostId === 'string' ? lab.hostId : null,
|
|
@@ -82,6 +83,7 @@ export function addLab(state, lab) {
|
|
|
82
83
|
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
83
84
|
pid: Number.isInteger(lab.pid) && lab.pid > 0 ? lab.pid : null,
|
|
84
85
|
pidStartedAt: typeof lab.pidStartedAt === 'string' ? lab.pidStartedAt : null,
|
|
86
|
+
memoryLimitMb: Number.isInteger(lab.memoryLimitMb) && lab.memoryLimitMb > 0 ? lab.memoryLimitMb : null,
|
|
85
87
|
ownerSessionId: typeof lab.ownerSessionId === 'string' ? lab.ownerSessionId : null,
|
|
86
88
|
ownerSince: typeof lab.ownerSince === 'string' ? lab.ownerSince : null,
|
|
87
89
|
hostId: typeof lab.hostId === 'string' ? lab.hostId : null,
|
|
@@ -114,5 +116,7 @@ export function describeLab(lab, runtime) {
|
|
|
114
116
|
ownerSessionId: lab.ownerSessionId || null,
|
|
115
117
|
ownerSince: lab.ownerSince || null,
|
|
116
118
|
hostId: lab.hostId || null,
|
|
119
|
+
memoryLimitMb: lab.memoryLimitMb || null,
|
|
120
|
+
rssMb: typeof live.rssMb === 'number' ? live.rssMb : null,
|
|
117
121
|
}
|
|
118
122
|
}
|
package/lib/lab/runtime.js
CHANGED
|
@@ -50,6 +50,8 @@ export function createRuntimeTable({ logRingLines, log, onEvent }) {
|
|
|
50
50
|
token: null,
|
|
51
51
|
startedAt: null,
|
|
52
52
|
exit: null,
|
|
53
|
+
/** 最近一次采样的常驻内存(MB);不落盘,只看。 */
|
|
54
|
+
rssMb: null,
|
|
53
55
|
buffer: createLogBuffer(logRingLines),
|
|
54
56
|
}
|
|
55
57
|
map.set(id, rt)
|
|
@@ -84,6 +86,10 @@ export function createRuntimeTable({ logRingLines, log, onEvent }) {
|
|
|
84
86
|
Object.assign(rt, extra)
|
|
85
87
|
emit({ type: 'status', id, status: next, at: new Date().toISOString() })
|
|
86
88
|
},
|
|
89
|
+
/** 记一次内存采样(面板显示「当前 xx MB」用)。 */
|
|
90
|
+
setSample(id, mb) {
|
|
91
|
+
of(id).rssMb = Number.isFinite(mb) ? mb : null
|
|
92
|
+
},
|
|
87
93
|
logs(id, since = 0) {
|
|
88
94
|
const rt = map.get(id)
|
|
89
95
|
if (!rt) return { entries: [], seq: 0 }
|
package/lib/lab/supervisor.js
CHANGED
|
@@ -17,6 +17,7 @@ import { isPortFree, pickFreePort } from './port.js'
|
|
|
17
17
|
import { resolveCoreBin } from './core-bin.js'
|
|
18
18
|
import { planVersionChange } from './versioning.js'
|
|
19
19
|
import { copyCredentials, ensureProfile } from './provision.js'
|
|
20
|
+
import { startLimitGuard } from './limit-guard.js'
|
|
20
21
|
import { attachLines, isAlive, killTree, spawnChild } from './process.js'
|
|
21
22
|
import { waitHealthy } from './health.js'
|
|
22
23
|
import { addLab, describeLab, emptyState, findLab, removeLab } from './registry.js'
|
|
@@ -42,6 +43,7 @@ export class LabSupervisor {
|
|
|
42
43
|
#ui
|
|
43
44
|
#uiMode = false
|
|
44
45
|
#disposed = false
|
|
46
|
+
#guard = null
|
|
45
47
|
|
|
46
48
|
constructor(opts) {
|
|
47
49
|
this.#root = opts.root
|
|
@@ -61,6 +63,16 @@ export class LabSupervisor {
|
|
|
61
63
|
})
|
|
62
64
|
const backendPreference = this.#config.uiBackend || this.#env.DSH_RIG_UI_BACKEND || 'auto'
|
|
63
65
|
this.#ui = new UiSessions({ log: (line) => this.#log(line), preference: backendPreference })
|
|
66
|
+
// 内存巡检:这是「实验室能弄死宿主」唯一的那条路,必须有人看着。
|
|
67
|
+
this.#guard = startLimitGuard({
|
|
68
|
+
targets: () => this.list().filter((lab) => lab.pid && (lab.status === STATUS.running || lab.status === STATUS.starting)),
|
|
69
|
+
onSample: (id, mb) => this.#rt.setSample(id, mb),
|
|
70
|
+
onExceed: async (lab, mb, limit) => {
|
|
71
|
+
this.#rt.pushLog(lab.id, 'rig', `内存 ${mb} MB 超过 ${limit} MB,已停;面板滑块可放宽`)
|
|
72
|
+
await this.stop(lab.id)
|
|
73
|
+
},
|
|
74
|
+
log: this.#log,
|
|
75
|
+
})
|
|
64
76
|
}
|
|
65
77
|
|
|
66
78
|
// ── 事件 ────────────────────────────────────────────────────────────
|
|
@@ -89,8 +101,7 @@ export class LabSupervisor {
|
|
|
89
101
|
list() {
|
|
90
102
|
return this.#state.labs.map((lab) => {
|
|
91
103
|
const rt = this.#rt.get(lab.id)
|
|
92
|
-
//
|
|
93
|
-
// 以前认 hostId(每进程随机)—— 宿主一重启就把自己的实验室全判成外人(2026-11-24 锁死事故)。
|
|
104
|
+
// 「别人的」只认运行时事实:pid 还活着且不是本进程管的(以前认随机 hostId,重启即锁死)。
|
|
94
105
|
return { ...describeLab(lab, rt), foreign: isForeign(lab, { ownedPid: rt?.pid ?? null, alive: isAlive }) }
|
|
95
106
|
})
|
|
96
107
|
}
|
|
@@ -202,12 +213,7 @@ export class LabSupervisor {
|
|
|
202
213
|
return this.get(id)
|
|
203
214
|
}
|
|
204
215
|
|
|
205
|
-
/**
|
|
206
|
-
* 一次性准备:profile(不在就补建)与凭据。
|
|
207
|
-
*
|
|
208
|
-
* 实现搬到 `provision.js` 了 —— 这个类已经贴着 420 行上限,而准备动作与生命周期无关。
|
|
209
|
-
* 留这个薄壳是为了让 create / start 两处共用同一套日志与路径。
|
|
210
|
-
*/
|
|
216
|
+
/** 一次性准备:profile(缺就补建)与凭据。实现见 provision.js。 */
|
|
211
217
|
async #provision(lab) {
|
|
212
218
|
const paths = labPaths(this.#root, lab.id)
|
|
213
219
|
return ensureProfile({
|
|
@@ -225,13 +231,8 @@ export class LabSupervisor {
|
|
|
225
231
|
})
|
|
226
232
|
}
|
|
227
233
|
|
|
228
|
-
/**
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* 跨进程判定「是不是别人在跑」只认这个字段(见 merge.isForeign):
|
|
232
|
-
* pid 存活 = 真有人在跑;pid 为空 = 没人跑,谁都可以接管。
|
|
233
|
-
* 落盘失败只记日志,不让一次写盘失败把启动流程带垮。
|
|
234
|
-
*/
|
|
234
|
+
/** 把「这条记录现在被谁跑着」写进盘:跨进程判归属只认它(见 merge.isForeign)。
|
|
235
|
+
* 落盘失败只记日志,不让一次写盘失败把启动流程带垮。 */
|
|
235
236
|
async #stampPid(id, pid) {
|
|
236
237
|
const lab = findLab(this.#state, id)
|
|
237
238
|
if (!lab) return
|
|
@@ -257,8 +258,7 @@ export class LabSupervisor {
|
|
|
257
258
|
await mkdir(paths.home, { recursive: true })
|
|
258
259
|
await mkdir(paths.workspace, { recursive: true })
|
|
259
260
|
|
|
260
|
-
//
|
|
261
|
-
// `profile "rig-lab1" does not exist`。正常情况这里只是 stat 一次目录;缺失时才补建(几十秒)。
|
|
261
|
+
// 起之前验一次 profile:lab1 就是栽在这儿(创建时静默失败)。正常只是 stat,缺失才补建。
|
|
262
262
|
const provision = await this.#provision(lab)
|
|
263
263
|
if (!provision.ok) {
|
|
264
264
|
throw new Error(`实验室的 profile 建不出来(${provision.reason}):${lab.profile}`)
|
package/package.json
CHANGED