jbc-dssi 1.1.106 → 1.1.108

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.
Files changed (2) hide show
  1. package/lib/proc-commands.js +119 -12
  2. package/package.json +1 -1
@@ -6,6 +6,8 @@
6
6
  * 电脑侧按白名单执行本地脚本,回传摘要 + 结果文件路径;完整日志留在电脑。
7
7
  * · **手机不能传 shell、路径、可执行文件** —— 白名单里没有的 id 一律拒绝。
8
8
  * · 危险指令(发版/重启/停服务…)必须:手机二次确认 + 一次性 nonce(30s、用一次即废)。
9
+ * · **票据按需领取**(2026-11-09 真机修复):nonce 由 `proc_ticket` 在"用户点确认的瞬间"签发,
10
+ * 不是打开页面时预取 —— 否则用户看一眼说明再确认,30 秒早过了,重启永远点不动。
9
11
  * · 同一指令单飞;危险级共用一把全局锁(防发版/重启踩踏);到点超时杀进程。
10
12
  * · 每次执行留痕:`proc-runs/<runId>.log`(脚本输出)+ `proc-runs/index.jsonl`(一行一条)。
11
13
  *
@@ -57,6 +59,38 @@ export const PROC_DEFAULTS = [
57
59
  args: [],
58
60
  builtin: 'health',
59
61
  },
62
+ {
63
+ id: 'release.preflight',
64
+ name: '发版预检',
65
+ desc: '发版前自检:changelog / perf / tag / 插件版本号 / 身份与配置闸门,缺什么直接报出来',
66
+ danger: 'low',
67
+ timeoutMs: 120_000,
68
+ args: [{ name: 'version', label: '版本号', required: true, pattern: '^\\d+\\.\\d+\\.\\d+$' }],
69
+ script: {
70
+ rel: 'scripts/release-preflight.py',
71
+ exec: 'python3',
72
+ argv: ['--version', '{version}'],
73
+ },
74
+ },
75
+ {
76
+ id: 'release.publish',
77
+ name: '发版',
78
+ desc: '按 release-fast 发版:构建 → 发布插件与安装包 → 六项自检(约 3–5 分钟,不可取消)',
79
+ danger: 'high',
80
+ confirmText:
81
+ '发版会构建 APK、发布插件与安装包、跑六项自检,约 3–5 分钟且不可取消。' +
82
+ '前提:changelog/perf 文案已审、tag 已打(可先跑「发版预检」)。',
83
+ timeoutMs: 900_000,
84
+ args: [
85
+ { name: 'version', label: '版本号', required: true, pattern: '^\\d+\\.\\d+\\.\\d+$' },
86
+ { name: 'code', label: 'versionCode', required: true, pattern: '^\\d+$' },
87
+ ],
88
+ script: {
89
+ rel: 'scripts/release-fast.sh',
90
+ exec: 'bash',
91
+ argv: ['--version', '{version}', '--code', '{code}'],
92
+ },
93
+ },
60
94
  {
61
95
  id: 'env.restart_dsh',
62
96
  name: '重启dsh',
@@ -136,6 +170,34 @@ export function resolveScript(cmd, repoRoot) {
136
170
  return null;
137
171
  }
138
172
 
173
+ /** 解释器解析:白名单显式 `exec` 优先,否则按扩展名推断;推不出来 = 不可执行。 */
174
+ export function resolveExec(cmd, scriptPath) {
175
+ const explicit = typeof cmd.script?.exec === 'string' ? cmd.script.exec.trim() : '';
176
+ if (explicit) return explicit;
177
+ const m = /\.[a-z0-9]+$/i.exec(scriptPath ?? '');
178
+ return EXEC_BY_EXT[(m?.[0] ?? '').toLowerCase()] ?? null;
179
+ }
180
+
181
+ /**
182
+ * 把命令参数套进脚本 argv 模板(白名单里声明,例:`['--version', '{version}']`)。
183
+ *
184
+ * 只替换模板里声明过的占位符;手机传来的参数**只能落进模板位置**,
185
+ * 拼不出新参数 —— 这是"手机传不了任意命令行"的同一条边界。
186
+ */
187
+ export function buildArgv(cmd, args) {
188
+ const tpl = Array.isArray(cmd.script?.argv) ? cmd.script.argv : [];
189
+ return tpl.map((tok) =>
190
+ String(tok).replace(/\{([a-zA-Z][a-zA-Z0-9_-]{0,31})\}/g, (_, name) => {
191
+ const v = args?.[name];
192
+ if (v == null) throw new Error(`参数模板引用了未提供的参数:${name}`);
193
+ return String(v);
194
+ }),
195
+ );
196
+ }
197
+
198
+ /** 脚本扩展名 → 默认解释器。 */
199
+ const EXEC_BY_EXT = { '.sh': 'bash', '.py': 'python3', '.mjs': 'node', '.js': 'node' };
200
+
139
201
  /* ------------------------------------------------------------ 默认探针 */
140
202
 
141
203
  /** TCP 端口探测(默认实现;测试可注入)。 */
@@ -262,21 +324,28 @@ export function createProcEngine(opts = {}) {
262
324
  });
263
325
  return {
264
326
  commands: items,
265
- // 高危指令要用这张一次性票据;30s 内有效、用一次即废
327
+ // 兼容老 App(1.1.106):它从 proc_list 取票。新 App 改用 proc_ticket 按需领票。
266
328
  nonce: issueNonce(),
267
329
  nonceTtlMs: NONCE_TTL_MS,
268
330
  version: String(version ?? ''),
269
331
  };
270
332
  }
271
333
 
272
- function issueNonce() {
273
- nonce = { value: randomBytes(16).toString('hex'), expiresAt: now() + NONCE_TTL_MS };
334
+ /**
335
+ * 签一张一次性确认票据。
336
+ *
337
+ * @param {string|null} cmdId 绑定到哪条指令;null = 不绑定(老路径 proc_list 兼容用)。
338
+ * 同时只保留**一张**有效票:新票签发即作废旧票(防止开两个确认弹窗后旧票还能用)。
339
+ */
340
+ function issueNonce(cmdId = null) {
341
+ nonce = { value: randomBytes(16).toString('hex'), expiresAt: now() + NONCE_TTL_MS, cmdId };
274
342
  return nonce.value;
275
343
  }
276
344
 
277
- function consumeNonce(given) {
278
- if (!nonce || now() > nonce.expiresAt) throw new Error('确认已过期,请刷新页面后重试');
279
- if (typeof given !== 'string' || given !== nonce.value) throw new Error('确认票据无效,请刷新页面后重试');
345
+ function consumeNonce(cmdId, given) {
346
+ if (!nonce || now() > nonce.expiresAt) throw new Error('确认已过期,请重新点执行');
347
+ if (nonce.cmdId != null && nonce.cmdId !== cmdId) throw new Error('确认票据与指令不匹配,请重新点执行');
348
+ if (typeof given !== 'string' || given !== nonce.value) throw new Error('确认票据无效,请重新点执行');
280
349
  nonce = null;
281
350
  }
282
351
 
@@ -321,6 +390,16 @@ export function createProcEngine(opts = {}) {
321
390
  }
322
391
  }
323
392
 
393
+ /** 取日志最后一行有效内容(失败原因 / 发版结论通常就在最后一行)。 */
394
+ function lastLogLine(text) {
395
+ const lines = String(text ?? '')
396
+ .split('\n')
397
+ .map((l) => l.trim())
398
+ .filter(Boolean);
399
+ if (lines.length === 0) return '';
400
+ return lines[lines.length - 1].slice(0, 200);
401
+ }
402
+
324
403
  function releaseLocks(record) {
325
404
  activeByCmd.delete(record.cmdId);
326
405
  if (activeHigh === record.runId) activeHigh = null;
@@ -419,9 +498,9 @@ export function createProcEngine(opts = {}) {
419
498
  return child;
420
499
  }
421
500
 
422
- function spawnForeground(scriptPath, args, record) {
501
+ function spawnForeground(exec, argv, record) {
423
502
  return new Promise((resolve) => {
424
- const child = spawnFn('python3', [scriptPath, ...args], {
503
+ const child = spawnFn(exec, argv, {
425
504
  cwd: repoRoot || undefined,
426
505
  stdio: ['ignore', 'pipe', 'pipe'],
427
506
  env: { ...process.env },
@@ -481,12 +560,19 @@ export function createProcEngine(opts = {}) {
481
560
  });
482
561
  return;
483
562
  }
484
- const { exitCode, signal, logText } = await spawnForeground(scriptPath, [], record);
563
+ const exec = resolveExec(cmd, scriptPath);
564
+ if (!exec) {
565
+ finish(record, { exitCode: -1, summary: `${cmd.name} 失败:不认识脚本类型(请在白名单里写 exec)` });
566
+ return;
567
+ }
568
+ const argv = buildArgv(cmd, record.args ?? {});
569
+ const { exitCode, signal, logText } = await spawnForeground(exec, [scriptPath, ...argv], record);
570
+ const tail = lastLogLine(logText);
485
571
  finish(record, {
486
572
  exitCode,
487
573
  summary: exitCode === 0
488
- ? `${cmd.name} 完成`
489
- : `${cmd.name} 失败(${signal ? `signal ${signal}` : `exit ${exitCode}`})`,
574
+ ? (tail ? `${cmd.name} 完成 · ${tail}` : `${cmd.name} 完成`)
575
+ : `${cmd.name} 失败(${signal ? `signal ${signal}` : `exit ${exitCode}`}):${tail || "没有输出"}`,
490
576
  logText,
491
577
  });
492
578
  } catch (err) {
@@ -499,12 +585,33 @@ export function createProcEngine(opts = {}) {
499
585
  /** 统一命令入口(`proc_*`)。与 dssAgent 的 command 同形状:同步校验、异步执行。 */
500
586
  async function command(op, payload = {}) {
501
587
  if (op === 'proc_list') return list();
588
+ if (op === 'proc_ticket') return ticket(payload);
502
589
  if (op === 'proc_run') return runCommand(payload);
503
590
  if (op === 'proc_status') return status(payload);
504
591
  if (op === 'proc_cancel') return cancel(payload);
505
592
  throw new Error(`未知的程序指令命令:${op}`);
506
593
  }
507
594
 
595
+ /**
596
+ * 领一张绑定到某条指令的一次性确认票据(用户点"确认"后再领,30 秒内立刻用掉)。
597
+ *
598
+ * 为什么不复用 proc_list 里的票:那张是打开页面时签的,用户看说明、犹豫一下就会过期
599
+ * (2026-11-09 真机反馈:体检能跑、重启报"确认已过期")。票据的生命周期应该从
600
+ * "用户真的决定执行"那一刻开始。
601
+ */
602
+ function ticket(payload) {
603
+ const cmdId = typeof payload?.cmdId === 'string' ? payload.cmdId : '';
604
+ const cmd = commands.get(cmdId);
605
+ if (!cmd) throw new Error(`没有这条程序指令:${cmdId || '(空)'}`);
606
+ if (!cmd.builtin) {
607
+ const scriptPath = resolveScript(cmd, repoRoot);
608
+ if (!scriptPath || !fs.existsSync(scriptPath)) {
609
+ throw new Error('电脑侧未配置仓库根目录或脚本不存在');
610
+ }
611
+ }
612
+ return { nonce: issueNonce(cmd.id), cmdId: cmd.id, ttlMs: NONCE_TTL_MS };
613
+ }
614
+
508
615
  function runCommand(payload) {
509
616
  const cmdId = typeof payload?.cmdId === 'string' ? payload.cmdId : '';
510
617
  const cmd = commands.get(cmdId);
@@ -513,7 +620,7 @@ export function createProcEngine(opts = {}) {
513
620
  if (cmd.danger !== 'low') {
514
621
  if (payload?.confirm !== true) throw new Error(`「${cmd.name}」需要二次确认`);
515
622
  }
516
- if (cmd.danger === 'high') consumeNonce(payload?.nonce);
623
+ if (cmd.danger === 'high') consumeNonce(cmd.id, payload?.nonce);
517
624
  if (activeByCmd.has(cmd.id)) throw new Error(`「${cmd.name}」正在执行中,请等它结束`);
518
625
  if (cmd.danger !== 'low' && activeHigh) throw new Error('有其它危险指令正在执行,稍后再试');
519
626
  if (!cmd.builtin) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "jbc-dssi",
3
- "version": "1.1.106",
3
+ "version": "1.1.108",
4
4
  "type": "module",
5
5
  "description": "鲸编程 - 不只是手机遥控操作电脑,而是将电脑级的Agent能力装进手机",
6
6
  "keywords": [