From a791651bcb3d26b79eb97dbfbe7e9d7a2e489fec Mon Sep 17 00:00:00 2001 From: Derek Hsu Date: Mon, 23 Nov 2026 11:17:56 +0800 Subject: [PATCH] fix(images): use the built-in browser tools in Hermes named profiles A named profile created with `hermes profile create` has no config.yaml. Hermes reads browser.backend from the profile's own YAML without merging DEFAULT_CONFIG, so the value was empty and, with uvx on PATH, Hermes used the Browser Use CLI, which fetches an unpinned browser-use from PyPI. The base image's profile-policy probe also failed on such a profile: ('browser.backend', '', 'off'). configure-tools-browser.py now takes the Hermes source directory and makes two exact one-line edits: a missing browser.backend reads as "off" in tools/browser_use_cli.py, and the browser.backend default in hermes_cli/config_defaults.py becomes "off". A profile that sets browser.backend keeps its value. Dockerfile.tools pins both source files to their hashes in BASE_IMAGE, rewrites their bytecode, and runs the profile-policy probe on a new named profile. The new phase 3 fixture hermes_profile_browser checks the tools a named profile gets, opens a page, and fails if uvx runs. The standard Hermes image is unchanged. Closes #83 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Derek Hsu --- agents/hermes-01x/Dockerfile.tools | 43 ++++-- agents/hermes-01x/configure-tools-browser.py | 55 +++++++- .../2026-11-17-hermes-tools-image.md | 21 ++- test/hermes-tools-browser-config.test.ts | 129 +++++++++++++++++- .../fixtures/run-phase3.sh | 44 ++++++ 5 files changed, 266 insertions(+), 26 deletions(-) diff --git a/agents/hermes-01x/Dockerfile.tools b/agents/hermes-01x/Dockerfile.tools index 1e90a2f1561..52e2c183c9c 100644 --- a/agents/hermes-01x/Dockerfile.tools +++ b/agents/hermes-01x/Dockerfile.tools @@ -13,9 +13,10 @@ # The image PATH puts the Hermes virtual environment first, so a plain # python3 is Hermes' own interpreter, which these packages must not change. # - No OpenClaw skill or openclaw.json change. The Hermes browser tools use -# the base image's agent-browser: config.yaml and the managed policy set -# browser.backend "off", and /etc/chromium.d/nemoclaw-openshell adds the -# Chromium flags that an OpenShell sandbox requires. +# the base image's agent-browser: config.yaml, the managed policy, and the +# Hermes default for named profiles set browser.backend "off", and +# /etc/chromium.d/nemoclaw-openshell adds the Chromium flags that an +# OpenShell sandbox requires. # # The final image keeps the base image's user, entrypoint, command, working # directory, environment, healthcheck, and controlled package versions. It @@ -203,7 +204,8 @@ RUN --mount=type=bind,from=downloads,target=/downloads \ NPM_CONFIG_OFFLINE=false npm install -g --no-audit --no-fund /downloads/pnpm-12.16.1.tgz # The printf and python -c arguments are literal text, not shell expansions. -# hadolint ignore=DL3003,SC2016 +# The only pipe ends in sha256sum -c, whose status is the pipe status. +# hadolint ignore=DL3003,DL4006,SC2016 RUN set -eu; \ # Offline frontend projects start from a copy of this template. pnpm's # store keeps a SQLite index that cannot be opened from a read-only path, @@ -230,9 +232,12 @@ RUN set -eu; \ # Keep Hermes on its built-in browser tools instead of the Browser Use CLI # that `uvx browser-use` fetches from PyPI (configure-tools-browser.py # explains why). The managed policy gets the same value, or the dashboard - # config seeder refuses the gateway config (#79). Then seal config.yaml - # again exactly as the base image does; only the config.yaml hash line - # changes. + # config seeder refuses the gateway config (#79). Named profiles have no + # config.yaml, so the script also changes the browser.backend default in + # two Hermes source files (#83). The hashes below are those files in + # BASE_IMAGE: check the script's edits against a new base image before + # changing them. Then seal config.yaml again exactly as the base image + # does; only the config.yaml hash line changes. config=/sandbox/.hermes/config.yaml; \ policy=/usr/local/share/nemoclaw/hermes-managed-policy.json; \ seal=/etc/nemoclaw/hermes.config-hash; \ @@ -241,8 +246,16 @@ RUN set -eu; \ test "$(stat -c '%U:%G %a' "$seal")" = "root:root 444"; \ sha256sum -c --quiet "$seal"; \ mcp_line=$(grep '^# nemoclaw-hermes-mcp-state-v1 ' "$seal"); \ - /usr/bin/python3 -I /tmp/nemoclaw-tools/configure-tools-browser.py "$config" "$policy"; \ + printf '%s %s\n' \ + d990eecd35d5b3447ca6f81ba1c6618d0702e2d4281b932974a5e64b29d8436b /opt/hermes/tools/browser_use_cli.py \ + 13e4261f6b47269b92c9cf0f13eac385c8c070a96190d4177639eea239d21def /opt/hermes/hermes_cli/config_defaults.py \ + | sha256sum -c --quiet -; \ + /usr/bin/python3 -I /tmp/nemoclaw-tools/configure-tools-browser.py "$config" "$policy" /opt/hermes; \ test "$(stat -c '%U:%G %a' "$policy")" = "root:root 444"; \ + # Replace the base image's bytecode of the two changed modules, so Hermes + # does not compile them on every start. + /opt/hermes/.venv/bin/python -I -m py_compile \ + /opt/hermes/tools/browser_use_cli.py /opt/hermes/hermes_cli/config_defaults.py; \ ( cd /opt/hermes && HERMES_HOME=/sandbox/.hermes /opt/hermes/.venv/bin/python -I -c \ 'import sys; sys.path.insert(0, "/opt/hermes"); from tools.browser_use_cli import get_browser_backend, is_browser_use_cli_mode; assert get_browser_backend() == "off" and not is_browser_use_cli_mode()' ); \ mcp_digest="$(/opt/hermes/.venv/bin/python -I /usr/local/lib/nemoclaw/build-hermes-mcp-digest.py --guard /usr/local/lib/nemoclaw/hermes-runtime-config-guard.py --config "$config")"; \ @@ -266,6 +279,20 @@ RUN set -eu; \ setpriv --reuid=sandbox --regid=sandbox --init-groups \ /opt/hermes/.venv/bin/python -I /tmp/nemoclaw-tools/image-build-probes.py dashboard-policy "$probe"; \ test ! -e "$probe"; \ + # Repeat the base image's profile-policy probe on a new named profile, + # which has no config.yaml. It compares every managed path, browser.backend + # included. get_browser_backend() must also return "off" for the named + # profile, so Hermes does not use the Browser Use CLI. + profile_root=$(mktemp -d); \ + HOME="$profile_root" HERMES_HOME="$profile_root/.hermes" \ + /usr/local/bin/hermes profile create nemoclaw-policy-probe --no-alias --no-skills; \ + profile_home="$profile_root/.hermes/profiles/nemoclaw-policy-probe"; \ + test ! -e "$profile_home/config.yaml"; \ + HOME="$profile_root" HERMES_HOME="$profile_home" \ + /opt/hermes/.venv/bin/python -I /tmp/nemoclaw-tools/image-build-probes.py profile-policy; \ + ( cd /opt/hermes && HOME="$profile_root" HERMES_HOME="$profile_home" /opt/hermes/.venv/bin/python -I -c \ + 'import sys; sys.path.insert(0, "/opt/hermes"); from tools.browser_use_cli import get_browser_backend, is_browser_use_cli_mode; assert get_browser_backend() == "off" and not is_browser_use_cli_mode()' ); \ + rm -rf "$profile_root"; \ rm -rf /tmp/nemoclaw-tools /root/.npm /root/.cache /root/.local/share/pnpm # Same user name as the base image configuration. diff --git a/agents/hermes-01x/configure-tools-browser.py b/agents/hermes-01x/configure-tools-browser.py index 25a60d0f31d..51aec6e2b1c 100644 --- a/agents/hermes-01x/configure-tools-browser.py +++ b/agents/hermes-01x/configure-tools-browser.py @@ -1,8 +1,8 @@ # SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 -"""Keep the Hermes tools image on Hermes' built-in browser tools (derekhsu/NemoClaw#74, #79). +"""Keep the Hermes tools image on Hermes' built-in browser tools (derekhsu/NemoClaw#74, #79, #83). -Usage: configure-tools-browser.py +Usage: configure-tools-browser.py With browser.backend unset, Hermes replaces its browser tools with the Browser Use CLI whenever uvx is on PATH, and that CLI is fetched from PyPI with @@ -19,9 +19,19 @@ managed path. The dashboard config seeder copies the gateway config only when each managed section equals the policy, and it copies the managed paths into the dashboard profile, so the dashboard also gets browser.backend "off" (#79). + +A named profile created with `hermes profile create` has no config.yaml, and +Hermes reads browser.backend from the profile's own YAML without the defaults, +so the two edits above do not reach it (#83). The script therefore changes two +Hermes source files under : a missing browser.backend reads +as "off" in tools/browser_use_cli.py, and the browser.backend default in +hermes_cli/config_defaults.py becomes "off". A profile that sets +browser.backend itself keeps its value. Each edit replaces one exact line and +fails if the line is missing or already changed. """ import json +import os import sys BROWSER_BLOCK = "browser:" @@ -30,6 +40,23 @@ # Keep the browser paths together in the policy's managed_paths list. BACKEND_PATH_AFTER = "browser.restrict_evaluate" +# Hermes source edits for profiles without browser.backend (#83): path under +# the Hermes source directory, the exact upstream text, and its replacement. +SOURCE_EDITS = ( + ( + "tools/browser_use_cli.py", + ' raw = _read_browser_cfg().get("backend")\n', + " # NemoClaw tools image: a missing browser.backend keeps the built-in browser tools.\n" + ' raw = _read_browser_cfg().get("backend", BACKEND_DISABLED)\n', + ), + ( + "hermes_cli/config_defaults.py", + ' "backend": "",\n "inactivity_timeout": 120,\n', + " # NemoClaw tools image: the built-in browser tools, not the Browser Use CLI.\n" + ' "backend": "off",\n "inactivity_timeout": 120,\n', + ), +) + def configure(text: str) -> str: """Return ``text`` with browser.backend set to "off"; raise ValueError otherwise.""" @@ -66,12 +93,28 @@ def configure_policy(text: str) -> str: return json.dumps(policy, indent=2, ensure_ascii=False) + "\n" +def source_edit(old: str, new: str): + """Return a change that replaces the one ``old`` text with ``new``.""" + + def change(text: str) -> str: + if text.count(new): + raise ValueError("the browser.backend default is already changed") + if text.count(old) != 1: + raise ValueError(f"expected one {old.strip()!r}, found {text.count(old)}") + return text.replace(old, new) + + return change + + def main() -> int: - if len(sys.argv) != 3: + if len(sys.argv) != 4: print(__doc__.strip().splitlines()[2], file=sys.stderr) return 2 + changes = [(sys.argv[1], configure), (sys.argv[2], configure_policy)] + for relative, old, new in SOURCE_EDITS: + changes.append((os.path.join(sys.argv[3], relative), source_edit(old, new))) updates = [] - for path, change in ((sys.argv[1], configure), (sys.argv[2], configure_policy)): + for path, change in changes: with open(path, encoding="utf-8") as handle: text = handle.read() try: @@ -79,8 +122,8 @@ def main() -> int: except ValueError as error: print(f"{path}: {error}", file=sys.stderr) return 1 - # Check both files before writing either. Write in place so the base - # image's owner and mode stay unchanged. + # Check every file before writing any. Write in place so the base image's + # owner and mode stay unchanged. for path, updated in updates: with open(path, "w", encoding="utf-8") as handle: handle.write(updated) diff --git a/docs/modification/2026-11-17-hermes-tools-image.md b/docs/modification/2026-11-17-hermes-tools-image.md index 5988c4cbbd5..ba1084f4155 100644 --- a/docs/modification/2026-11-17-hermes-tools-image.md +++ b/docs/modification/2026-11-17-hermes-tools-image.md @@ -5,8 +5,8 @@ > **日期**: 2026-11-17 > **Fork**: `derekhsu/NemoClaw` -> **Issue**: [#74](https://github.com/derekhsu/NemoClaw/issues/74)、[#79](https://github.com/derekhsu/NemoClaw/issues/79) -> **狀態**: `tools.1` 已發布並在 OpenShell 0.1.7 sandbox 驗收。第二階段(`tools.2` 起)開啟 Hermes browser 工具。`tools.2` 的 Dashboard 拿不到模型設定,`tools.3` 修正(#79)。 +> **Issue**: [#74](https://github.com/derekhsu/NemoClaw/issues/74)、[#79](https://github.com/derekhsu/NemoClaw/issues/79)、[#83](https://github.com/derekhsu/NemoClaw/issues/83) +> **狀態**: `tools.1` 已發布並在 OpenShell 0.1.7 sandbox 驗收。第二階段(`tools.2` 起)開啟 Hermes browser 工具。`tools.2` 的 Dashboard 拿不到模型設定,`tools.3` 修正(#79)。`tools.4`(尚未發布)讓沒有 `config.yaml` 的具名 profile 也使用內建 browser 工具(#83)。 ## 1. 摘要 @@ -75,13 +75,22 @@ Node.js 套件、LibreOffice、officecli、OCR、pandoc、ImageMagick、ffmpeg | 設定 | 位置 | 原因 | |------|------|------| -| `browser.backend: "off"` | `/sandbox/.hermes/config.yaml`;`tools.3` 起也寫進 managed policy `/usr/local/share/nemoclaw/hermes-managed-policy.json`,並列入受管路徑 | 沒有設定時,只要 `PATH` 上有 `uvx`,Hermes 就改用 Browser Use CLI:模型只拿到 `browser_exec`,第一次使用時以 `uvx browser-use` 從 PyPI 下載未鎖版本的套件。`off` 讓 Hermes 使用內建的 browser 工具 | +| `browser.backend: "off"` | `/sandbox/.hermes/config.yaml`;`tools.3` 起也寫進 managed policy `/usr/local/share/nemoclaw/hermes-managed-policy.json`,並列入受管路徑;`tools.4` 起 Hermes 原始碼的預設值也是 `off`(見下方說明) | 沒有設定時,只要 `PATH` 上有 `uvx`,Hermes 就改用 Browser Use CLI:模型只拿到 `browser_exec`,第一次使用時以 `uvx browser-use` 從 PyPI 下載未鎖版本的套件。`off` 讓 Hermes 使用內建的 browser 工具 | | `--no-sandbox --no-zygote --disable-gpu` | `/etc/chromium.d/nemoclaw-openshell` | agent-browser 啟動 `/usr/bin/chromium`,這個 Debian 包裝腳本會讀 `/etc/chromium.d` 的參數再執行 `/usr/lib/chromium/chromium`。OpenShell 的 seccomp 不允許 Chromium 自己的 sandbox 與 zygote 建立 user namespace,沒有這些參數時 Chromium 會立刻結束(`CDP response channel closed`)。Hermes Agent v0.21.3 只在 root 或 Docker 下才會自己加 `--no-sandbox`(`tools/browser_tool_session.py`) | Dashboard 使用自己的 profile `dashboard-home`。sandbox 每次啟動時,`start.sh` 會以 `seed-hermes-dashboard-config.py` 把 gateway `config.yaml` 的模型設定與受管路徑複製到這個 profile;gateway 設定中每個受管區段必須與 managed policy 完全相同,否則整份不複製。`tools.2` 只改了 `config.yaml`,`browser` 區段因此與 policy 不同,Dashboard 的 CHAT 與 Models 頁沒有模型(`No inference provider configured`)。`tools.3` 同時更新 policy,Dashboard 也拿到模型與 `browser.backend: "off"`。 修改 `config.yaml` 後,建置步驟以 base 映像相同的方式重算 `/etc/nemoclaw/hermes.config-hash`(與 `/sandbox/.hermes/.config-hash`),Hermes 啟動時的設定完整性檢查因此仍會通過。MCP 設定沒有改變,MCP digest 維持原值。接著以 sandbox 使用者重跑 base 映像建置時的 dashboard policy 檢查:同步腳本必須接受 gateway 設定,產生的 Dashboard 設定要有 `model:`,每個受管路徑(包含 `browser.backend`)都要與 policy 相同。 +具名 profile(`hermes profile create` 建立)沒有 `config.yaml`。Hermes 的 `get_browser_backend()`(`tools/browser_use_cli.py`)直接讀 profile 的 `config.yaml`,不合併 `DEFAULT_CONFIG`,所以 `tools.2` 與 `tools.3` 的這類 profile 讀到空值,又因 `PATH` 上有 `uvx` 而改用 Browser Use CLI。`tools.4` 起,`configure-tools-browser.py` 多接收 Hermes 原始碼目錄(`/opt/hermes`),並修改兩個檔案: + +- `tools/browser_use_cli.py`:沒有 `browser.backend` 時讀成 `off`。 +- `hermes_cli/config_defaults.py`:`DEFAULT_CONFIG` 的 `browser.backend` 預設值改為 `off`。合併 `DEFAULT_CONFIG` 的程式(例如 `load_config_readonly()` 與 `profile-policy` 檢查)因此也讀到 `off`。web backend 的預設值不變。 + +每項修改只取代一行完全相符的內容;該行不存在或已被修改時,腳本失敗。腳本先檢查所有檔案,再寫入任何檔案。profile 自己設定了 `browser.backend` 時(包含明確設成 `""` 或 `browser-use`),保留該值。 + +建置步驟在執行腳本前以 `sha256sum -c` 比對這兩個檔案與 `Dockerfile.tools` 記錄的 hash(`BASE_IMAGE` 中這兩個檔案的 hash),不符時建置失敗;執行後以 `py_compile` 重新產生這兩個 module 的 bytecode。Dashboard policy 檢查之後,建置步驟建立具名 profile `nemoclaw-policy-probe`,確認它沒有 `config.yaml`,接著執行 base 映像的 `profile-policy` 檢查(每個受管路徑,包含 `browser.backend`),並確認 `get_browser_backend()` 為 `off`、不是 Browser Use CLI 模式。 + Hermes Agent v0.21.3 以本機 Chromium 執行時不做私有位址檢查(`tools/browser_tool.py` 的 `_url_policy_error`),所以 OpenShell policy DNS 回傳的 `198.18.0.0/15` 合成 IP 不會被擋;雲端 metadata 位址(例如 `169.254.169.254`)仍一律拒絕。這點與 OpenClaw 不同,不需要設定 SSRF 例外。 要讓瀏覽器開網站,sandbox 的 network policy(由 ClawShell 管理)需要一條規則: @@ -103,7 +112,7 @@ Hermes Agent v0.21.3 以本機 Chromium 執行時不做私有位址檢查(`tools |------|------| | `check-image.sh ... hermes` | 執行設定、`hermes --version` 與 base 相同;新增的 setuid、setgid 或 file capability 檔案必須列在 `tools/openclaw-tools-image/privileged-files-added.txt` | | `check-tools-start.sh` | base 與 tools 映像以相同限制啟動(無網路、`--cap-drop ALL`、`no-new-privileges`),容器狀態、啟動輸出、`/tmp/nemoclaw-start.log` 與 process 清單必須相同 | -| `run-fixtures.sh ... hermes` | 以 sandbox 使用者、唯讀 root、無 capability、無網路執行共用 fixture;OpenClaw 專屬的 `himalaya_skill_override` 與 `browser_config` 記為 `SKIP`,`himalaya_journey` 略過 skill 檢查。`hermes_browser_backend` 確認 `browser.backend` 為 `off`、設定 hash 相符、Hermes 提供內建 browser 工具;`hermes_dashboard_seed` 以映像內的 policy 與 `config.yaml` 執行 Dashboard 設定同步,產生的設定要有模型,`browser.backend` 為 `off`;`agent_browser` 不帶任何 agent-browser 設定開頁,並確認 Chromium 帶著 `/etc/chromium.d` 的參數執行 | +| `run-fixtures.sh ... hermes` | 以 sandbox 使用者、唯讀 root、無 capability、無網路執行共用 fixture;OpenClaw 專屬的 `himalaya_skill_override` 與 `browser_config` 記為 `SKIP`,`himalaya_journey` 略過 skill 檢查。`hermes_browser_backend` 確認 `browser.backend` 為 `off`、設定 hash 相符、Hermes 提供內建 browser 工具;`hermes_dashboard_seed` 以映像內的 policy 與 `config.yaml` 執行 Dashboard 設定同步,產生的設定要有模型,`browser.backend` 為 `off`;`agent_browser` 不帶任何 agent-browser 設定開頁,並確認 Chromium 帶著 `/etc/chromium.d` 的參數執行;`hermes_profile_browser` 建立沒有 `config.yaml` 的具名 profile,並把記錄呼叫的 `uvx` 放在 `PATH` 最前面,確認 `browser.backend` 為 `off`、不是 Browser Use CLI 模式、browser 工具組有 `browser_navigate` 與 `browser_snapshot` 而沒有 `browser_exec`,再以 `browser_navigate` 開啟本機檔案頁面並檢查 snapshot;`uvx` 被呼叫時失敗 | | Trivy | tools layer 不得新增可修正的 CRITICAL 或 HIGH 弱點 | `check-tools-start.sh` 用一般 Docker 執行,只涵蓋映像自己的啟動設定。 @@ -126,7 +135,7 @@ gateway 與 API runtime 由 OpenShell 啟動,要在 OpenShell 0.1.7 sandbox 中 - `BASE_IMAGE` 必須是 `docker.io/derekhsu/openshell-hermes` 的 `openshell01x-hermes-*` tag,並以 digest 固定。 - tag 已存在時拒絕,不覆寫。 -更新 base:Hermes 映像發布新版後,把新的 tag 與 index digest 寫進 `Dockerfile.tools` 的 `BASE_IMAGE`,合併後再發布 tools 映像,`n` 從 1 開始。 +更新 base:Hermes 映像發布新版後,把新的 tag 與 index digest 寫進 `Dockerfile.tools` 的 `BASE_IMAGE`,合併後再發布 tools 映像,`n` 從 1 開始。更新 `BASE_IMAGE` 時,維護者必須先確認 `configure-tools-browser.py` 的兩項修改仍適用於新 base 的 `tools/browser_use_cli.py` 與 `hermes_cli/config_defaults.py`,再更新 `Dockerfile.tools` 中這兩個檔案的 hash。hash 不符時建置失敗。 部署端(例如 ClawShell)要以 digest 選用 tools 映像。 @@ -134,6 +143,6 @@ gateway 與 API runtime 由 OpenShell 啟動,要在 OpenShell 0.1.7 sandbox 中 - **新增的特權檔案**:LibreOffice 的相依套件帶入 `dbus-daemon-launch-helper`(setuid)與 `gst-ptp-helper`(file capability)。process 設定 `no_new_privs` 時兩者都不會取得額外權限;fixture 與啟動比對都在這個條件下執行,OpenShell sandbox 內的 process 也實測為 `NoNewPrivs: 1`(見下方實機驗收)。其他新增的特權檔案會讓 `check-image.sh` 失敗。 - **API profile 不受影響**:API profile 的政策拒絕 terminal 與 browser 工具,Landlock 也只開放部分路徑,新工具只在預設 profile 使用。API profile 的 `config.yaml` 由 managed policy 的受管區段產生,`tools.3` 起 `browser` 區段也帶有 `backend: "off"`,但 browser 工具仍被拒絕。 -- **沒有 `config.yaml` 的具名 profile**:base 映像建置時依 managed policy 修改 Hermes 對沒有 `config.yaml` 的具名 profile 所用的預設值(`patch-profile-policy-defaults.py`),但沒有修改 `browser.backend`。Hermes 直接讀 profile 的 `config.yaml` 取得這個值,所以這類 profile 的 `browser.backend` 仍是未設定,與 `tools.2` 相同;base 映像建置時的 `profile-policy` 檢查若在 tools 映像重跑,會因此不通過。預設 profile、`dashboard-home` 與 API profile 都有 `config.yaml`,不受影響。 +- **沒有 `config.yaml` 的具名 profile**:`tools.2` 與 `tools.3` 中,這類 profile 的 `browser.backend` 是未設定,Hermes 改用 Browser Use CLI;base 映像的 `profile-policy` 檢查若在這兩版重跑,會因此不通過。`tools.4` 起,Hermes 原始碼的預設值是 `off`(見 3.3 節),建置步驟在 tools 映像重跑 `profile-policy` 檢查,fixture 的 `hermes_profile_browser` 也確認不會呼叫 `uvx`。profile 自己設定的 `browser.backend` 仍然優先,設成 `""` 或 `browser-use` 時 Hermes 仍會改用 Browser Use CLI。base 映像(`openshell-hermes`)沒有這項修改。 - **base 的安全檢查**:Hermes base 沒有 tmux,受控的 expat 版本(`libexpat1=2.8.3-1`)也與 OpenClaw base(2.9.0)不同,所以 `base_security_checks` 只對 OpenClaw 檢查這兩項;Hermes 的 expat 版本由 `controlled_packages` 依 base 的 `security-packages.txt` 檢查。 - **實機驗收**(`tools.1`,OpenShell 0.1.7、Podman、arm64,經 ClawShell 建立):sandbox 到 `running`,API 對話與 `upload_file` 上傳下載正常;`nemoclaw-start`、Hermes gateway、API runtime 與 MCP supervisor 的 `NoNewPrivs` 皆為 1;4 個 LibreOffice 同時轉檔時 sandbox 內 process 峰值 31(#32 的上限 512)。 diff --git a/test/hermes-tools-browser-config.test.ts b/test/hermes-tools-browser-config.test.ts index d1e6fd80613..1ea80580999 100644 --- a/test/hermes-tools-browser-config.test.ts +++ b/test/hermes-tools-browser-config.test.ts @@ -47,14 +47,50 @@ const BASE_POLICY_OBJECT = { }; const BASE_POLICY = `${JSON.stringify(BASE_POLICY_OBJECT, null, 2)}\n`; -function configure(text: string, policyText = BASE_POLICY) { +// The Hermes Agent v0.21.3 source lines that read and default browser.backend. +const BASE_BROWSER_USE_CLI = [ + "def get_browser_backend() -> str:", + ' """Configured browser backend key."""', + ' raw = _read_browser_cfg().get("backend")', + ' return (BACKEND_DISABLED if raw is False else "") if isinstance(raw, bool) else str(raw or "").strip().lower()', + "", + "def is_legacy_browser_use_cloud_config(browser_cfg: dict) -> bool:", + ' if not isinstance(browser_cfg, dict) or browser_cfg.get("backend"):', + " return False", + "", +].join("\n"); +const BASE_CONFIG_DEFAULTS = [ + ' "web": {', + ' "backend": "", # shared fallback', + " },", + ' "browser": {', + ' "backend": "",', + ' "inactivity_timeout": 120,', + " },", + "", +].join("\n"); +const SOURCES = { + "tools/browser_use_cli.py": BASE_BROWSER_USE_CLI, + "hermes_cli/config_defaults.py": BASE_CONFIG_DEFAULTS, +}; + +function configure( + text: string, + policyText = BASE_POLICY, + sources: Record = SOURCES, +) { const dir = fs.mkdtempSync(path.join(os.tmpdir(), "nemoclaw-hermes-tools-browser-")); try { const file = path.join(dir, "config.yaml"); const policyFile = path.join(dir, "managed-policy.json"); + const hermes = path.join(dir, "hermes"); fs.writeFileSync(file, text); fs.writeFileSync(policyFile, policyText); - const result = spawnSync("python3", [SCRIPT, file, policyFile], { + for (const [relative, source] of Object.entries(sources)) { + fs.mkdirSync(path.dirname(path.join(hermes, relative)), { recursive: true }); + fs.writeFileSync(path.join(hermes, relative), source); + } + const result = spawnSync("python3", [SCRIPT, file, policyFile, hermes], { encoding: "utf8", timeout: 10_000, }); @@ -62,13 +98,19 @@ function configure(text: string, policyText = BASE_POLICY) { result, written: fs.readFileSync(file, "utf8"), policy: fs.readFileSync(policyFile, "utf8"), + sources: Object.fromEntries( + Object.keys(sources).map((relative) => [ + relative, + fs.readFileSync(path.join(hermes, relative), "utf8"), + ]), + ), }; } finally { fs.rmSync(dir, { recursive: true, force: true }); } } -describe("Hermes tools image browser backend (#74, #79)", () => { +describe("Hermes tools image browser backend (#74, #79, #83)", () => { it("sets browser.backend to off and changes no other line", () => { const { result, written } = configure(BASE_CONFIG); @@ -133,13 +175,88 @@ describe("Hermes tools image browser backend (#74, #79)", () => { expect(policy).toBe(policyText); }); - it("requires both the config and the policy path", () => { - const result = spawnSync("python3", [SCRIPT, "config.yaml"], { + it("defaults browser.backend to off for a profile without config.yaml (#83)", () => { + const { result, sources } = configure(BASE_CONFIG); + + expect(result.status, result.stderr).toBe(0); + expect(sources["tools/browser_use_cli.py"]).toBe( + BASE_BROWSER_USE_CLI.replace( + ' raw = _read_browser_cfg().get("backend")\n', + " # NemoClaw tools image: a missing browser.backend keeps the built-in browser tools.\n" + + ' raw = _read_browser_cfg().get("backend", BACKEND_DISABLED)\n', + ), + ); + expect(sources["hermes_cli/config_defaults.py"]).toBe( + BASE_CONFIG_DEFAULTS.replace( + ' "backend": "",\n "inactivity_timeout"', + " # NemoClaw tools image: the built-in browser tools, not the Browser Use CLI.\n" + + ' "backend": "off",\n "inactivity_timeout"', + ), + ); + // The web backend default keeps its value. + expect(sources["hermes_cli/config_defaults.py"]).toContain( + '"web": {\n "backend": "", # shared fallback', + ); + }); + + const patchedCli = BASE_BROWSER_USE_CLI.replace( + '.get("backend")\n', + '.get("backend", BACKEND_DISABLED)\n', + ); + + it.each([ + [ + "a changed browser_use_cli.py", + { + ...SOURCES, + "tools/browser_use_cli.py": BASE_BROWSER_USE_CLI.replace("raw = ", "value = "), + }, + "found 0", + ], + [ + "an already edited browser_use_cli.py", + { + ...SOURCES, + "tools/browser_use_cli.py": patchedCli.replace( + " raw =", + " # NemoClaw tools image: a missing browser.backend keeps the built-in browser tools.\n raw =", + ), + }, + "already changed", + ], + [ + "a browser default other than an empty string", + { + ...SOURCES, + "hermes_cli/config_defaults.py": BASE_CONFIG_DEFAULTS.replace( + '"backend": "",\n "inactivity', + '"backend": "browser-use",\n "inactivity', + ), + }, + "found 0", + ], + ])("refuses %s and leaves every file unchanged", (_name, sources, message) => { + const { + result, + written, + policy, + sources: after, + } = configure(BASE_CONFIG, BASE_POLICY, sources); + + expect(result.status).toBe(1); + expect(result.stderr).toContain(message); + expect(written).toBe(BASE_CONFIG); + expect(policy).toBe(BASE_POLICY); + expect(after).toEqual(sources); + }); + + it("requires the config, policy, and Hermes source paths", () => { + const result = spawnSync("python3", [SCRIPT, "config.yaml", "policy.json"], { encoding: "utf8", timeout: 10_000, }); expect(result.status).toBe(2); - expect(result.stderr).toContain(""); + expect(result.stderr).toContain(""); }); }); diff --git a/tools/openclaw-tools-image/fixtures/run-phase3.sh b/tools/openclaw-tools-image/fixtures/run-phase3.sh index 8f23d29a11f..431d817f596 100755 --- a/tools/openclaw-tools-image/fixtures/run-phase3.sh +++ b/tools/openclaw-tools-image/fixtures/run-phase3.sh @@ -149,6 +149,49 @@ agent_browser() { [ "$rc" -eq 0 ] && [ "$procs" -gt 0 ] && grep -q 'agent-browser fixture' <<<"$out" } +# A named profile has no config.yaml. It must still get the built-in browser +# tools, not the Browser Use CLI, and opening a page must not run uvx (#83). A +# uvx that records its calls comes first on PATH. +hermes_profile_browser() { + local home=$WORK/profile-root profile + profile=$home/.hermes/profiles/fixture + mkdir -p "$home/bin" + printf '#!/bin/sh\necho "$*" >>%s/uvx-calls\nexit 1\n' "$home" >"$home/bin/uvx" + chmod 0755 "$home/bin/uvx" + HOME=$home HERMES_HOME=$home/.hermes hermes profile create fixture --no-alias --no-skills >/dev/null + test ! -e "$profile/config.yaml" + printf 'profile

named profile fixture

\n' >profile-browser.html + ( + cd /opt/hermes && HOME=$home HERMES_HOME=$profile PATH=$home/bin:$PATH \ + AGENT_BROWSER_SOCKET_DIR=$WORK/profile-agent-browser \ + python3 -I -W ignore - "file://$WORK/profile-browser.html" <<'PY' +import json +import sys + +sys.path.insert(0, "/opt/hermes") +import model_tools +from tools import browser_tool +from tools.browser_tool_lifecycle import cleanup_browser +from tools.browser_use_cli import get_browser_backend, is_browser_use_cli_mode + +assert get_browser_backend() == "off", repr(get_browser_backend()) +assert not is_browser_use_cli_mode(), "Browser Use CLI mode" +names = {d["function"]["name"] for d in model_tools.get_tool_definitions(enabled_toolsets=["browser"], quiet_mode=True)} +assert {"browser_navigate", "browser_snapshot"} <= names and "browser_exec" not in names, sorted(names) +try: + result = json.loads(browser_tool.browser_navigate(sys.argv[1], task_id="profile-fixture")) +finally: + cleanup_browser("profile-fixture") +assert result.get("success") and "named profile fixture" in result.get("snapshot", ""), result +print("named profile: built-in browser tools") +PY + ) || return 1 + if [ -e "$home/uvx-calls" ]; then + cat "$home/uvx-calls" + return 1 + fi +} + check chromium_cli chromium_cli check playwright_default playwright_default check playwright_shared playwright_shared @@ -156,6 +199,7 @@ check frontend_offline frontend_offline check p5js p5js if [ "$AGENT" = hermes ]; then check agent_browser agent_browser + check hermes_profile_browser hermes_profile_browser fi finish