dsh-builtin-browser 0.4.14 → 0.4.15

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
@@ -4,6 +4,36 @@
4
4
 
5
5
  ---
6
6
 
7
+ # 第五十二轮(2026-10-19,两个启动缺陷:hand-off 只认一次、启动参数白送一个空标签)
8
+
9
+ 报告者在 Windows 11 + Chrome 156 上报了两个,并自己定位了位置。他先报在 0.4.7 上,**升到 0.4.14 复测后两个都仍在** —— 我核实确认。
10
+
11
+ ## 一、exit 0 的认领只做一次(0.4.14 上是"读早了")
12
+
13
+ `start()` 在 child 退出后判 `clean = exitCode === 0 && signal === null`,然后调**一次** `adoptRunningBrowser('after a hand-off')`。0.4.14 已经把"exit 0 当作移交而不是失败"做对了,但**只认一次**。
14
+
15
+ 报告者测出了为什么必然失败:Windows 上启动器 stub **300 ms 就退出**,而 `DevToolsActivePort` **441 ms 才落地** —— stub 退出那一刻文件还不存在,认领返回 undefined,于是仍然抛 `exited immediately after launch (exit code 0)`,而浏览器其实已经在启动。
16
+
17
+ 改法按他的建议做成**有界重试**:`HANDOFF_ADOPT_MS = 8_000`、间隔复用既有的 `PORT_PROBE_MS`(250 ms),`this.disposed` 或超时即停。8 秒是他实测可用的上界,相对那约 150 ms 的间隙是宽裕的;真正失败的启动仍会在同一预算内报错,不会被拖成"慢失败"。
18
+
19
+ **重试只在 `clean` 分支内**。非 0 退出码或信号仍然立即判死并给出"它没有把请求交给谁"的措辞 —— 把它们也重试会把一句明确的"检查它能不能跑"变成 8 秒停顿后同一句话。
20
+
21
+ ## 二、启动参数 `about:blank` 白送一个空标签
22
+
23
+ 参数列表原来以 `'about:blank'` 结尾。插件自己的页面是首次使用时由 `Target.createTarget` 建的(`buildSession`),**开头那个标签从来不复用也不关闭** —— 标签栏是"空页 + 目标页"两行,而 `dsh_browser_list_tabs` 只报 1 个,因为那个空标签不在插件的模型里。这正是报告者描述的形态。
24
+
25
+ 改为 **`--no-startup-window`**:不建窗口、不起初始 page,浏览器仍然存活并可连 CDP。报告者实测(用正确引号重测后):`DevToolsActivePort` 599 ms 落地、`/json/version` 200、`/json/list` 只有两个 extension target 没有 page、然后 `Target.createTarget` 能正常建出窗口与页面。
26
+
27
+ **兼容性没丢**:若某个浏览器仍开了一个空白页,`normalizeBlankPages` 会关掉多余的、`buildSession` 会领养留下来的那个 —— 这条路径本来就在,现在只是不再主动制造它。
28
+
29
+ ## 三、验证:先证明断言能区分修复前后
30
+
31
+ 3 条新断言,把源码回退到修复前跑同一组:**3 条全失败**(其中启动参数那条等了 30 秒超时),修复后 3 条全过。全量 **295 → 298**(297 通过 + 1 条平台门控跳过),`tsc` 干净。
32
+
33
+ **未验证部分**:我没有在 Windows 上跑真实的 Chrome 156 复现那 150 ms 的间隙。两个改动都按报告者的实测参数落地(8 秒 / 250 ms、`--no-startup-window`),但真实时序仍需在他的机器上复测。
34
+
35
+ ---
36
+
7
37
  # 第五十一轮(2026-10-19,无显示环境无法启动浏览器:自动 headless)
8
38
 
9
39
  报告者把一个 systemd 服务上的部署失败追到了底:Debian 13、`dsh web` 以服务常驻、**进程里没有 `DISPLAY` / `WAYLAND_DISPLAY`**,于是每次 `dsh_browser_*` 都报 `exited immediately after launch (exit code 1)`,`DevToolsActivePort` 从不出现,整组工具不可用。底层原因是 Chromium 打印 `qt.qpa.xcb: could not connect to display` 后立即退出,而我们的启动参数从来不含 `--headless`。
@@ -61,6 +61,20 @@ const CLOSE_GRACE_MS = 6_000;
61
61
  * interval, so a worst-case iteration costs 500 ms and the budget still affords ~60 attempts.
62
62
  */
63
63
  const PORT_PROBE_MS = 250;
64
+ /**
65
+ * How long to keep trying to adopt a browser after a launcher reported a clean exit.
66
+ *
67
+ * Exit code 0 means the launcher handed the request to an instance already holding this profile —
68
+ * but on Windows that stub exits BEFORE the real browser writes `DevToolsActivePort`. Measured on
69
+ * Chrome 156: stub gone at 300 ms, port file present at 441 ms (issue #33). A single attempt read
70
+ * too early and reported a launch failure for a browser that was starting normally, so this is a
71
+ * retry rather than one shot.
72
+ *
73
+ * 8 s is the reporter's measured-working bound and is generous against that ~150 ms gap: an honest
74
+ * hand-off resolves in well under a second, and a launch that genuinely failed still reaches the
75
+ * error within the same budget the CDP loop already allows.
76
+ */
77
+ const HANDOFF_ADOPT_MS = 8_000;
64
78
  /** The real launcher: spawn a detached-free child with pipes closed. */
65
79
  const defaultLauncher = (path, args) => spawn(path, [...args], { stdio: ['ignore', 'ignore', 'ignore'], windowsHide: false });
66
80
  import { spawn } from 'node:child_process';
@@ -745,7 +759,18 @@ export class SystemBrowserViewHost {
745
759
  // /dev/shm far smaller than a desktop. See resolveLaunchMode for why this is conditional.
746
760
  ...launch.headless ? ['--headless=new', '--disable-gpu', '--disable-dev-shm-usage'] : [],
747
761
  ...this.extraArgs,
748
- 'about:blank',
762
+ // NOT `about:blank`. Every page this plugin drives is created by `Target.createTarget` when a
763
+ // session first needs one (`buildSession`, which adopts a blank if the browser opened one and
764
+ // creates one otherwise), so a startup page is a tab nobody asked for and nobody closes: the
765
+ // tab strip showed `about:blank` next to the real page while `dsh_browser_list_tabs` reported
766
+ // only the real one. Reported as issue #33, banner and all.
767
+ //
768
+ // `--no-startup-window` keeps the browser alive with no window and no initial page; measured
769
+ // by the reporter on Chrome 156 as writing DevToolsActivePort in 599 ms, answering
770
+ // `/json/version`, listing two extension targets and no page, and then creating windows
771
+ // normally through `Target.createTarget`. A browser that does start a blank despite this is
772
+ // still handled: `normalizeBlankPages` closes the surplus and `buildSession` adopts the rest.
773
+ '--no-startup-window',
749
774
  ];
750
775
  // Chromium's own sandbox cannot start in some of these environments — running as root, or
751
776
  // inside a container without the namespaces it needs — and it exits rather than degrading.
@@ -998,10 +1023,23 @@ export class SystemBrowserViewHost {
998
1023
  // Exit code 0 IS the hand-off: the request went to an instance already holding this profile,
999
1024
  // so that instance is our browser and it is running right now. Adopting it turns the
1000
1025
  // reported "kill the leftovers by hand, on every restart" into a call that just works.
1026
+ //
1027
+ // Adopted with a bounded RETRY, because on Windows the launcher stub exits before the browser
1028
+ // has written its port file: measured 300 ms to exit against 441 ms for DevToolsActivePort.
1029
+ // One attempt therefore read too early and reported "exited immediately after launch" for a
1030
+ // browser that was starting normally — reported as issue #33, where the reporter's own
1031
+ // retry loop fixed it. 8 s at 250 ms is their measured-working bound and is generous against
1032
+ // the ~150 ms gap, without turning a genuinely failed launch into a slow failure.
1001
1033
  if (clean) {
1002
- const recovered = await this.adoptRunningBrowser('after a hand-off');
1003
- if (recovered !== undefined)
1004
- return recovered;
1034
+ const deadline = Date.now() + HANDOFF_ADOPT_MS;
1035
+ for (;;) {
1036
+ const recovered = await this.adoptRunningBrowser('after a hand-off');
1037
+ if (recovered !== undefined)
1038
+ return recovered;
1039
+ if (this.disposed || Date.now() >= deadline)
1040
+ break;
1041
+ await new Promise(resolve => setTimeout(resolve, PORT_PROBE_MS));
1042
+ }
1005
1043
  }
1006
1044
  throw new Error(clean
1007
1045
  ? `dsh-builtin-browser: ${target} exited immediately after launch (exit code 0). `
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-builtin-browser",
3
- "version": "0.4.14",
3
+ "version": "0.4.15",
4
4
  "description": "Shared real browser plugin for DeepSeek Harness: install-and-use — a visible native browser the human can take over, driven by the agent over CDP. DOM-level interaction (React/Vue safe), per-task session isolation, cookie persistence (dsh_browser_auth), CAPTCHA detection, batch form fill (dsh_browser_fill), authenticated download, history/replay, action restriction",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",
@@ -68,6 +68,21 @@ const CLOSE_GRACE_MS = 6_000
68
68
  */
69
69
  const PORT_PROBE_MS = 250
70
70
 
71
+ /**
72
+ * How long to keep trying to adopt a browser after a launcher reported a clean exit.
73
+ *
74
+ * Exit code 0 means the launcher handed the request to an instance already holding this profile —
75
+ * but on Windows that stub exits BEFORE the real browser writes `DevToolsActivePort`. Measured on
76
+ * Chrome 156: stub gone at 300 ms, port file present at 441 ms (issue #33). A single attempt read
77
+ * too early and reported a launch failure for a browser that was starting normally, so this is a
78
+ * retry rather than one shot.
79
+ *
80
+ * 8 s is the reporter's measured-working bound and is generous against that ~150 ms gap: an honest
81
+ * hand-off resolves in well under a second, and a launch that genuinely failed still reaches the
82
+ * error within the same budget the CDP loop already allows.
83
+ */
84
+ const HANDOFF_ADOPT_MS = 8_000
85
+
71
86
  /**
72
87
  * Starts a browser process and returns a handle to it.
73
88
  *
@@ -769,7 +784,18 @@ export class SystemBrowserViewHost implements ElectronBrowserViewHost {
769
784
  // /dev/shm far smaller than a desktop. See resolveLaunchMode for why this is conditional.
770
785
  ...launch.headless ? ['--headless=new', '--disable-gpu', '--disable-dev-shm-usage'] : [],
771
786
  ...this.extraArgs,
772
- 'about:blank',
787
+ // NOT `about:blank`. Every page this plugin drives is created by `Target.createTarget` when a
788
+ // session first needs one (`buildSession`, which adopts a blank if the browser opened one and
789
+ // creates one otherwise), so a startup page is a tab nobody asked for and nobody closes: the
790
+ // tab strip showed `about:blank` next to the real page while `dsh_browser_list_tabs` reported
791
+ // only the real one. Reported as issue #33, banner and all.
792
+ //
793
+ // `--no-startup-window` keeps the browser alive with no window and no initial page; measured
794
+ // by the reporter on Chrome 156 as writing DevToolsActivePort in 599 ms, answering
795
+ // `/json/version`, listing two extension targets and no page, and then creating windows
796
+ // normally through `Target.createTarget`. A browser that does start a blank despite this is
797
+ // still handled: `normalizeBlankPages` closes the surplus and `buildSession` adopts the rest.
798
+ '--no-startup-window',
773
799
  ]
774
800
  // Chromium's own sandbox cannot start in some of these environments — running as root, or
775
801
  // inside a container without the namespaces it needs — and it exits rather than degrading.
@@ -984,9 +1010,21 @@ export class SystemBrowserViewHost implements ElectronBrowserViewHost {
984
1010
  // Exit code 0 IS the hand-off: the request went to an instance already holding this profile,
985
1011
  // so that instance is our browser and it is running right now. Adopting it turns the
986
1012
  // reported "kill the leftovers by hand, on every restart" into a call that just works.
1013
+ //
1014
+ // Adopted with a bounded RETRY, because on Windows the launcher stub exits before the browser
1015
+ // has written its port file: measured 300 ms to exit against 441 ms for DevToolsActivePort.
1016
+ // One attempt therefore read too early and reported "exited immediately after launch" for a
1017
+ // browser that was starting normally — reported as issue #33, where the reporter's own
1018
+ // retry loop fixed it. 8 s at 250 ms is their measured-working bound and is generous against
1019
+ // the ~150 ms gap, without turning a genuinely failed launch into a slow failure.
987
1020
  if (clean) {
988
- const recovered = await this.adoptRunningBrowser('after a hand-off')
989
- if (recovered !== undefined) return recovered
1021
+ const deadline = Date.now() + HANDOFF_ADOPT_MS
1022
+ for (;;) {
1023
+ const recovered = await this.adoptRunningBrowser('after a hand-off')
1024
+ if (recovered !== undefined) return recovered
1025
+ if (this.disposed || Date.now() >= deadline) break
1026
+ await new Promise(resolve => setTimeout(resolve, PORT_PROBE_MS))
1027
+ }
990
1028
  }
991
1029
  throw new Error(
992
1030
  clean