dsh-rig 0.1.2 → 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 CHANGED
@@ -3,6 +3,14 @@
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
+
6
14
  ## [0.1.2] - 2026-11-21
7
15
 
8
16
  ### Added
@@ -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,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
+ }
@@ -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
  }
@@ -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
- // 自定义 profile 的正确起法是只给 --profile,web 服务由 profile 模板决定。
86
- const args = [coreBin, '--profile', profile, '--port', '0', '--no-open', '--host', '127.0.0.1']
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',
@@ -1,15 +1,12 @@
1
1
  /**
2
2
  * dsh-rig · 进程看管(IO 层 · 唯一写盘点)
3
3
  *
4
- * 只做三件事:**把实验室拉起来、看着它、把它收干净**。
5
- * 拼命令行在 `spawn-plan.js`(纯)、判隔离在 `naming.js`(纯)、日志缓冲在 `log-buffer.js`(纯)、
6
- * 瞬时状态在 `runtime.js`(纯)、探针在 `probe.js`、一次性命令在 `seed.js`、界面驱动在 `ui-session.js`。
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
- * - 不做常驻健康轮询:只在启动阶段探到活,之后靠子进程 `exit` 判死。
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
- // 注意签名:process.js 的 attachLines 回调是 (stream, line),不是柯里化 ——
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 = typeof version === 'string' && version.trim() !== '' ? version.trim() : 'host'
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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-rig",
3
- "version": "0.1.2",
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",