From 5cfa7ccc8f4082db111bc0186c5fa3d44b767ad3 Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 00:01:32 +0530 Subject: refactor(tools): declare concurrency in the tool definition serialized: true was applied in registry.ts, away from the tools it describes. Now a required concurrency field on each definition, with a test pinning the serialized set so the classes cannot drift. The lock check defaults to taking the mutex for any class that is not explicitly "parallel", so a definition that bypasses defineBrowserTool at runtime fails safe rather than racing on shared CDP state. The test script gains --conditions=import: pi-coding-agent publishes no "require" condition, so importing any tool definition from a CJS-loaded test could not resolve it. Co-Authored-By: Claude Opus 5 (1M context) --- src/domains/click.ts | 1 + src/domains/console.ts | 1 + src/domains/dialog.ts | 1 + src/domains/drag.ts | 1 + src/domains/files.ts | 3 ++ src/domains/form.ts | 3 ++ src/domains/forms.ts | 4 +++ src/domains/history.ts | 3 ++ src/domains/js.ts | 2 ++ src/domains/keyboard.ts | 3 ++ src/domains/navigate.ts | 2 ++ src/domains/network.ts | 2 ++ src/domains/page.ts | 4 +++ src/domains/readpage/read-page.ts | 1 + src/domains/screenshot.ts | 1 + src/domains/scroll.ts | 1 + src/domains/search/web-search.ts | 1 + src/domains/setup.ts | 1 + src/domains/snapshot.ts | 1 + src/domains/tabs.ts | 5 ++++ src/domains/viewport.ts | 1 + src/registry.ts | 58 ++++++++++++++++++--------------------- src/util/tool.ts | 11 ++------ 23 files changed, 70 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/domains/click.ts b/src/domains/click.ts index db340a8..81bd7ca 100644 --- a/src/domains/click.ts +++ b/src/domains/click.ts @@ -67,6 +67,7 @@ export const clickTool = defineBrowserTool({ "If a click doesn't register, set BH_DEBUG_CLICKS=1 to get annotated screenshots (debug only). For React/Vue components ignoring clicks, try browser_dispatch_key.", ], parameters: ClickArgs, + concurrency: "serialized", renderCall: (a) => new Text(`πŸ–±οΈ Click ${a.ref ? `[${a.ref}]` : `(${a.x}, ${a.y})`}`, 0, 0), async handler(args, { client }) { const button = args.button ?? "left"; diff --git a/src/domains/console.ts b/src/domains/console.ts index f9814d6..9696cd9 100644 --- a/src/domains/console.ts +++ b/src/domains/console.ts @@ -119,6 +119,7 @@ export const consoleTool = defineBrowserTool({ "Don't use as a default observation tool β€” prefer browser_snapshot for page structure.", ], parameters: ConsoleArgs, + concurrency: "parallel", async handler(args, { client }): Promise> { const session = client.session(); diff --git a/src/domains/dialog.ts b/src/domains/dialog.ts index c30e70c..7038c64 100644 --- a/src/domains/dialog.ts +++ b/src/domains/dialog.ts @@ -18,6 +18,7 @@ export const handleDialogTool = defineBrowserTool({ "For prompt() dialogs, supply promptText with the value to submit.", ], parameters: HandleDialogArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const r = await client.session().call("Page.handleJavaScriptDialog", { accept: args.accept, diff --git a/src/domains/drag.ts b/src/domains/drag.ts index 9075fd0..9ad2ada 100644 --- a/src/domains/drag.ts +++ b/src/domains/drag.ts @@ -24,6 +24,7 @@ export const dragAndDropTool = defineBrowserTool({ "dataTransfer is an optional MIMEβ†’data map (e.g., { 'text/plain': 'hello' }).", ], parameters: DragArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const data = args.dataTransfer ? { diff --git a/src/domains/files.ts b/src/domains/files.ts index cf9393e..4e0195d 100644 --- a/src/domains/files.ts +++ b/src/domains/files.ts @@ -109,6 +109,7 @@ export const uploadFileTool = defineBrowserTool({ "File path must be absolute and readable.", ], parameters: UploadArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const readable = await verifyReadable(args.filePath); if (!readable.success) return readable; @@ -160,6 +161,7 @@ export const downloadTool = defineBrowserTool({ "Affects all subsequent downloads on this browser.", ], parameters: DownloadArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { try { await mkdir(args.downloadPath, { recursive: true }); @@ -205,6 +207,7 @@ export const printToPdfTool = defineBrowserTool({ promptSnippet: "Print the current page to PDF", promptGuidelines: ["Default output path is in tmpdir; pass outputPath to control."], parameters: PrintPdfArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const r = await client.session().call("Page.printToPDF", { printBackground: true, diff --git a/src/domains/form.ts b/src/domains/form.ts index 95516c5..6e3285a 100644 --- a/src/domains/form.ts +++ b/src/domains/form.ts @@ -136,6 +136,7 @@ export const fillTool = defineBrowserTool({ "The result reports the field's value after writing and an appended page-changes diff β€” confirm both match what you intended before moving on.", ], parameters: FillArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const r = await runOnElement(client, { ref: args.ref, selector: args.selector, fnBody: FILL_FN, arg: args.value }); if (!r.success) return r; @@ -198,6 +199,7 @@ export const focusTool = defineBrowserTool({ "For simply setting a value, prefer browser_fill (it doesn't need a separate focus step).", ], parameters: FocusArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const r = await runOnElement(client, { ref: args.ref, selector: args.selector, fnBody: FOCUS_FN, arg: "" }); if (!r.success) return r; @@ -277,6 +279,7 @@ export const selectOptionTool = defineBrowserTool({ "If no option matches, the error details list the available options so you can retry.", ], parameters: SelectOptionArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { if (args.value === undefined && args.label === undefined && args.index === undefined) { return err({ kind: "invalid_state", message: "Provide one of value, label, or index." }); diff --git a/src/domains/forms.ts b/src/domains/forms.ts index e38af9a..ed64a74 100644 --- a/src/domains/forms.ts +++ b/src/domains/forms.ts @@ -231,6 +231,7 @@ export const fillTool = defineBrowserTool({ "If a ref no longer resolves, re-run browser_snapshot to get fresh refs.", ], parameters: FillArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const r = await resolveAndCall(client, args.ref, FILL_FN, [args.value]); if (!r.success) return r; @@ -278,6 +279,7 @@ export const fillFormTool = defineBrowserTool({ "Re-run browser_snapshot afterwards to verify the form state if needed.", ], parameters: FillFormArgs, + concurrency: "serialized", async handler(args, { client }): Promise> { const results: Array>> = []; let okCount = 0; @@ -323,6 +325,7 @@ export const selectOptionTool = defineBrowserTool({ "Use this for native