dsh-rig 0.1.5 → 0.2.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 +68 -0
- package/README.md +16 -7
- package/docs/DESIGN.md +20 -18
- package/docs/PLATFORMS.md +181 -0
- package/docs/ROADMAP.md +246 -0
- package/lib/backends/contract.js +162 -0
- package/lib/backends/desktop-shell.js +143 -0
- package/lib/backends/index.js +89 -0
- package/lib/backends/web-core.js +92 -0
- package/lib/client.js +35 -33
- package/lib/http/routes.js +11 -3
- package/lib/lab/health.js +47 -0
- package/lib/lab/layout.js +9 -1
- package/lib/lab/merge.js +19 -5
- package/lib/lab/process.js +17 -0
- package/lib/lab/provision.js +83 -0
- package/lib/lab/registry.js +23 -5
- package/lib/lab/supervisor.js +65 -56
- package/lib/mirror/cdp.js +313 -0
- package/lib/mirror/contract.js +237 -0
- package/lib/mirror/plugin/README.md +85 -0
- package/lib/mirror/plugin/host.js +415 -0
- package/lib/mirror/plugin/package.json +10 -0
- package/lib/mirror/server.js +161 -0
- package/package.json +1 -1
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 启动就绪等待(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 从 `supervisor.start()` 里抽出来的:那段 while 循环占着看管者二十多行,
|
|
5
|
+
* 而它其实只回答一个问题 —— 「HTTP 起来了吗」。抽走之后 supervisor 只留生命周期,
|
|
6
|
+
* 也顺手把它压回 420 行以内(0.1.6 加 pid 与补种之后它已经贴到上限)。
|
|
7
|
+
*
|
|
8
|
+
* 判定口径刻意保守:**拿到任意 HTTP 状态码**就算就绪(见 probe.js)。
|
|
9
|
+
* 因为这个探针要区分的是「进程活着但服务没起」和「服务起了」,不是「服务是否健康」——
|
|
10
|
+
* 实验室里跑的是正在开发的插件,健康检查本身可能就是坏的。
|
|
11
|
+
*/
|
|
12
|
+
import { STATUS } from './constants.js'
|
|
13
|
+
import { probeOnce } from './probe.js'
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @param {object} p
|
|
17
|
+
* @param {string} p.id 实验室 id
|
|
18
|
+
* @param {object} p.rt 运行时快照(就地观察 status / url)
|
|
19
|
+
* @param {number} p.deadline 到点放弃(毫秒时间戳)
|
|
20
|
+
* @param {{healthPollMs: number, startTimeoutMs: number}} p.config
|
|
21
|
+
* @param {(id: string, status: string, extra?: object) => void} p.setStatus
|
|
22
|
+
* @param {(id: string, stream: string, line: string) => void} p.pushLog
|
|
23
|
+
* @param {(id: string, opts?: object) => Promise<unknown>} p.stop
|
|
24
|
+
* @param {() => boolean} p.isDisposed 插件已卸载就别再等了
|
|
25
|
+
*/
|
|
26
|
+
export async function waitHealthy({ id, rt, deadline, config, setStatus, pushLog, stop, isDisposed }) {
|
|
27
|
+
let firstFailure = ''
|
|
28
|
+
while (!isDisposed() && Date.now() < deadline) {
|
|
29
|
+
if (rt.status !== STATUS.starting) return
|
|
30
|
+
if (rt.url) {
|
|
31
|
+
const probe = await probeOnce(rt.url)
|
|
32
|
+
if (probe.ok) {
|
|
33
|
+
setStatus(id, STATUS.running)
|
|
34
|
+
pushLog(id, 'rig', `就绪:${rt.url.replace(/token=[^&]+/u, 'token=***')}`)
|
|
35
|
+
return
|
|
36
|
+
}
|
|
37
|
+
if (firstFailure === '') {
|
|
38
|
+
firstFailure = probe.reason || 'unknown'
|
|
39
|
+
pushLog(id, 'rig', `探针未通过(${firstFailure}),继续等…`)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, config.healthPollMs))
|
|
43
|
+
}
|
|
44
|
+
if (rt.status !== STATUS.starting) return
|
|
45
|
+
pushLog(id, 'rig', `启动超时(${Math.round(config.startTimeoutMs / 1000)}s)${firstFailure ? `:${firstFailure}` : ''}`)
|
|
46
|
+
await stop(id, { asCrash: true })
|
|
47
|
+
}
|
package/lib/lab/layout.js
CHANGED
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
* 手写 join 在 Windows 盘符与 UNC 上太容易错,而这一层本身没有副作用,测起来只是字符串断言。
|
|
6
6
|
* 判据(隔离/命名)不在这里 —— 它们在 `naming.js`(零内建,可穷举)。
|
|
7
7
|
*/
|
|
8
|
+
import { homedir } from 'node:os'
|
|
8
9
|
import { join } from 'node:path'
|
|
9
10
|
|
|
10
11
|
/**
|
|
@@ -33,6 +34,13 @@ export function coreBinPath(root, version) {
|
|
|
33
34
|
/** 实验室根目录:默认放本机应用数据目录,绝不放进宿主 DSH_HOME 里。 */
|
|
34
35
|
export function resolveLabRoot(configRoot, env) {
|
|
35
36
|
if (typeof configRoot === 'string' && configRoot.trim() !== '') return configRoot.trim()
|
|
36
|
-
|
|
37
|
+
// 三平台的「应用数据目录」不是一个变量:
|
|
38
|
+
// Windows → LOCALAPPDATA(APPDATA 是漫游目录,必要时才退);
|
|
39
|
+
// macOS → XDG_DATA_HOME 缺省为 ~/.local/share(APPDATA 不存在);
|
|
40
|
+
// Linux → 同上。
|
|
41
|
+
// ⚠️ 兜底**不能是 '.'**:那会让实验室落在进程的当前工作目录里(vim 打开文件的那个目录),
|
|
42
|
+
// 换一个 profile 起服务就换一处落点,日志与数据看起来像「凭空消失」。
|
|
43
|
+
const home = env.HOME || homedir()
|
|
44
|
+
const base = env.LOCALAPPDATA || env.APPDATA || env.XDG_DATA_HOME || join(home, '.local', 'share')
|
|
37
45
|
return join(base, 'dsh-rig')
|
|
38
46
|
}
|
package/lib/lab/merge.js
CHANGED
|
@@ -36,12 +36,26 @@ export function mergeStates(disk, ours) {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
/**
|
|
39
|
-
*
|
|
40
|
-
*
|
|
39
|
+
* 这条实验室是不是**别人的进程正在跑**(只能看、不能驱动)。
|
|
40
|
+
*
|
|
41
|
+
* ⚠️ 判据是「记录的 pid 还活着,且不是本进程管的那个」,**不是**「hostId 一样不一样」。
|
|
42
|
+
* 0.1.5 用的是后者,而 hostId 是每进程随机生成的 —— 宿主重启一次,之前建的实验室
|
|
43
|
+
* 全部变成「别人的」,面板锁成只读、启动与销毁都点不了,claim 也解不开(它只换会话)。
|
|
44
|
+
* 2026-11-24 就是这么把 lab1 锁死的。
|
|
45
|
+
*
|
|
46
|
+
* 现在的两条边界:
|
|
47
|
+
* - 没有 pid(从没跑过、或已正常停止)→ 不是别人的;
|
|
48
|
+
* - pid 就是本进程正在管的那个 → 不是别人的。
|
|
49
|
+
* 只有「有 pid、不是我的、进程还活着」才是别人的 —— 那是真孤儿,由 orphans 负责收。
|
|
50
|
+
*
|
|
41
51
|
* @param {object} lab 实验室记录
|
|
42
|
-
* @param {
|
|
52
|
+
* @param {{ownedPid?: number|null, alive?: (pid: number) => boolean}} probe 由 supervisor 注入的运行时事实
|
|
43
53
|
* @returns {boolean}
|
|
44
54
|
*/
|
|
45
|
-
export function isForeign(lab,
|
|
46
|
-
|
|
55
|
+
export function isForeign(lab, probe = {}) {
|
|
56
|
+
const pid = Number.isInteger(lab?.pid) && lab.pid > 0 ? lab.pid : null
|
|
57
|
+
if (!pid) return false
|
|
58
|
+
if (Number.isInteger(probe.ownedPid) && probe.ownedPid === pid) return false
|
|
59
|
+
const alive = typeof probe.alive === 'function' ? probe.alive : () => false
|
|
60
|
+
return Boolean(alive(pid))
|
|
47
61
|
}
|
package/lib/lab/process.js
CHANGED
|
@@ -22,6 +22,23 @@ export function spawnChild(plan) {
|
|
|
22
22
|
})
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
/**
|
|
26
|
+
* 这个 pid 现在还活着吗。
|
|
27
|
+
*
|
|
28
|
+
* 用 `process.kill(pid, 0)`:不发信号、只做存在性检查,Windows / macOS / Linux 行为一致。
|
|
29
|
+
* `EPERM` 说明进程在、只是没权限动它 —— 那是「活着」而不是「没有」,
|
|
30
|
+
* 判反了会让别人的实验室被当成自己的(2026-11-24 那类锁死的反面)。
|
|
31
|
+
*/
|
|
32
|
+
export function isAlive(pid) {
|
|
33
|
+
if (!Number.isInteger(pid) || pid <= 0) return false
|
|
34
|
+
try {
|
|
35
|
+
process.kill(pid, 0)
|
|
36
|
+
return true
|
|
37
|
+
} catch (err) {
|
|
38
|
+
return Boolean(err && err.code === 'EPERM')
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
25
42
|
/** 把子进程的两根输出管道都接到同一个回调(逐行,已去换行)。 */
|
|
26
43
|
export function attachLines(child, onLine) {
|
|
27
44
|
if (child.stdout) createInterface({ input: child.stdout }).on('line', (line) => onLine('out', line))
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-rig · 一次性准备(IO 层)
|
|
3
|
+
*
|
|
4
|
+
* 「实验室第一次起来之前必须有」的两样东西:profile 与凭据。两者都是**跑一次就退**的命令,
|
|
5
|
+
* 与 supervisor 管的「持续活着的进程」分开 —— 混在一起时,看管者会为一个一次性动作长出一堆失败分支。
|
|
6
|
+
*
|
|
7
|
+
* 抽出来还有一个直接原因:这一步**静默失败过**,而且没人发现。
|
|
8
|
+
* `labs/lab1/home/init.err` 里躺着一条 `Cannot find module 'E:\Program'`(安装路径带空格),
|
|
9
|
+
* 说明 2026-11-22 建 lab1 时 profile 根本没生成;之后每次启动都死在
|
|
10
|
+
* `profile "rig-lab1" does not exist`,面板看起来就是「完全坏了」。
|
|
11
|
+
*
|
|
12
|
+
* 所以现在的规矩是:**create 时建一次,start 前再验一次,不在就补**,
|
|
13
|
+
* 补失败要说人话并带原因,不再默默继续。判定「profile 在不在」只看目录 —— DSH 认的就是它。
|
|
14
|
+
*/
|
|
15
|
+
import { mkdir, stat } from 'node:fs/promises'
|
|
16
|
+
import { join } from 'node:path'
|
|
17
|
+
import { buildSeedPlan } from './spawn-plan.js'
|
|
18
|
+
import { resolveCoreBin } from './core-bin.js'
|
|
19
|
+
import { copyCredentials, runOnce } from './seed.js'
|
|
20
|
+
|
|
21
|
+
/** `<lab home>/profiles/<profile>`。 */
|
|
22
|
+
export function profileDir(home, profile) {
|
|
23
|
+
return join(home, 'profiles', profile)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** profile 目录在不在。任何异常都算不在(不能因为 stat 失败就把实验室判死)。 */
|
|
27
|
+
export async function profileExists(home, profile) {
|
|
28
|
+
try {
|
|
29
|
+
const info = await stat(profileDir(home, profile))
|
|
30
|
+
return info.isDirectory()
|
|
31
|
+
} catch {
|
|
32
|
+
return false
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 跑一次 `--from-default-profile … --dump-config` 建 profile。
|
|
38
|
+
*
|
|
39
|
+
* 不抛:失败只落日志,由 `ensureProfile` 用「目录在不在」做最终判据 ——
|
|
40
|
+
* 子进程的非零退出码不该代替文件系统的证据。
|
|
41
|
+
*
|
|
42
|
+
* @returns {Promise<{ok: boolean, reason: string|null}>}
|
|
43
|
+
*/
|
|
44
|
+
export async function seedProfile(options) {
|
|
45
|
+
const { lab, home, workspace, execPath, isElectron, version, root, env, log } = options
|
|
46
|
+
let coreBin
|
|
47
|
+
try {
|
|
48
|
+
coreBin = await resolveCoreBin({ version, root, env })
|
|
49
|
+
} catch (err) {
|
|
50
|
+
log('rig', `建 profile 失败:${err?.message ?? err}`)
|
|
51
|
+
return { ok: false, reason: 'core-missing' }
|
|
52
|
+
}
|
|
53
|
+
const plan = buildSeedPlan({ execPath, isElectron, coreBin, profile: lab.profile, cwd: workspace, home, env })
|
|
54
|
+
const result = await runOnce(plan, {
|
|
55
|
+
onLine: (stream, line) => {
|
|
56
|
+
// 只挑有用的几类,别把整段 pnpm 输出灌进环形缓冲。`Cannot find module` 是路径出错的指纹。
|
|
57
|
+
if (stream === 'err' && /disabling profile plugin row|incompatible with dsh|Cannot find module|ENOENT/iu.test(line)) {
|
|
58
|
+
log('seed', line)
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
})
|
|
62
|
+
log('rig', `profile 就绪(exit ${result.code ?? 'null'}${result.timedOut ? ',超时' : ''})`)
|
|
63
|
+
return { ok: result.code === 0, reason: result.timedOut ? 'timeout' : null }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 确保 profile 在:已在就什么都不做;不在就补建一次,补完再用目录验一次。
|
|
68
|
+
*
|
|
69
|
+
* @returns {Promise<{ok: boolean, created: boolean, reason: string|null}>}
|
|
70
|
+
*/
|
|
71
|
+
export async function ensureProfile(options) {
|
|
72
|
+
const { lab, home, workspace, log } = options
|
|
73
|
+
if (await profileExists(home, lab.profile)) return { ok: true, created: false, reason: null }
|
|
74
|
+
|
|
75
|
+
log('rig', `profile ${lab.profile} 不存在,补建一次(这步以前只在创建时跑,静默失败过)`)
|
|
76
|
+
await mkdir(join(home, 'profiles'), { recursive: true })
|
|
77
|
+
const seeded = await seedProfile(options)
|
|
78
|
+
const ok = await profileExists(home, lab.profile)
|
|
79
|
+
if (!ok) log('rig', `补建失败:${seeded.reason || 'seed 命令没生成 profile'}`)
|
|
80
|
+
return { ok, created: true, reason: ok ? null : seeded.reason || 'seed-failed' }
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { copyCredentials }
|
package/lib/lab/registry.js
CHANGED
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* dsh-rig · 持久状态
|
|
3
3
|
*
|
|
4
|
-
* `labs.json`
|
|
5
|
-
* -
|
|
6
|
-
* -
|
|
7
|
-
*
|
|
4
|
+
* `labs.json` 里存**静态**字段(名字、版本、路径、profile)与两个例外:
|
|
5
|
+
* - 端口:固定后要复用,否则每换端口就换 origin,登录态与页面状态全丢(见 port.js);
|
|
6
|
+
* - pid / pidStartedAt:**从 2 起落盘**。它不再用于「孤儿探测」那么窄的目的 ——
|
|
7
|
+
* 它是「这条记录现在被谁跑着」的唯一事实来源(见 merge.isForeign)。
|
|
8
|
+
* token 仍然不落盘:落盘等于白送一份凭据到磁盘上,而它每次启动都会重新生成。
|
|
8
9
|
*
|
|
9
10
|
* 这里的函数都是纯的(不碰 fs):读写由 supervisor 负责,方便单测穷举迁移。
|
|
10
11
|
*/
|
|
11
12
|
import { STATUS } from './constants.js'
|
|
12
13
|
import { profileNameFor } from './naming.js'
|
|
13
14
|
|
|
15
|
+
/**
|
|
16
|
+
* 当前写法。**加可选字段不动版本号** —— 这与 DSH 官方对会话格式的纪律一致:
|
|
17
|
+
* 只能新增,不能移动或覆盖已存在的内容。
|
|
18
|
+
*
|
|
19
|
+
* 具体教训:0.1.6 一度想升到 2(因为新增了 pid / pidStartedAt),
|
|
20
|
+
* 但 0.1.5 的判定是 `schemaVersion !== 1 → 未知格式 → 按空状态处理`,
|
|
21
|
+
* 于是「回滚到 0.1.5」会把整份 labs.json 读成空的、并在下一次写盘时清掉 ——
|
|
22
|
+
* 为了两个可选字段引入一次数据丢失风险,不划算。
|
|
23
|
+
* 现在 v1 仍是 v1,旧版读到多余字段会忽略它们,只是丢掉 pid 能力。
|
|
24
|
+
*/
|
|
14
25
|
const SCHEMA_VERSION = 1
|
|
26
|
+
/** 能读的版本。将来真需要破坏性变更时,在这里加而不是改 SCHEMA_VERSION。 */
|
|
27
|
+
const READABLE_VERSIONS = new Set([1])
|
|
15
28
|
|
|
16
29
|
/** 空状态。 */
|
|
17
30
|
export function emptyState() {
|
|
@@ -27,9 +40,10 @@ export function parseState(text) {
|
|
|
27
40
|
} catch {
|
|
28
41
|
return { state: emptyState(), problem: 'invalid-json' }
|
|
29
42
|
}
|
|
30
|
-
if (!raw || typeof raw !== 'object' || raw.schemaVersion
|
|
43
|
+
if (!raw || typeof raw !== 'object' || !READABLE_VERSIONS.has(raw.schemaVersion) || !Array.isArray(raw.labs)) {
|
|
31
44
|
return { state: emptyState(), problem: 'unknown-schema' }
|
|
32
45
|
}
|
|
46
|
+
// 缺字段补默认值(v1 老记录没有 pid / pidStartedAt),不搬内容,所以重复读结果相同。
|
|
33
47
|
const labs = raw.labs
|
|
34
48
|
.filter((lab) => lab && typeof lab.id === 'string')
|
|
35
49
|
.map((lab) => ({
|
|
@@ -38,6 +52,8 @@ export function parseState(text) {
|
|
|
38
52
|
version: typeof lab.version === 'string' ? lab.version : 'host',
|
|
39
53
|
profile: typeof lab.profile === 'string' ? lab.profile : profileNameFor(lab.id),
|
|
40
54
|
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
55
|
+
pid: Number.isInteger(lab.pid) && lab.pid > 0 ? lab.pid : null,
|
|
56
|
+
pidStartedAt: typeof lab.pidStartedAt === 'string' ? lab.pidStartedAt : null,
|
|
41
57
|
ownerSessionId: typeof lab.ownerSessionId === 'string' ? lab.ownerSessionId : null,
|
|
42
58
|
ownerSince: typeof lab.ownerSince === 'string' ? lab.ownerSince : null,
|
|
43
59
|
hostId: typeof lab.hostId === 'string' ? lab.hostId : null,
|
|
@@ -64,6 +80,8 @@ export function addLab(state, lab) {
|
|
|
64
80
|
version: lab.version,
|
|
65
81
|
profile: lab.profile,
|
|
66
82
|
port: Number.isInteger(lab.port) && lab.port > 0 ? lab.port : null,
|
|
83
|
+
pid: Number.isInteger(lab.pid) && lab.pid > 0 ? lab.pid : null,
|
|
84
|
+
pidStartedAt: typeof lab.pidStartedAt === 'string' ? lab.pidStartedAt : null,
|
|
67
85
|
ownerSessionId: typeof lab.ownerSessionId === 'string' ? lab.ownerSessionId : null,
|
|
68
86
|
ownerSince: typeof lab.ownerSince === 'string' ? lab.ownerSince : null,
|
|
69
87
|
hostId: typeof lab.hostId === 'string' ? lab.hostId : null,
|
package/lib/lab/supervisor.js
CHANGED
|
@@ -11,14 +11,14 @@ import { join } from 'node:path'
|
|
|
11
11
|
import { DEFAULTS, STATUS } from './constants.js'
|
|
12
12
|
import { labPaths } from './layout.js'
|
|
13
13
|
import { assertIsolated, isValidLabId, nextLabId, profileNameFor } from './naming.js'
|
|
14
|
-
import {
|
|
14
|
+
import { buildStartPlan, parseLabUrl } from './spawn-plan.js'
|
|
15
15
|
import { buildDiagnostics } from './diagnostics.js'
|
|
16
16
|
import { isPortFree, pickFreePort } from './port.js'
|
|
17
17
|
import { resolveCoreBin } from './core-bin.js'
|
|
18
18
|
import { planVersionChange } from './versioning.js'
|
|
19
|
-
import { copyCredentials,
|
|
20
|
-
import { attachLines, killTree, spawnChild } from './process.js'
|
|
21
|
-
import {
|
|
19
|
+
import { copyCredentials, ensureProfile } from './provision.js'
|
|
20
|
+
import { attachLines, isAlive, killTree, spawnChild } from './process.js'
|
|
21
|
+
import { waitHealthy } from './health.js'
|
|
22
22
|
import { addLab, describeLab, emptyState, findLab, removeLab } from './registry.js'
|
|
23
23
|
import { createRuntimeTable } from './runtime.js'
|
|
24
24
|
import { LabStateStore } from './state-store.js'
|
|
@@ -87,7 +87,12 @@ export class LabSupervisor {
|
|
|
87
87
|
|
|
88
88
|
// ── 读取 ────────────────────────────────────────────────────────────
|
|
89
89
|
list() {
|
|
90
|
-
return this.#state.labs.map((lab) =>
|
|
90
|
+
return this.#state.labs.map((lab) => {
|
|
91
|
+
const rt = this.#rt.get(lab.id)
|
|
92
|
+
// 「别人的」只认运行时事实:记录里的 pid 还活着,且不是本进程正在管的那个。
|
|
93
|
+
// 以前认 hostId(每进程随机)—— 宿主一重启就把自己的实验室全判成外人(2026-11-24 锁死事故)。
|
|
94
|
+
return { ...describeLab(lab, rt), foreign: isForeign(lab, { ownedPid: rt?.pid ?? null, alive: isAlive }) }
|
|
95
|
+
})
|
|
91
96
|
}
|
|
92
97
|
|
|
93
98
|
get(id) {
|
|
@@ -180,7 +185,7 @@ export class LabSupervisor {
|
|
|
180
185
|
}
|
|
181
186
|
this.#rt.of(id)
|
|
182
187
|
|
|
183
|
-
await this.#
|
|
188
|
+
await this.#provision(lab)
|
|
184
189
|
if (this.#config.copyCredentials !== false) {
|
|
185
190
|
const ok = await copyCredentials({ fromHome: this.#hostHome, toHome: paths.home })
|
|
186
191
|
this.#rt.pushLog(
|
|
@@ -197,33 +202,46 @@ export class LabSupervisor {
|
|
|
197
202
|
return this.get(id)
|
|
198
203
|
}
|
|
199
204
|
|
|
200
|
-
/**
|
|
201
|
-
|
|
205
|
+
/**
|
|
206
|
+
* 一次性准备:profile(不在就补建)与凭据。
|
|
207
|
+
*
|
|
208
|
+
* 实现搬到 `provision.js` 了 —— 这个类已经贴着 420 行上限,而准备动作与生命周期无关。
|
|
209
|
+
* 留这个薄壳是为了让 create / start 两处共用同一套日志与路径。
|
|
210
|
+
*/
|
|
211
|
+
async #provision(lab) {
|
|
202
212
|
const paths = labPaths(this.#root, lab.id)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
this.#rt.pushLog(lab.id, 'rig', `建 profile 失败:${err?.message ?? err}`)
|
|
208
|
-
return
|
|
209
|
-
}
|
|
210
|
-
const plan = buildSeedPlan({
|
|
213
|
+
return ensureProfile({
|
|
214
|
+
lab,
|
|
215
|
+
home: paths.home,
|
|
216
|
+
workspace: paths.workspace,
|
|
211
217
|
execPath: this.#execPath,
|
|
212
218
|
isElectron: this.#isElectron,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
cwd: paths.workspace,
|
|
216
|
-
home: paths.home,
|
|
219
|
+
version: lab.version,
|
|
220
|
+
root: this.#root,
|
|
217
221
|
env: this.#env,
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
onLine: (stream, line) => {
|
|
221
|
-
if (stream === 'err' && /disabling profile plugin row|incompatible with dsh/iu.test(line)) {
|
|
222
|
-
this.#rt.pushLog(lab.id, 'seed', line)
|
|
223
|
-
}
|
|
222
|
+
log: (stream, line) => {
|
|
223
|
+
this.#rt.pushLog(lab.id, stream, line)
|
|
224
224
|
},
|
|
225
225
|
})
|
|
226
|
-
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 把「这条记录现在被谁跑着」写进盘。
|
|
230
|
+
*
|
|
231
|
+
* 跨进程判定「是不是别人在跑」只认这个字段(见 merge.isForeign):
|
|
232
|
+
* pid 存活 = 真有人在跑;pid 为空 = 没人跑,谁都可以接管。
|
|
233
|
+
* 落盘失败只记日志,不让一次写盘失败把启动流程带垮。
|
|
234
|
+
*/
|
|
235
|
+
async #stampPid(id, pid) {
|
|
236
|
+
const lab = findLab(this.#state, id)
|
|
237
|
+
if (!lab) return
|
|
238
|
+
lab.pid = Number.isInteger(pid) && pid > 0 ? pid : null
|
|
239
|
+
lab.pidStartedAt = lab.pid ? new Date().toISOString() : null
|
|
240
|
+
try {
|
|
241
|
+
await this.#save()
|
|
242
|
+
} catch (err) {
|
|
243
|
+
this.#log(`[dsh-rig] pid 落盘失败:${err?.message ?? err}`)
|
|
244
|
+
}
|
|
227
245
|
}
|
|
228
246
|
|
|
229
247
|
async start(id) {
|
|
@@ -239,6 +257,13 @@ export class LabSupervisor {
|
|
|
239
257
|
await mkdir(paths.home, { recursive: true })
|
|
240
258
|
await mkdir(paths.workspace, { recursive: true })
|
|
241
259
|
|
|
260
|
+
// 0.1.6:起之前验一次 profile。lab1 就是栽在这儿 —— 创建时静默失败,之后每次启动都报
|
|
261
|
+
// `profile "rig-lab1" does not exist`。正常情况这里只是 stat 一次目录;缺失时才补建(几十秒)。
|
|
262
|
+
const provision = await this.#provision(lab)
|
|
263
|
+
if (!provision.ok) {
|
|
264
|
+
throw new Error(`实验室的 profile 建不出来(${provision.reason}):${lab.profile}`)
|
|
265
|
+
}
|
|
266
|
+
|
|
242
267
|
if (!lab.port || !(await isPortFree(lab.port))) {
|
|
243
268
|
lab.port = await pickFreePort(this.#config.basePort)
|
|
244
269
|
await this.#save()
|
|
@@ -272,6 +297,7 @@ export class LabSupervisor {
|
|
|
272
297
|
}
|
|
273
298
|
rt.child = child
|
|
274
299
|
rt.pid = child.pid ?? null
|
|
300
|
+
await this.#stampPid(id, rt.pid)
|
|
275
301
|
|
|
276
302
|
// 注意签名:attachLines 回调是 (stream, line),不是柯里化 —— 2026-11-21 错配导致实验室输出全被吞。
|
|
277
303
|
attachLines(child, (stream, line) => {
|
|
@@ -293,6 +319,7 @@ export class LabSupervisor {
|
|
|
293
319
|
child.on('exit', (code, signal) => {
|
|
294
320
|
rt.child = null
|
|
295
321
|
rt.pid = null
|
|
322
|
+
void this.#stampPid(id, null)
|
|
296
323
|
const wasLive = rt.status === STATUS.starting || rt.status === STATUS.running
|
|
297
324
|
const exit = { code: code ?? null, signal: signal ?? null, at: new Date().toISOString() }
|
|
298
325
|
this.#rt.pushLog(id, 'rig', `实验室退出:code=${exit.code ?? 'null'} signal=${exit.signal ?? 'null'}`)
|
|
@@ -304,38 +331,19 @@ export class LabSupervisor {
|
|
|
304
331
|
this.#ui.close(id)
|
|
305
332
|
})
|
|
306
333
|
|
|
307
|
-
void
|
|
334
|
+
void waitHealthy({
|
|
335
|
+
id,
|
|
336
|
+
rt,
|
|
337
|
+
deadline: Date.now() + this.#config.startTimeoutMs,
|
|
338
|
+
config: this.#config,
|
|
339
|
+
setStatus: (labId, status, extra) => this.#rt.setStatus(labId, status, extra),
|
|
340
|
+
pushLog: (labId, stream, line) => this.#rt.pushLog(labId, stream, line),
|
|
341
|
+
stop: (labId, opts) => this.stop(labId, opts),
|
|
342
|
+
isDisposed: () => this.#disposed,
|
|
343
|
+
})
|
|
308
344
|
return this.get(id)
|
|
309
345
|
}
|
|
310
346
|
|
|
311
|
-
async #waitHealthy(id, rt, deadline) {
|
|
312
|
-
let firstFailure = ''
|
|
313
|
-
while (!this.#disposed && Date.now() < deadline) {
|
|
314
|
-
if (rt.status !== STATUS.starting) return
|
|
315
|
-
if (rt.url) {
|
|
316
|
-
const probe = await probeOnce(rt.url)
|
|
317
|
-
if (probe.ok) {
|
|
318
|
-
this.#rt.setStatus(id, STATUS.running)
|
|
319
|
-
this.#rt.pushLog(id, 'rig', `就绪:${rt.url.replace(/token=[^&]+/u, 'token=***')}`)
|
|
320
|
-
return
|
|
321
|
-
}
|
|
322
|
-
if (firstFailure === '') {
|
|
323
|
-
firstFailure = probe.reason || 'unknown'
|
|
324
|
-
this.#rt.pushLog(id, 'rig', `探针未通过(${firstFailure}),继续等…`)
|
|
325
|
-
}
|
|
326
|
-
}
|
|
327
|
-
await new Promise((r) => setTimeout(r, this.#config.healthPollMs))
|
|
328
|
-
}
|
|
329
|
-
if (rt.status === STATUS.starting) {
|
|
330
|
-
this.#rt.pushLog(
|
|
331
|
-
id,
|
|
332
|
-
'rig',
|
|
333
|
-
`启动超时(${Math.round(this.#config.startTimeoutMs / 1000)}s)${firstFailure ? `:${firstFailure}` : ''}`,
|
|
334
|
-
)
|
|
335
|
-
await this.stop(id, { asCrash: true })
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
347
|
async stop(id, opts = {}) {
|
|
340
348
|
const rt = this.#rt.get(id)
|
|
341
349
|
if (!rt) return this.get(id)
|
|
@@ -356,6 +364,7 @@ export class LabSupervisor {
|
|
|
356
364
|
rt.token = null
|
|
357
365
|
rt.port = null
|
|
358
366
|
this.#ui.close(id)
|
|
367
|
+
await this.#stampPid(id, null)
|
|
359
368
|
return this.get(id)
|
|
360
369
|
}
|
|
361
370
|
|