From 632600ec0a0a68d9327e19bd419a1ae72047955f Mon Sep 17 00:00:00 2001 From: amankumarsingh77 Date: Tue, 28 Jul 2026 21:55:55 +0530 Subject: refactor: one door from a tool to the browser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Four routes coexisted: cdpCall, a raw client.session().call, a session held in a local and called directly, and client.evaluateJs — and five files mixed two or three of them. The CdpError-to-ToolErr mapping was hand-copied at nine sites, plus a private copy in isolated-tab.ts. domains/cdp-call.ts now owns all of it: cdpErrToToolErr, cdpCall, cdpCallOnTarget, cdpCallBrowser, evalJs. Every other file under src/domains goes through them, which also removes a pile of `if (!r.success) return err({ kind: "cdp_error", ... })` boilerplate in favour of returning the failed Result as-is. Two scanner rules keep it that way, scoped to src/domains/ so the layer that legitimately owns the session — cdp/ and client.ts — is unaffected. Scoping is new to the rule model; without it the rules fired on the transport itself. One deliberate behaviour change: a CDP timeout now surfaces as kind "timeout" rather than "cdp_error". isolated-tab.ts already did this and the other 13 sites flattened it; the shared mapper uses the honest label rather than preserving the mislabel. Co-Authored-By: Claude Opus 5 (1M context) --- test/util/check-boundaries-test.ts | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'test') diff --git a/test/util/check-boundaries-test.ts b/test/util/check-boundaries-test.ts index b42baa0..f9eb7eb 100644 --- a/test/util/check-boundaries-test.ts +++ b/test/util/check-boundaries-test.ts @@ -97,6 +97,40 @@ describe("boundary check", () => { assert.equal(checkSource([{ path: "a.ts", text }], noExemptions).length, 0); }); + test("flags a tool reaching the session directly", () => { + const forms = [ + "const r = await client.session().call(\"DOM.resolveNode\", { backendNodeId: id });", + "const r = await client.session().callBrowser(\"Target.getTargets\");", + "const r = await client.session().callOnTarget(\"Page.enable\", {}, sid);", + "const r = await session.call(\"Accessibility.getFullAXTree\", {});", + "const r = await session.callBrowser(\"Browser.getWindowForTarget\", { targetId });", + ]; + for (const text of forms) { + const v = checkSource([{ path: "src/domains/thing.ts", text }], noExemptions); + assert.equal(v.some((x) => x.rule === "raw-cdp-call"), true, text); + } + }); + + test("flags a tool calling evaluateJs directly", () => { + const v = checkSource( + [{ path: "src/domains/thing.ts", text: "const r = await client.evaluateJs(expr);" }], + noExemptions, + ); + assert.equal(v.some((x) => x.rule === "raw-evaluate"), true); + }); + + test("does not flag the session layer, which owns the session", () => { + const text = 'const r = await session.callBrowser("Target.getTargets");\nawait client.evaluateJs(expr);'; + for (const path of ["src/client.ts", "src/cdp/session.ts", "src/cdp/target-factory.ts"]) { + assert.equal(checkSource([{ path, text }], noExemptions).length, 0, path); + } + }); + + test("does not mistake Function.prototype.call in an evaluated snippet for a CDP call", () => { + const text = "const expr = `${prelude} ${opts.fnBody} }).call(el, __arg); })()`;"; + assert.equal(checkSource([{ path: "src/domains/form.ts", text }], noExemptions).length, 0); + }); + test("reports the 1-indexed line number and the trimmed text", () => { const v = checkSource([{ path: "a.ts", text: "const a = 1;\n const b = c as any;" }], noExemptions); assert.equal(v[0]?.line, 2); -- cgit v1.2.3