Skip to content

fix(browser): publish the WebView devtools socket and resume the last page - #442

Merged
yuga-hashimoto merged 3 commits into
mainfrom
fix/guest-browser-devtools-socket
Nov 26, 2026
Merged

yuga-hashimoto merged 3 commits into
mainfrom
fix/guest-browser-devtools-socket

Conversation

@yuga-hashimoto

@yuga-hashimoto yuga-hashimoto commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Problem

browser_status/browser_navigate fail with "WebView devtools socket not found" even though browser_show opens the browser. The MCP server discovers the socket by scanning /proc/net/unix, which SELinux denies to untrusted_app (Permission denied inside the guest), so the scan can never succeed.

With a socket found, every CDP tool still timed out: the WebView's GET /json replies with Content-Length and keeps the connection open, while list_targets() read until the text ended with ] — which never happens — so it blocked until the socket timeout.

Reopening the full-screen guest browser from Settings also started from a blank address bar every time, even after the user had been reading a page.

Fix

  • App publishes webview_devtools_remote_<pid> to .and-code/browser-devtools.txt in the active workspace, only when the agent-control opt-in is on.
  • MCP server reads that file first (it is this app's own socket) and falls back to /proc/net/unix, verifying with a connect probe so a closed socket still reports not connected.
  • list_targets() now reads the HTTP body by Content-Length / chunked encoding instead of waiting for the socket to close, so browser_status, browser_info, browser_click, browser_screenshot and the rest stop timing out.
  • Guest browser remembers the last visited http(s) page in its own preferences and resumes it when opened without an explicit URL (agent browser_show / preview handoff still win). Non-web pages are ignored, so a blank/error page never replaces the remembered one. Sign-in cookies stay in the WebView profile and are untouched.
  • Tests: socket name is published only after the opt-in; the last URL is saved, restored by a new instance, and non-web values are never resumed.

On-device check (module level, against a live WebView)

  • list_targets() returned the page target in 0.0 s after the fix (previously it blocked until the 10 s socket timeout).
  • tool_info returned the URL/title, tool_screenshot produced a valid PNG, and tool_click clicked example.com's link and navigated to iana.org.

Test plan

  • CI unit tests
  • Install the PR APK, enable Let the agent control the Guest Browser, browser_show then browser_status reports connected (not a timeout)
  • Open a page in the guest browser, close it, reopen from Settings: the last page comes back

Pre-PR review

判定: APPROVE

ブロッカー

なし

提案(非ブロッキング)

  • PRのスコープ/積み重ね: origin/main(5a90380d)にはまだ fix(browser): publish the WebView devtools socket and resume the last page #442 の devtools 公開(publishDevtoolsSocketName / andcode-browser-mcp.py の _published_devtools_socket)が入っていません。このブランチを main 向けに出すと fix(browser): publish the WebView devtools socket and resume the last page #442 の差分も含まれるため、fix(browser): publish the WebView devtools socket and resume the last page #442 を先にマージするか base を fix(browser): publish the WebView devtools socket and resume the last page #442 ブランチへ切り替える前提かを確認してください。
  • GuestBrowserLastUrl はアプリ全体で1つの URL を保存するため、別ワークスペース/ランタイムで URL なしに開くと、前の http://127.0.0.1:<port> をそのまま復元して別サーバや到達不能ページを表示し得ます(GuestBrowserLastUrl.kt:18-27, GuestBrowserScreen.kt:55)。キーを workspace/runtime 単位にするか、この挙動を KDoc/PR に明記すると安全です。
  • publishDevtoolsSocketName は opt-in OFF やアプリ pid 変更後も .and-code/browser-devtools.txt を残します(BrowserOpenCommand.kt:64-76,118-120)。_published_devtools_socket の listen 確認で誤接続は防げますが、共有ストレージのワークスペースでは他アプリからもソケット名が読めるため、opt-out 時にファイルを削除するのが望ましいです。
  • _read_http_body は今回の実機バグの再発防止に直結する一方、自動テストがありません(andcode-browser-mcp.py:81-123)。Content-Length / chunked / keep-alive(接続が開いたまま)を stdlib の unittest で数ケース追加し、CI の python -m py_compile 対象に app/src/main/assets/scripts/andcode-browser-mcp.py も加えると、同種の退行を CI で止められます。
  • GuestBrowserScreenInstrumentedTest は initialUrl = "" で開くため、端末上に last URL が保存済みだとそれを復元してテストが状態依存になり得ます(CI では instrumentation を実行していないので現状の影響は小)。テスト前に guest_browser prefs を clear するか、ストアを注入できるとより堅牢になります。

チェック済み項目

  • git fetch origin main 後に git diff origin/main...HEAD の全7ファイル・全hunkを確認(スクリプト1、新規Kotlin1、既存Kotlin2、NavGraph1、テスト2)。
  • _read_http_body: ヘッダと本文の分割受信、Content-Length 完読、chunked(サイズ行・データ末尾CRLF・0終端)、keep-alive(接続を閉じない)を机上トレースし、旧 data.endswith(b"]") によるタイムアウト原因が解消されることを確認。
  • _published_devtools_socket: ファイル名・接頭辞検証・abstract socket(\0 + name)の接続確認、および _find_devtools_socket が「公開名を優先し、未接続時のみ /proc/net/unix へフォールバック」することを確認。
  • Kotlin↔Python で .and-code/browser-devtools.txt と webview_devtools_remote_<pid> の形式が一致。publishDevtoolsSocketName が agent-control opt-in 時のみ呼ばれ、GuestWebContent の setWebContentsDebuggingEnabled(agentControlEnabled) と同じフラグであることを確認。
  • GuestBrowserLastUrl: http/https のみ保存・復元、非Web URL は既存値を保持、about:blank 等を弾くこと、InMemorySharedPreferences を使った5テストでカバーされていることを確認。
  • BrowserCommandWatcherTest の新テストは app/build.gradle.kts の unitTests.isReturnDefaultValues = true により Process.myPid() が 0 を返すため JVM テストで通ることを確認(app/build.gradle.kts:204-208)。
  • i18n: 変更された文字列リソースなし(新規キー・全ロケール対応は不要)。Kotlin/XML に新規のユーザー可視ハードコードなし。
  • detekt/spotless 観点: 変更行はすべて140文字未満、git diff --check クリーン、import 順・命名・KDoc は既存規約に沿う。GuestBrowserLastUrl は ViewModel の依存追加ではないため Koin / ViewModelFactory の同期は不要。
  • Gradle は環境制約(JIT 不可)で未実行。コンパイル・単体テストは CI に委ねる静的レビューのみ。

@yuga-hashimoto yuga-hashimoto added the build-apk Build and comment the Android debug APK label Nov 25, 2026
…/proc/net/unix

Android denies /proc/net/unix to untrusted_app, so the in-guest MCP server could never scan for webview_devtools_remote_<pid> and always reported 'not connected', even with agent control enabled. The app now publishes the socket name to .and-code/browser-devtools.txt (only after the agent-control opt-in) and the MCP server falls back to it, verifying with a connect probe.
@yuga-hashimoto
yuga-hashimoto force-pushed the fix/guest-browser-devtools-socket branch from a21c79c to 9dda011 Compare November 25, 2026 19:31
@yuga-hashimoto yuga-hashimoto added build-apk Build and comment the Android debug APK and removed build-apk Build and comment the Android debug APK labels Nov 25, 2026
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Debug APK built successfully

Download APK artifact
View workflow run

Commit: 74f9b6e2eccc | Artifact expires in 14 days.

@yuga-hashimoto yuga-hashimoto changed the title fix(browser): let the guest find the WebView devtools socket without /proc/net/unix fix(browser): publish the WebView devtools socket and resume the last page Nov 25, 2026
@yuga-hashimoto yuga-hashimoto added build-apk Build and comment the Android debug APK and removed build-apk Build and comment the Android debug APK labels Nov 25, 2026
@yuga-hashimoto
yuga-hashimoto merged commit 19dbd74 into main Nov 26, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build-apk Build and comment the Android debug APK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant