dsh-builtin-browser 0.4.13 → 0.4.14
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 +44 -0
- package/docs/limitations.md +14 -0
- package/lib/browser-electron/entry.js +3 -1
- package/lib/browser-electron/settings-store.d.ts +24 -0
- package/lib/browser-electron/settings-store.js +34 -2
- package/lib/browser-electron/system-browser.d.ts +12 -1
- package/lib/browser-electron/system-browser.js +109 -1
- package/package.json +1 -1
- package/src/browser-electron/entry.ts +4 -1
- package/src/browser-electron/settings-store.ts +58 -2
- package/src/browser-electron/system-browser.ts +104 -0
package/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,50 @@
|
|
|
4
4
|
|
|
5
5
|
---
|
|
6
6
|
|
|
7
|
+
# 第五十一轮(2026-10-19,无显示环境无法启动浏览器:自动 headless)
|
|
8
|
+
|
|
9
|
+
报告者把一个 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`。
|
|
10
|
+
|
|
11
|
+
他给了本地补丁(改在 `node_modules` 里)、端到端验证(重启服务后成功打开页面并产出 780×437 的 PNG),并明确要求**上游做成设置驱动**,因为 `node_modules` 的改动会被插件更新覆盖。
|
|
12
|
+
|
|
13
|
+
## 一、核实
|
|
14
|
+
|
|
15
|
+
`start()` 的参数确实只有 `--remote-debugging-port=0`、`--user-data-dir`、`--no-first-run`、`--no-default-browser-check`、`--disable-features`、两个 bubble 开关 —— **没有 `--headless`,也没有 `--no-sandbox`**。而 `--headless` 缺席时,无显示的 Chromium 必然退出。这不是配置问题:桌面端有显示所以能开窗,服务端没有所以必然失败,而服务端恰好是"用截图把结果回传"的主流用法。
|
|
16
|
+
|
|
17
|
+
## 二、改法:设置项 + 环境变量,而不是只在源码里加参数
|
|
18
|
+
|
|
19
|
+
新增 **`browser.headless`**,取值 `auto`(默认) / `headless` / `windowed`:
|
|
20
|
+
|
|
21
|
+
| 值 | 行为 |
|
|
22
|
+
| --- | --- |
|
|
23
|
+
| `auto` | 有显示 → 窗口;**无显示 → headless** |
|
|
24
|
+
| `headless` | 强制 headless |
|
|
25
|
+
| `windowed` | 强制窗口;显示不可用时**明确失败**,不悄悄退化 |
|
|
26
|
+
|
|
27
|
+
**为什么是三个字符串而不是布尔三元组**:JSON 文档里混用 string 与 boolean 会产生歧义(手写 `true` 与 `"true"` 是两回事)。但手写 `true` / `false` 仍然接受,分别映射到 `headless` / `windowed` —— 那是任何人第一次写这个设置时会写的形态,因为类型差异拒绝它属于咬文嚼字。任何不认识的值都会被**报告**并回落到 `auto`,理由与 `browser.channel` 相同:被丢弃的值和生效的值看起来必须不一样。
|
|
28
|
+
|
|
29
|
+
**环境变量优先于设置**:`DSH_BROWSER_HEADLESS=1` / `=0` 覆盖设置项。报告者第 4.3 条提的就是这个 —— 运维改一个运行中的部署不该需要编辑文件。测试里专门断言了**覆盖在模式判断之前生效**,否则存下来的设置会压过运维的应急开关。
|
|
30
|
+
|
|
31
|
+
## 三、headless 时补的三个开关与一条安全约束
|
|
32
|
+
|
|
33
|
+
`--headless=new`(不是旧 `--headless`):新模式的行为更接近有窗口的浏览器,而本插件所有命令都是对着有窗口的浏览器写并实测的 —— 包括 CDP 输入,Chromium 会静默丢弃"它认为没有显示表面"的视图的输入。
|
|
34
|
+
|
|
35
|
+
`--disable-gpu`、`--disable-dev-shm-usage`:无 GPU、`/dev/shm` 远小于桌面的环境里,这两个是让 headless 可用的前提。
|
|
36
|
+
|
|
37
|
+
**`--no-sandbox` 只在必要时加**,按报告者第 4.4 条:只有"以 root 运行"或"在容器内"(`/.dockerenv` 或 `container` 环境变量)才加,并且**在 stderr 明确写出这次降级**。默认仍走沙箱;降低进程隔离这件事必须让人能看见并反对。
|
|
38
|
+
|
|
39
|
+
## 四、验证:先证明断言能区分修复前后
|
|
40
|
+
|
|
41
|
+
5 条新断言,把源码回退到修复前跑同一组:**4 条失败**(其中"启动参数"那条等了 30 秒超时 —— 那正是没有 `--headless` 时浏览器起不来的表现),修复后 5 条全过。唯一在旧代码上也通过的是"windowed 不加 headless",它本来就该一直成立。
|
|
42
|
+
|
|
43
|
+
`--no-sandbox` 那条是静态断言(检查它挂在 `headless && needsNoSandbox()` 条件下、且会打印),因为本机是 Windows、`needsNoSandbox()` 恒为 false,无法在这里触发真实的 root/容器分支。
|
|
44
|
+
|
|
45
|
+
**未验证部分**:本次没有在真实的 systemd / 无显示环境里跑过。报告者已经用等价补丁做过端到端验证(页面打开 + PNG 产出),而我实现的路径与他的补丁在同一处、同样的开关;但仍需在真实无显示环境复测,这一点如实记下。
|
|
46
|
+
|
|
47
|
+
**测试**:290 → 295(新增 5 条:`tests/headless-launch.test.mjs`)。`tsc` 干净。
|
|
48
|
+
|
|
49
|
+
---
|
|
50
|
+
|
|
7
51
|
# 第五十轮(2026-10-17,对齐 DSH 0.2.1 的插件契约 + 工具描述瘦身)
|
|
8
52
|
|
|
9
53
|
## 一、版本范围改到 DSH 真正读取的位置
|
package/docs/limitations.md
CHANGED
|
@@ -32,6 +32,20 @@
|
|
|
32
32
|
|
|
33
33
|
## 载体行为
|
|
34
34
|
|
|
35
|
+
- **无显示环境会自动改用 headless**,不需要额外配置。Chromium 在没有 `DISPLAY` 与 `WAYLAND_DISPLAY` 时会以 `could not connect to display` 立即退出,而 systemd 服务、容器、只有 SSH 的服务器正好都是这个状态——那偏偏是服务端浏览器最有用的部署形态。`browser.headless` 的默认值 `auto` 就是"有显示开窗口,没显示走 headless",桌面端与侧栏用户不受影响。
|
|
36
|
+
|
|
37
|
+
想显式控制时:
|
|
38
|
+
|
|
39
|
+
| 设置 | 行为 |
|
|
40
|
+
| --- | --- |
|
|
41
|
+
| `"auto"`(默认) | 有显示 → 窗口;无显示 → headless |
|
|
42
|
+
| `"headless"` | 强制 headless(服务器 / 截图专用 / CI) |
|
|
43
|
+
| `"windowed"` | 强制窗口;显示不可用时**直接报错**,而不是悄悄退化 |
|
|
44
|
+
|
|
45
|
+
手写 `true` / `false` 也接受,分别等价于 `headless` / `windowed`。运行中的部署可以用环境变量覆盖设置,不必改文件:`DSH_BROWSER_HEADLESS=1` 强制 headless,`=0` 强制窗口(环境变量优先于设置项)。
|
|
46
|
+
|
|
47
|
+
headless 启动会自动带上 `--disable-gpu` 与 `--disable-dev-shm-usage`;`--no-sandbox` **只在必要时**加(以 root 运行,或在容器内),并且会在 stderr 明确写出——降低进程隔离这件事必须让人能看见并反对。
|
|
48
|
+
|
|
35
49
|
- **侧栏载体有前置条件**,不是装上 bridge 就成立。官方侧栏浏览器面板只在**外壳同时满足**两个条件时才是 `<webview>`:
|
|
36
50
|
1. `preload` 暴露 `dshDesktop.browser`(而不只是 `protocolVersion`);
|
|
37
51
|
2. 外壳窗口开启 `webviewTag`。
|
|
@@ -294,7 +294,9 @@ export function apply(ctx, config) {
|
|
|
294
294
|
sweepAbandonedEphemeralProfiles(profileRoot, detected.kind);
|
|
295
295
|
claimEphemeralProfile(profileDir);
|
|
296
296
|
}
|
|
297
|
-
|
|
297
|
+
// The headless mode rides along so a display-less host (systemd, container, SSH-only server)
|
|
298
|
+
// still gets a working browser instead of "exited immediately after launch" — issue #32.
|
|
299
|
+
adopt(new SystemBrowserViewHost(detected, profileDir, [], persist ? undefined : profileDir, undefined, settings.get().browser.headless), `will drive the installed ${detected.kind} (${detected.path}${persist ? '' : ', ephemeral profile'})`);
|
|
298
300
|
}
|
|
299
301
|
}
|
|
300
302
|
// An explicit choice was already adopted above; discovering the sidebar here would
|
|
@@ -11,6 +11,15 @@
|
|
|
11
11
|
import type { BrowserChannel } from './system-browser.js';
|
|
12
12
|
/** Vision/operation strategy the provider prefers when the model can see. */
|
|
13
13
|
export type VisionStrategy = 'auto' | 'nonVisual';
|
|
14
|
+
/**
|
|
15
|
+
* Whether a launched browser gets a window.
|
|
16
|
+
*
|
|
17
|
+
* Split from {@link VisionStrategy} because these answer different questions: that one is how the
|
|
18
|
+
* agent examines a page, this one is whether the browser has anywhere to draw.
|
|
19
|
+
*/
|
|
20
|
+
export type HeadlessMode = 'auto' | 'headless' | 'windowed';
|
|
21
|
+
/** Every value {@link HeadlessMode} accepts, for validation and the settings UI. */
|
|
22
|
+
export declare const HEADLESS_MODES: readonly ["auto", "headless", "windowed"];
|
|
14
23
|
/** The full settings document, with every field resolved. */
|
|
15
24
|
export interface BrowserSettings {
|
|
16
25
|
/** Persistent browsing history (visited pages). */
|
|
@@ -39,6 +48,21 @@ export interface BrowserSettings {
|
|
|
39
48
|
/** Which browser binary carries the agent's pages. */
|
|
40
49
|
readonly browser: {
|
|
41
50
|
readonly channel: BrowserChannel;
|
|
51
|
+
/**
|
|
52
|
+
* Whether the browser gets a window.
|
|
53
|
+
*
|
|
54
|
+
* `auto` (the default) launches headless when the process has no display — no `DISPLAY` and no
|
|
55
|
+
* `WAYLAND_DISPLAY` — and with a window otherwise. That combination is what makes the plugin
|
|
56
|
+
* work under systemd, in a container, or on an SSH-only server while leaving desktop users
|
|
57
|
+
* with the visible window they expect. `true` forces headless (CI, screenshot-only servers);
|
|
58
|
+
* `false` forces a window, so a misconfigured display fails loudly instead of silently
|
|
59
|
+
* degrading into a browser nobody can see.
|
|
60
|
+
*
|
|
61
|
+
* Reported as issue #32: as a systemd service there is no display, Chromium exits with
|
|
62
|
+
* "could not connect to display", and every `dsh_browser_*` call failed with "exited
|
|
63
|
+
* immediately after launch".
|
|
64
|
+
*/
|
|
65
|
+
readonly headless: HeadlessMode;
|
|
42
66
|
};
|
|
43
67
|
/** Whether the agent may read cookies / export login state. */
|
|
44
68
|
readonly credentials: {
|
|
@@ -11,6 +11,8 @@
|
|
|
11
11
|
import { mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
12
12
|
import { homedir } from 'node:os';
|
|
13
13
|
import { dirname, join } from 'node:path';
|
|
14
|
+
/** Every value {@link HeadlessMode} accepts, for validation and the settings UI. */
|
|
15
|
+
export const HEADLESS_MODES = ['auto', 'headless', 'windowed'];
|
|
14
16
|
/** Defaults for every switch: the documented out-of-the-box behaviour. */
|
|
15
17
|
export const DEFAULT_SETTINGS = {
|
|
16
18
|
history: { enabled: true, maxEntries: 5_000, maxAgeDays: 90 },
|
|
@@ -20,7 +22,7 @@ export const DEFAULT_SETTINGS = {
|
|
|
20
22
|
// Bundled by default: it needs nothing installed, and on the desktop the carrier
|
|
21
23
|
// is the sidebar anyway. Choosing a system browser is an explicit opt-in because
|
|
22
24
|
// it starts a real Chrome/Edge with its own profile.
|
|
23
|
-
browser: { channel: 'bundled' },
|
|
25
|
+
browser: { channel: 'bundled', headless: 'auto' },
|
|
24
26
|
credentials: { allowRead: true },
|
|
25
27
|
actions: { allowExecute: true, allowDownload: true, allowCredentialWrite: true },
|
|
26
28
|
};
|
|
@@ -75,6 +77,33 @@ const SETTINGS_WHEN_UNREADABLE = {
|
|
|
75
77
|
* message that lists a value the code rejects (or omits one it takes) is worse than no message.
|
|
76
78
|
*/
|
|
77
79
|
export const BROWSER_CHANNELS = ['chrome', 'edge', 'auto', 'bundled', 'sidebar'];
|
|
80
|
+
/**
|
|
81
|
+
* Coerce the headless mode, keeping `auto` when the value is not one of the known choices.
|
|
82
|
+
*
|
|
83
|
+
* Booleans are accepted as a convenience because "headless: true" is what anyone writing this by
|
|
84
|
+
* hand would try first, and rejecting it over a type difference would be pedantry: `true` maps to
|
|
85
|
+
* `headless`, `false` to `windowed`. Anything else is reported rather than silently defaulted,
|
|
86
|
+
* for the same reason as {@link resolveChannel}: a discarded value and an honoured one must not
|
|
87
|
+
* look alike.
|
|
88
|
+
* @param value - the raw value from the settings document.
|
|
89
|
+
* @param warn - where to report a discarded value.
|
|
90
|
+
* @returns a valid mode.
|
|
91
|
+
*/
|
|
92
|
+
function resolveHeadless(value, warn) {
|
|
93
|
+
if (value === undefined)
|
|
94
|
+
return DEFAULT_SETTINGS.browser.headless;
|
|
95
|
+
if (value === true)
|
|
96
|
+
return 'headless';
|
|
97
|
+
if (value === false)
|
|
98
|
+
return 'windowed';
|
|
99
|
+
if (typeof value === 'string' && HEADLESS_MODES.includes(value)) {
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
warn?.(`dsh-builtin-browser: browser.headless ${JSON.stringify(value)} is not a known mode, so the `
|
|
103
|
+
+ `default "${DEFAULT_SETTINGS.browser.headless}" was used instead. Expected one of: `
|
|
104
|
+
+ `${HEADLESS_MODES.join(', ')} (or a boolean).`);
|
|
105
|
+
return DEFAULT_SETTINGS.browser.headless;
|
|
106
|
+
}
|
|
78
107
|
/**
|
|
79
108
|
* Coerce the browser channel, keeping the default when it is not one of the
|
|
80
109
|
* known choices. A hand-edited typo must not silently select a different browser.
|
|
@@ -164,7 +193,10 @@ export function resolveSettings(raw, warn) {
|
|
|
164
193
|
virtualCursor: bool(ui?.virtualCursor, DEFAULT_SETTINGS.ui.virtualCursor),
|
|
165
194
|
},
|
|
166
195
|
vision: { strategy: vision?.strategy === 'nonVisual' ? 'nonVisual' : DEFAULT_SETTINGS.vision.strategy },
|
|
167
|
-
browser: {
|
|
196
|
+
browser: {
|
|
197
|
+
channel: resolveChannel(browser?.channel, warn),
|
|
198
|
+
headless: resolveHeadless(browser?.headless, warn),
|
|
199
|
+
},
|
|
168
200
|
credentials: {
|
|
169
201
|
// `gate` returns false for anything present-and-not-a-boolean, so an unreadable section
|
|
170
202
|
// is refused here for free: the section itself is the "present but wrong" value.
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
* only the carrier differs.
|
|
25
25
|
* @module dsh-browser/browser-electron/system-browser
|
|
26
26
|
*/
|
|
27
|
+
import type { HeadlessMode } from './settings-store.js';
|
|
27
28
|
/**
|
|
28
29
|
* Starts a browser process and returns a handle to it.
|
|
29
30
|
*
|
|
@@ -79,6 +80,11 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
|
|
|
79
80
|
private readonly extraArgs;
|
|
80
81
|
private readonly ephemeralDir?;
|
|
81
82
|
private readonly launcher;
|
|
83
|
+
/**
|
|
84
|
+
* Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
|
|
85
|
+
* and why a display-less host needs headless at all (issue #32).
|
|
86
|
+
*/
|
|
87
|
+
private readonly headlessMode;
|
|
82
88
|
/**
|
|
83
89
|
* viewId -> the session that view rides, and the client that issued it.
|
|
84
90
|
*
|
|
@@ -145,7 +151,12 @@ export declare class SystemBrowserViewHost implements ElectronBrowserViewHost {
|
|
|
145
151
|
* removing every sessions.clear() left the recovery tests green. A launcher lets a test
|
|
146
152
|
* provide something that actually speaks CDP, so the state those tests describe exists.
|
|
147
153
|
*/
|
|
148
|
-
constructor(browser: DetectedBrowser, profileDir: string, extraArgs?: readonly string[], ephemeralDir?: string | undefined, launcher?: BrowserLauncher
|
|
154
|
+
constructor(browser: DetectedBrowser, profileDir: string, extraArgs?: readonly string[], ephemeralDir?: string | undefined, launcher?: BrowserLauncher,
|
|
155
|
+
/**
|
|
156
|
+
* Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
|
|
157
|
+
* and why a display-less host needs headless at all (issue #32).
|
|
158
|
+
*/
|
|
159
|
+
headlessMode?: HeadlessMode);
|
|
149
160
|
/** Which product this host would drive (for diagnostics). */
|
|
150
161
|
get kind(): DetectedBrowser['kind'];
|
|
151
162
|
/** Whether the browser has actually been started yet (diagnostics and tests). */
|
|
@@ -168,6 +168,86 @@ const INSTALL_LOCATIONS = {
|
|
|
168
168
|
function isBrowserProduct(product) {
|
|
169
169
|
return typeof product === 'string' && /chrome|chromium|edg/i.test(product);
|
|
170
170
|
}
|
|
171
|
+
/**
|
|
172
|
+
* Whether this process has somewhere to draw a window.
|
|
173
|
+
*
|
|
174
|
+
* The same test Chromium makes: with neither `DISPLAY` nor `WAYLAND_DISPLAY` set it cannot open a
|
|
175
|
+
* window and exits with "could not connect to display". Declared here rather than in
|
|
176
|
+
* settings-store because that module imports from this one, and a value import back would be a
|
|
177
|
+
* cycle; the equivalent export there delegates to this.
|
|
178
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
179
|
+
* @returns true when a display is advertised.
|
|
180
|
+
*/
|
|
181
|
+
function hasDisplay(env = process.env) {
|
|
182
|
+
// Windows and macOS use neither variable, and always have a window server.
|
|
183
|
+
if (process.platform === 'win32' || process.platform === 'darwin')
|
|
184
|
+
return true;
|
|
185
|
+
return (env.DISPLAY ?? '') !== '' || (env.WAYLAND_DISPLAY ?? '') !== '';
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Whether Chromium's own sandbox cannot start here.
|
|
189
|
+
*
|
|
190
|
+
* Two cases, both of which make the browser exit at once rather than degrade: running as root
|
|
191
|
+
* (the sandbox refuses), and running inside a container that lacks the namespaces it needs. The
|
|
192
|
+
* check is deliberately conservative — when it is unsure it answers false, so the default is the
|
|
193
|
+
* sandboxed launch and the weaker mode only happens where it is required.
|
|
194
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
195
|
+
* @returns true when `--no-sandbox` is required for the browser to start at all.
|
|
196
|
+
*/
|
|
197
|
+
function needsNoSandbox(env = process.env) {
|
|
198
|
+
if (process.platform === 'win32')
|
|
199
|
+
return false;
|
|
200
|
+
try {
|
|
201
|
+
if (typeof process.getuid === 'function' && process.getuid() === 0)
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
catch {
|
|
205
|
+
// No getuid: not a POSIX process; the container checks below still apply.
|
|
206
|
+
}
|
|
207
|
+
if ((env.container ?? '') !== '')
|
|
208
|
+
return true;
|
|
209
|
+
try {
|
|
210
|
+
if (existsSync('/.dockerenv'))
|
|
211
|
+
return true;
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
// An unreadable root is not evidence of a container.
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Resolve `auto` into a concrete decision, and say why.
|
|
220
|
+
*
|
|
221
|
+
* Chromium refuses to start without a display: with neither `DISPLAY` nor `WAYLAND_DISPLAY` set it
|
|
222
|
+
* exits at once with "could not connect to display", and the caller sees only "exited immediately
|
|
223
|
+
* after launch (exit code 1)". That is the ordinary state of a systemd service, a container, and an
|
|
224
|
+
* SSH-only server — the deployment where a server-side browser is most useful, and the one reported
|
|
225
|
+
* as issue #32: every `dsh_browser_*` call failed, with nothing in the message pointing at the
|
|
226
|
+
* display.
|
|
227
|
+
*
|
|
228
|
+
* `auto` therefore launches headless exactly when there is no display, which leaves desktop and
|
|
229
|
+
* sidebar users with the visible window they expect. An environment override wins over the setting
|
|
230
|
+
* so an operator can fix a running deployment without editing settings: `DSH_BROWSER_HEADLESS=1`
|
|
231
|
+
* forces headless, `=0` forces a window.
|
|
232
|
+
* @param mode - the configured mode.
|
|
233
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
234
|
+
* @returns whether to launch headless, and a one-line reason for the log.
|
|
235
|
+
*/
|
|
236
|
+
function resolveLaunchMode(mode, env = process.env) {
|
|
237
|
+
const override = env.DSH_BROWSER_HEADLESS;
|
|
238
|
+
if (override === '1' || override === 'true')
|
|
239
|
+
return { headless: true, why: 'DSH_BROWSER_HEADLESS' };
|
|
240
|
+
if (override === '0' || override === 'false')
|
|
241
|
+
return { headless: false, why: 'DSH_BROWSER_HEADLESS' };
|
|
242
|
+
if (mode === 'headless')
|
|
243
|
+
return { headless: true, why: 'browser.headless = headless' };
|
|
244
|
+
if (mode === 'windowed')
|
|
245
|
+
return { headless: false, why: 'browser.headless = windowed' };
|
|
246
|
+
const display = hasDisplay(env);
|
|
247
|
+
return display
|
|
248
|
+
? { headless: false, why: 'a display is available' }
|
|
249
|
+
: { headless: true, why: 'no DISPLAY or WAYLAND_DISPLAY' };
|
|
250
|
+
}
|
|
171
251
|
/**
|
|
172
252
|
* Whether a target URL is one of the blanks a browser opens by itself.
|
|
173
253
|
*
|
|
@@ -388,6 +468,7 @@ export class SystemBrowserViewHost {
|
|
|
388
468
|
extraArgs;
|
|
389
469
|
ephemeralDir;
|
|
390
470
|
launcher;
|
|
471
|
+
headlessMode;
|
|
391
472
|
/**
|
|
392
473
|
* viewId -> the session that view rides, and the client that issued it.
|
|
393
474
|
*
|
|
@@ -454,12 +535,18 @@ export class SystemBrowserViewHost {
|
|
|
454
535
|
* removing every sessions.clear() left the recovery tests green. A launcher lets a test
|
|
455
536
|
* provide something that actually speaks CDP, so the state those tests describe exists.
|
|
456
537
|
*/
|
|
457
|
-
constructor(browser, profileDir, extraArgs = [], ephemeralDir, launcher = defaultLauncher
|
|
538
|
+
constructor(browser, profileDir, extraArgs = [], ephemeralDir, launcher = defaultLauncher,
|
|
539
|
+
/**
|
|
540
|
+
* Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
|
|
541
|
+
* and why a display-less host needs headless at all (issue #32).
|
|
542
|
+
*/
|
|
543
|
+
headlessMode = 'auto') {
|
|
458
544
|
this.browser = browser;
|
|
459
545
|
this.profileDir = profileDir;
|
|
460
546
|
this.extraArgs = extraArgs;
|
|
461
547
|
this.ephemeralDir = ephemeralDir;
|
|
462
548
|
this.launcher = launcher;
|
|
549
|
+
this.headlessMode = headlessMode;
|
|
463
550
|
}
|
|
464
551
|
/** Which product this host would drive (for diagnostics). */
|
|
465
552
|
get kind() {
|
|
@@ -639,6 +726,7 @@ export class SystemBrowserViewHost {
|
|
|
639
726
|
const adopted = await this.adoptRunningBrowser('before launch');
|
|
640
727
|
if (adopted !== undefined)
|
|
641
728
|
return adopted;
|
|
729
|
+
const launch = resolveLaunchMode(this.headlessMode);
|
|
642
730
|
const args = [
|
|
643
731
|
'--remote-debugging-port=0',
|
|
644
732
|
`--user-data-dir=${this.profileDir}`,
|
|
@@ -649,9 +737,29 @@ export class SystemBrowserViewHost {
|
|
|
649
737
|
'--disable-features=Translate,MediaRouter',
|
|
650
738
|
'--disable-session-crashed-bubble',
|
|
651
739
|
'--hide-crash-restore-bubble',
|
|
740
|
+
// Headless only where there is nothing to draw on. `--headless=new` rather than the legacy
|
|
741
|
+
// flag: the new mode behaves like a windowed browser, which matters because every command
|
|
742
|
+
// here was written and measured against one — including CDP input, which Chromium silently
|
|
743
|
+
// drops for a view it believes has no display surface. `--disable-gpu` and
|
|
744
|
+
// `--disable-dev-shm-usage` are what make that mode usable on a server: no GPU, and a
|
|
745
|
+
// /dev/shm far smaller than a desktop. See resolveLaunchMode for why this is conditional.
|
|
746
|
+
...launch.headless ? ['--headless=new', '--disable-gpu', '--disable-dev-shm-usage'] : [],
|
|
652
747
|
...this.extraArgs,
|
|
653
748
|
'about:blank',
|
|
654
749
|
];
|
|
750
|
+
// Chromium's own sandbox cannot start in some of these environments — running as root, or
|
|
751
|
+
// inside a container without the namespaces it needs — and it exits rather than degrading.
|
|
752
|
+
// Only then is the sandbox dropped, and never silently: a setting that weakens process
|
|
753
|
+
// isolation must be visible in the log, because whoever reads it has to be able to disagree.
|
|
754
|
+
if (launch.headless && needsNoSandbox()) {
|
|
755
|
+
args.splice(args.length - 2, 0, '--no-sandbox');
|
|
756
|
+
process.stderr.write('[dsh-browser] launching with --no-sandbox: '
|
|
757
|
+
+ 'this process runs as root or in a container, where the Chromium sandbox cannot start\n');
|
|
758
|
+
}
|
|
759
|
+
if (launch.headless) {
|
|
760
|
+
process.stderr.write(`[dsh-browser] launching headless (${launch.why}); `
|
|
761
|
+
+ 'the browser has no window but every tool works the same\n');
|
|
762
|
+
}
|
|
655
763
|
// A browser from a previous attempt may still be running with this same
|
|
656
764
|
// --user-data-dir. On Windows the new one would merely hand off to it and exit, and that
|
|
657
765
|
// exit would then be treated as the launch failing.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-builtin-browser",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.14",
|
|
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",
|
|
@@ -327,7 +327,10 @@ export function apply(ctx: Context & { browser: BrowserRuntime }, config: Config
|
|
|
327
327
|
sweepAbandonedEphemeralProfiles(profileRoot, detected.kind)
|
|
328
328
|
claimEphemeralProfile(profileDir)
|
|
329
329
|
}
|
|
330
|
-
|
|
330
|
+
// The headless mode rides along so a display-less host (systemd, container, SSH-only server)
|
|
331
|
+
// still gets a working browser instead of "exited immediately after launch" — issue #32.
|
|
332
|
+
adopt(new SystemBrowserViewHost(detected, profileDir, [], persist ? undefined : profileDir,
|
|
333
|
+
undefined, settings.get().browser.headless),
|
|
331
334
|
`will drive the installed ${detected.kind} (${detected.path}${persist ? '' : ', ephemeral profile'})`)
|
|
332
335
|
}
|
|
333
336
|
}
|
|
@@ -17,6 +17,17 @@ import type { BrowserChannel } from './system-browser.js'
|
|
|
17
17
|
/** Vision/operation strategy the provider prefers when the model can see. */
|
|
18
18
|
export type VisionStrategy = 'auto' | 'nonVisual'
|
|
19
19
|
|
|
20
|
+
/**
|
|
21
|
+
* Whether a launched browser gets a window.
|
|
22
|
+
*
|
|
23
|
+
* Split from {@link VisionStrategy} because these answer different questions: that one is how the
|
|
24
|
+
* agent examines a page, this one is whether the browser has anywhere to draw.
|
|
25
|
+
*/
|
|
26
|
+
export type HeadlessMode = 'auto' | 'headless' | 'windowed'
|
|
27
|
+
|
|
28
|
+
/** Every value {@link HeadlessMode} accepts, for validation and the settings UI. */
|
|
29
|
+
export const HEADLESS_MODES = ['auto', 'headless', 'windowed'] as const
|
|
30
|
+
|
|
20
31
|
/** The full settings document, with every field resolved. */
|
|
21
32
|
export interface BrowserSettings {
|
|
22
33
|
/** Persistent browsing history (visited pages). */
|
|
@@ -45,6 +56,21 @@ export interface BrowserSettings {
|
|
|
45
56
|
/** Which browser binary carries the agent's pages. */
|
|
46
57
|
readonly browser: {
|
|
47
58
|
readonly channel: BrowserChannel
|
|
59
|
+
/**
|
|
60
|
+
* Whether the browser gets a window.
|
|
61
|
+
*
|
|
62
|
+
* `auto` (the default) launches headless when the process has no display — no `DISPLAY` and no
|
|
63
|
+
* `WAYLAND_DISPLAY` — and with a window otherwise. That combination is what makes the plugin
|
|
64
|
+
* work under systemd, in a container, or on an SSH-only server while leaving desktop users
|
|
65
|
+
* with the visible window they expect. `true` forces headless (CI, screenshot-only servers);
|
|
66
|
+
* `false` forces a window, so a misconfigured display fails loudly instead of silently
|
|
67
|
+
* degrading into a browser nobody can see.
|
|
68
|
+
*
|
|
69
|
+
* Reported as issue #32: as a systemd service there is no display, Chromium exits with
|
|
70
|
+
* "could not connect to display", and every `dsh_browser_*` call failed with "exited
|
|
71
|
+
* immediately after launch".
|
|
72
|
+
*/
|
|
73
|
+
readonly headless: HeadlessMode
|
|
48
74
|
}
|
|
49
75
|
/** Whether the agent may read cookies / export login state. */
|
|
50
76
|
readonly credentials: {
|
|
@@ -77,7 +103,7 @@ export const DEFAULT_SETTINGS: BrowserSettings = {
|
|
|
77
103
|
// Bundled by default: it needs nothing installed, and on the desktop the carrier
|
|
78
104
|
// is the sidebar anyway. Choosing a system browser is an explicit opt-in because
|
|
79
105
|
// it starts a real Chrome/Edge with its own profile.
|
|
80
|
-
browser: { channel: 'bundled' },
|
|
106
|
+
browser: { channel: 'bundled', headless: 'auto' },
|
|
81
107
|
credentials: { allowRead: true },
|
|
82
108
|
actions: { allowExecute: true, allowDownload: true, allowCredentialWrite: true },
|
|
83
109
|
}
|
|
@@ -137,6 +163,33 @@ const SETTINGS_WHEN_UNREADABLE: BrowserSettings = {
|
|
|
137
163
|
*/
|
|
138
164
|
export const BROWSER_CHANNELS = ['chrome', 'edge', 'auto', 'bundled', 'sidebar'] as const
|
|
139
165
|
|
|
166
|
+
/**
|
|
167
|
+
* Coerce the headless mode, keeping `auto` when the value is not one of the known choices.
|
|
168
|
+
*
|
|
169
|
+
* Booleans are accepted as a convenience because "headless: true" is what anyone writing this by
|
|
170
|
+
* hand would try first, and rejecting it over a type difference would be pedantry: `true` maps to
|
|
171
|
+
* `headless`, `false` to `windowed`. Anything else is reported rather than silently defaulted,
|
|
172
|
+
* for the same reason as {@link resolveChannel}: a discarded value and an honoured one must not
|
|
173
|
+
* look alike.
|
|
174
|
+
* @param value - the raw value from the settings document.
|
|
175
|
+
* @param warn - where to report a discarded value.
|
|
176
|
+
* @returns a valid mode.
|
|
177
|
+
*/
|
|
178
|
+
function resolveHeadless(value: unknown, warn?: (message: string) => void): HeadlessMode {
|
|
179
|
+
if (value === undefined) return DEFAULT_SETTINGS.browser.headless
|
|
180
|
+
if (value === true) return 'headless'
|
|
181
|
+
if (value === false) return 'windowed'
|
|
182
|
+
if (typeof value === 'string' && (HEADLESS_MODES as readonly string[]).includes(value)) {
|
|
183
|
+
return value as HeadlessMode
|
|
184
|
+
}
|
|
185
|
+
warn?.(
|
|
186
|
+
`dsh-builtin-browser: browser.headless ${JSON.stringify(value)} is not a known mode, so the `
|
|
187
|
+
+ `default "${DEFAULT_SETTINGS.browser.headless}" was used instead. Expected one of: `
|
|
188
|
+
+ `${HEADLESS_MODES.join(', ')} (or a boolean).`,
|
|
189
|
+
)
|
|
190
|
+
return DEFAULT_SETTINGS.browser.headless
|
|
191
|
+
}
|
|
192
|
+
|
|
140
193
|
/**
|
|
141
194
|
* Coerce the browser channel, keeping the default when it is not one of the
|
|
142
195
|
* known choices. A hand-edited typo must not silently select a different browser.
|
|
@@ -227,7 +280,10 @@ export function resolveSettings(raw: unknown, warn?: (message: string) => void):
|
|
|
227
280
|
virtualCursor: bool(ui?.virtualCursor, DEFAULT_SETTINGS.ui.virtualCursor),
|
|
228
281
|
},
|
|
229
282
|
vision: { strategy: vision?.strategy === 'nonVisual' ? 'nonVisual' : DEFAULT_SETTINGS.vision.strategy },
|
|
230
|
-
browser: {
|
|
283
|
+
browser: {
|
|
284
|
+
channel: resolveChannel(browser?.channel, warn),
|
|
285
|
+
headless: resolveHeadless(browser?.headless, warn),
|
|
286
|
+
},
|
|
231
287
|
credentials: {
|
|
232
288
|
// `gate` returns false for anything present-and-not-a-boolean, so an unreadable section
|
|
233
289
|
// is refused here for free: the section itself is the "present but wrong" value.
|
|
@@ -28,6 +28,9 @@
|
|
|
28
28
|
import { readFileSync, rmSync, statSync } from 'node:fs'
|
|
29
29
|
import { existsSync } from 'node:fs'
|
|
30
30
|
import { join } from 'node:path'
|
|
31
|
+
// Type-only: settings-store imports BrowserChannel from here, so a value import would be a cycle.
|
|
32
|
+
// `hasDisplay` lives here instead of there for the same reason — it is a runtime function.
|
|
33
|
+
import type { HeadlessMode } from './settings-store.js'
|
|
31
34
|
|
|
32
35
|
/**
|
|
33
36
|
* How long a killed browser is given to actually exit before the launch proceeds.
|
|
@@ -201,6 +204,81 @@ function isBrowserProduct(product: unknown): boolean {
|
|
|
201
204
|
return typeof product === 'string' && /chrome|chromium|edg/i.test(product)
|
|
202
205
|
}
|
|
203
206
|
|
|
207
|
+
/**
|
|
208
|
+
* Whether this process has somewhere to draw a window.
|
|
209
|
+
*
|
|
210
|
+
* The same test Chromium makes: with neither `DISPLAY` nor `WAYLAND_DISPLAY` set it cannot open a
|
|
211
|
+
* window and exits with "could not connect to display". Declared here rather than in
|
|
212
|
+
* settings-store because that module imports from this one, and a value import back would be a
|
|
213
|
+
* cycle; the equivalent export there delegates to this.
|
|
214
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
215
|
+
* @returns true when a display is advertised.
|
|
216
|
+
*/
|
|
217
|
+
function hasDisplay(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
218
|
+
// Windows and macOS use neither variable, and always have a window server.
|
|
219
|
+
if (process.platform === 'win32' || process.platform === 'darwin') return true
|
|
220
|
+
return (env.DISPLAY ?? '') !== '' || (env.WAYLAND_DISPLAY ?? '') !== ''
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Whether Chromium's own sandbox cannot start here.
|
|
225
|
+
*
|
|
226
|
+
* Two cases, both of which make the browser exit at once rather than degrade: running as root
|
|
227
|
+
* (the sandbox refuses), and running inside a container that lacks the namespaces it needs. The
|
|
228
|
+
* check is deliberately conservative — when it is unsure it answers false, so the default is the
|
|
229
|
+
* sandboxed launch and the weaker mode only happens where it is required.
|
|
230
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
231
|
+
* @returns true when `--no-sandbox` is required for the browser to start at all.
|
|
232
|
+
*/
|
|
233
|
+
function needsNoSandbox(env: NodeJS.ProcessEnv = process.env): boolean {
|
|
234
|
+
if (process.platform === 'win32') return false
|
|
235
|
+
try {
|
|
236
|
+
if (typeof process.getuid === 'function' && process.getuid() === 0) return true
|
|
237
|
+
} catch {
|
|
238
|
+
// No getuid: not a POSIX process; the container checks below still apply.
|
|
239
|
+
}
|
|
240
|
+
if ((env.container ?? '') !== '') return true
|
|
241
|
+
try {
|
|
242
|
+
if (existsSync('/.dockerenv')) return true
|
|
243
|
+
} catch {
|
|
244
|
+
// An unreadable root is not evidence of a container.
|
|
245
|
+
}
|
|
246
|
+
return false
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Resolve `auto` into a concrete decision, and say why.
|
|
251
|
+
*
|
|
252
|
+
* Chromium refuses to start without a display: with neither `DISPLAY` nor `WAYLAND_DISPLAY` set it
|
|
253
|
+
* exits at once with "could not connect to display", and the caller sees only "exited immediately
|
|
254
|
+
* after launch (exit code 1)". That is the ordinary state of a systemd service, a container, and an
|
|
255
|
+
* SSH-only server — the deployment where a server-side browser is most useful, and the one reported
|
|
256
|
+
* as issue #32: every `dsh_browser_*` call failed, with nothing in the message pointing at the
|
|
257
|
+
* display.
|
|
258
|
+
*
|
|
259
|
+
* `auto` therefore launches headless exactly when there is no display, which leaves desktop and
|
|
260
|
+
* sidebar users with the visible window they expect. An environment override wins over the setting
|
|
261
|
+
* so an operator can fix a running deployment without editing settings: `DSH_BROWSER_HEADLESS=1`
|
|
262
|
+
* forces headless, `=0` forces a window.
|
|
263
|
+
* @param mode - the configured mode.
|
|
264
|
+
* @param env - the environment to inspect; defaults to the current process.
|
|
265
|
+
* @returns whether to launch headless, and a one-line reason for the log.
|
|
266
|
+
*/
|
|
267
|
+
function resolveLaunchMode(
|
|
268
|
+
mode: HeadlessMode,
|
|
269
|
+
env: NodeJS.ProcessEnv = process.env,
|
|
270
|
+
): { readonly headless: boolean; readonly why: string } {
|
|
271
|
+
const override = env.DSH_BROWSER_HEADLESS
|
|
272
|
+
if (override === '1' || override === 'true') return { headless: true, why: 'DSH_BROWSER_HEADLESS' }
|
|
273
|
+
if (override === '0' || override === 'false') return { headless: false, why: 'DSH_BROWSER_HEADLESS' }
|
|
274
|
+
if (mode === 'headless') return { headless: true, why: 'browser.headless = headless' }
|
|
275
|
+
if (mode === 'windowed') return { headless: false, why: 'browser.headless = windowed' }
|
|
276
|
+
const display = hasDisplay(env)
|
|
277
|
+
return display
|
|
278
|
+
? { headless: false, why: 'a display is available' }
|
|
279
|
+
: { headless: true, why: 'no DISPLAY or WAYLAND_DISPLAY' }
|
|
280
|
+
}
|
|
281
|
+
|
|
204
282
|
/**
|
|
205
283
|
* Whether a target URL is one of the blanks a browser opens by itself.
|
|
206
284
|
*
|
|
@@ -494,6 +572,11 @@ export class SystemBrowserViewHost implements ElectronBrowserViewHost {
|
|
|
494
572
|
private readonly extraArgs: readonly string[] = [],
|
|
495
573
|
private readonly ephemeralDir?: string,
|
|
496
574
|
private readonly launcher: BrowserLauncher = defaultLauncher,
|
|
575
|
+
/**
|
|
576
|
+
* Whether this launch should get a window. See {@link resolveLaunchMode} for how `auto` decides
|
|
577
|
+
* and why a display-less host needs headless at all (issue #32).
|
|
578
|
+
*/
|
|
579
|
+
private readonly headlessMode: HeadlessMode = 'auto',
|
|
497
580
|
) {}
|
|
498
581
|
|
|
499
582
|
/** Which product this host would drive (for diagnostics). */
|
|
@@ -667,6 +750,7 @@ export class SystemBrowserViewHost implements ElectronBrowserViewHost {
|
|
|
667
750
|
// after launch (exit code 0)" that required killing seven processes by hand.
|
|
668
751
|
const adopted = await this.adoptRunningBrowser('before launch')
|
|
669
752
|
if (adopted !== undefined) return adopted
|
|
753
|
+
const launch = resolveLaunchMode(this.headlessMode)
|
|
670
754
|
const args = [
|
|
671
755
|
'--remote-debugging-port=0',
|
|
672
756
|
`--user-data-dir=${this.profileDir}`,
|
|
@@ -677,9 +761,29 @@ export class SystemBrowserViewHost implements ElectronBrowserViewHost {
|
|
|
677
761
|
'--disable-features=Translate,MediaRouter',
|
|
678
762
|
'--disable-session-crashed-bubble',
|
|
679
763
|
'--hide-crash-restore-bubble',
|
|
764
|
+
// Headless only where there is nothing to draw on. `--headless=new` rather than the legacy
|
|
765
|
+
// flag: the new mode behaves like a windowed browser, which matters because every command
|
|
766
|
+
// here was written and measured against one — including CDP input, which Chromium silently
|
|
767
|
+
// drops for a view it believes has no display surface. `--disable-gpu` and
|
|
768
|
+
// `--disable-dev-shm-usage` are what make that mode usable on a server: no GPU, and a
|
|
769
|
+
// /dev/shm far smaller than a desktop. See resolveLaunchMode for why this is conditional.
|
|
770
|
+
...launch.headless ? ['--headless=new', '--disable-gpu', '--disable-dev-shm-usage'] : [],
|
|
680
771
|
...this.extraArgs,
|
|
681
772
|
'about:blank',
|
|
682
773
|
]
|
|
774
|
+
// Chromium's own sandbox cannot start in some of these environments — running as root, or
|
|
775
|
+
// inside a container without the namespaces it needs — and it exits rather than degrading.
|
|
776
|
+
// Only then is the sandbox dropped, and never silently: a setting that weakens process
|
|
777
|
+
// isolation must be visible in the log, because whoever reads it has to be able to disagree.
|
|
778
|
+
if (launch.headless && needsNoSandbox()) {
|
|
779
|
+
args.splice(args.length - 2, 0, '--no-sandbox')
|
|
780
|
+
process.stderr.write('[dsh-browser] launching with --no-sandbox: '
|
|
781
|
+
+ 'this process runs as root or in a container, where the Chromium sandbox cannot start\n')
|
|
782
|
+
}
|
|
783
|
+
if (launch.headless) {
|
|
784
|
+
process.stderr.write(`[dsh-browser] launching headless (${launch.why}); `
|
|
785
|
+
+ 'the browser has no window but every tool works the same\n')
|
|
786
|
+
}
|
|
683
787
|
// A browser from a previous attempt may still be running with this same
|
|
684
788
|
// --user-data-dir. On Windows the new one would merely hand off to it and exit, and that
|
|
685
789
|
// exit would then be treated as the launch failing.
|