From de7953e3b8af5800e2a505d7fc5c76d23c66d560 Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 2 Nov 2026 02:15:47 +0800 Subject: [PATCH] browser: browser_evaluate calls an async()=> function and awaits it MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The check that tells a function from an expression wanted a space after async, so `async()=>{…}` was evaluated as an expression: the function itself came back, serialized as {}, and its body never ran. And a parameter list could hold parentheses, so `(async()=>{…})()` counted as a function and its promise was called ("is not a function"). async may now touch its parameters, and a parameter list holds no parentheses. The extension goes to 0.1.2. Found driving X through magpie-browser from Claude Code. Run on mote (Chrome for Testing 1234): - MAGPIE_BROWSER_CHROME=… go test ./internal/gateway -run 'TestBrowser.*InARealBrowser' -count=1: ok (46.5s). Five new evaluate cases: async()=>, async () =>, async function, an async IIFE and Promise.resolve, each answering "awaited". - Broken by hand: with origin/main's background.js the test is red at the first case (async()=>… doesn't say "awaited"). - go test ./internal/browserext: ok. gofmt -l: clean. Not run: the full suite; not tried in Edge or Firefox. --- internal/browserext/ext/background.js | 4 +++- internal/browserext/ext/manifest.json | 2 +- internal/gateway/browser_e2e_test.go | 11 +++++++++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/internal/browserext/ext/background.js b/internal/browserext/ext/background.js index a37923ad4..f45b5ef35 100644 --- a/internal/browserext/ext/background.js +++ b/internal/browserext/ext/background.js @@ -669,7 +669,9 @@ const TOOLS = { const t = await ready(session, args); if (t.dialog) return fail(notices(t).join("\n")); const src = String(args.script).trim(); - const fn = /^(async\s+)?(function\b|\([^)]*\)\s*=>|[A-Za-z_$][\w$]*\s*=>)/.test(src); + // async may touch its parameters (async()=>…), and a parameter list + // holds no parentheses, so (async () => …)() stays an expression. + const fn = /^(async\b\s*)?(function\b|\([^()]*\)\s*=>|[A-Za-z_$][\w$]*\s*=>)/.test(src); let r; if (args.ref) { const el = await find(t, args.ref); diff --git a/internal/browserext/ext/manifest.json b/internal/browserext/ext/manifest.json index c9222a5e3..9f2741f14 100644 --- a/internal/browserext/ext/manifest.json +++ b/internal/browserext/ext/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "Magpie Browser", - "version": "0.1.1", + "version": "0.1.2", "description": "Lets the agents you run through Magpie use this browser: your tabs, signed in to the sites you use.", "key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwjVk++2N/+l0PX53QSgXvBmDiyIOFXPDRTSo6l7HfM633QbDes2FXjS09l6hY33gsVX9eXE9O+94jQD2SVKSBn8V/EsL3f7mbNfkBqsZ4hYF3ogYxHUjJD6wjKHMFZQi54ArMZBI/psMUBbIjVTeuU9EGwnxoYU5umC8UHvEwkb4ZO0lprP3PGbHDjxrNi5fdFI8azVjyX0zlYlSw2wv0bCAZdkWuRoZWWgKdQwpdtRHrDQMnulWXT5DmNLYDpHgCreDrVaVLXzIAblSLOkZFo/T5xBiicmEbkWPbb8d3iE0OQStm1HYLdp4LLeKnnU6B7Kj/woOFt1uGIwoMqPzswIDAQAB", "minimum_chrome_version": "116", diff --git a/internal/gateway/browser_e2e_test.go b/internal/gateway/browser_e2e_test.go index c09892c70..9e3447298 100644 --- a/internal/gateway/browser_e2e_test.go +++ b/internal/gateway/browser_e2e_test.go @@ -103,6 +103,17 @@ func TestBrowserToolsInARealBrowser(t *testing.T) { ok("browser_scroll", map[string]any{"direction": "down", "amount": 600}, "Scrolled down 600px") ok("browser_evaluate", map[string]any{"script": "window.scrollY"}, "600") + // An async function is called and awaited however it is spaced, and + // an expression that calls one is awaited, not called again. + for _, script := range []string{ + "async()=>{await new Promise(r=>setTimeout(r,50));return 'awaited'}", + "async () => { await null; return 'awaited' }", + "async function(){ await null; return 'awaited' }", + "(async()=>{await new Promise(r=>setTimeout(r,50));return 'awaited'})()", + "Promise.resolve('awaited')", + } { + ok("browser_evaluate", map[string]any{"script": script}, `"awaited"`) + } ok("browser_text", nil, "Magpie fixture", "Bottom of the page") shot := call("browser_screenshot", nil)