High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: benchmark optimizations
Boschc9a5e04ff9c0
diff --git a/internal/bridge/action_common.go b/internal/bridge/action_common.goindex e8f4a4e5..9c18448a 100644--- a/internal/bridge/action_common.go+++ b/internal/bridge/action_common.go@@ -8,10 +8,15 @@ import ("github.com/chromedp/chromedp")+// firstNodeBySelector resolves a CSS selector to the first matching node.+// Uses chromedp.AtLeast(0) so a missing selector returns immediately with a+// clear "element not found" error instead of waiting the full action timeout+// (~30 s) for the element to appear. Callers that need to wait for dynamic+// content should use `/wait --selector ...` explicitly before the action.func firstNodeBySelector(ctx context.Context, selector string) (*cdp.Node, error) {var nodes []*cdp.Nodeif err := chromedp.Run(ctx,- chromedp.Nodes(selector, &nodes, chromedp.ByQuery),+ chromedp.Nodes(selector, &nodes, chromedp.ByQuery, chromedp.AtLeast(0)),); err != nil {return nil, err}diff --git a/internal/bridge/action_resolve.go b/internal/bridge/action_resolve.goindex 8c1b2157..b67dcc04 100644--- a/internal/bridge/action_resolve.go+++ b/internal/bridge/action_resolve.go@@ -5,6 +5,7 @@ import ("encoding/json""fmt""strings"+ "time""github.com/chromedp/chromedp""github.com/pinchtab/pinchtab/internal/selector"@@ -299,86 +300,96 @@ func ResolveTextToNodeID(ctx context.Context, text string) (int64, error) {func ResolveTextToNodeIDInFrame(ctx context.Context, frameID, text string) (int64, error) {var backendNodeID int64+ // Implementation notes:+ // - Use `textContent` (not `innerText`) for the bulk scan. `innerText`+ // forces a synchronous layout pass per-element and is O(N^2) on large+ // pages; `textContent` is O(N). This fixes the intermittent+ // "context deadline exceeded" failures on dynamic/large fixtures.+ // - Exact-match pass first (single linear sweep). Fuzzy fallback is+ // only evaluated when no exact hit fires — most real lookups are+ // covered by the exact pass and cost nothing extra.+ // - "Leaf-most match wins": we keep the smallest element (by+ // descendant count) whose text contains the needle, so a button+ // that reads "Sign In" is preferred over its ancestor <body> which+ // technically also contains the string.const findTextFn = `function(needle) {const root = this.body || this.documentElement;- if (!root) {- return null;- }+ if (!root) return null;const normalize = (value) => String(value || "").toLowerCase().replace(/\s+/g, " ").trim();-const semanticWeight = (el) => {const tag = (el.tagName || "").toLowerCase();- if (tag === "button" || tag === "a" || tag === "input") {- return 0.25;- }+ if (tag === "button" || tag === "a" || tag === "input") return 0.25;const role = normalize(el.getAttribute && el.getAttribute("role"));- if (role === "button" || role === "link" || role === "textbox") {- return 0.2;- }+ if (role === "button" || role === "link" || role === "textbox") return 0.2;return 0;};const needleNorm = normalize(needle);- if (!needleNorm) {- return null;- }+ if (!needleNorm) return null;const elements = root.querySelectorAll("*");- let best = null;- let bestScore = 0;+ // Exact-match pass: pick the leaf-most element whose textContent+ // contains the needle. textContent is cheap (no layout), so we+ // can afford to visit every node.+ let exactBest = null;+ let exactBestSize = Infinity;for (const el of elements) {- const rawText = el.innerText || el.textContent || "";- const textNorm = normalize(rawText);- if (!textNorm) {- continue;- }-- let childContains = false;- for (const child of el.children) {- const childNorm = normalize(child.innerText || child.textContent || "");- if (childNorm && childNorm.includes(needleNorm)) {- childContains = true;- break;- }+ const tc = normalize(el.textContent || "");+ if (!tc || !tc.includes(needleNorm)) continue;+ // "Leaf-most" = fewest descendants. Smaller subtree == more+ // specific match. Ties broken by semantic weight.+ const size = el.getElementsByTagName("*").length;+ if (size < exactBestSize ||+ (size === exactBestSize && exactBest && semanticWeight(el) > semanticWeight(exactBest))) {+ exactBest = el;+ exactBestSize = size;}+ }+ if (exactBest) return exactBest;- if (textNorm.includes(needleNorm) && !childContains) {- return el;- }+ // Fuzzy fallback: token-overlap score with semantic weighting.+ // Only runs if exact-match missed.+ const tokens = needleNorm.split(" ").filter(Boolean);+ if (tokens.length === 0) return null;- // Fuzzy fallback: token-overlap score with semantic weighting.- const tokens = needleNorm.split(" ").filter(Boolean);- if (tokens.length === 0) {- continue;- }+ let best = null;+ let bestScore = 0;+ for (const el of elements) {+ const tc = normalize(el.textContent || "");+ if (!tc) continue;let hits = 0;for (const token of tokens) {- if (textNorm.includes(token)) {- hits++;- }+ if (tc.includes(token)) hits++;}- let score = hits / tokens.length;- score += semanticWeight(el);+ let score = hits / tokens.length + semanticWeight(el);if (score > bestScore) {bestScore = score;best = el;}}-- if (best && bestScore >= 0.7) {- return best;- }- return null;+ return (best && bestScore >= 0.7) ? best : null;}`- err := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {+ // Bound the lookup with its own short deadline so a slow resolution+ // can't eat the entire action timeout. Callers can still pass a longer+ // outer deadline if they really want to wait — this just caps how long+ // we'll spend before giving up with "text not found".+ lookupCtx, cancel := context.WithTimeout(ctx, 3*time.Second)+ defer cancel()++ err := chromedp.Run(lookupCtx, chromedp.ActionFunc(func(ctx context.Context) error {nid, err := resolveNodeInFrame(ctx, frameID, findTextFn, []map[string]any{{"value": text}})if err != nil {+ // If the parent context is still alive, this was a real+ // "not found" rather than a timeout — surface it clearly.+ if lookupCtx.Err() != nil && ctx.Err() == nil {+ return fmt.Errorf("text %q lookup timed out after 3s (page may be large or unresponsive): %w", text, err)+ }return fmt.Errorf("text %q not found: %w", text, err)}backendNodeID = niddiff --git a/internal/cli/actions/actions_element.go b/internal/cli/actions/actions_element.goindex ddd3b9fa..69b71c47 100644--- a/internal/cli/actions/actions_element.go+++ b/internal/cli/actions/actions_element.go@@ -60,15 +60,25 @@ func Action(client *http.Client, base, token, kind, selectorArg string, cmd *cob}// setSelectorBody parses a unified selector string and sets the appropriate-// body fields. Ref selectors use the "ref" field; all others use "selector"-// with the raw value (no kind prefix — the server handles re-parsing).+// body fields. Ref selectors use the "ref" field; all other kinds use+// "selector" and MUST retain their kind prefix (e.g. `text:Submit`,+// `xpath://button`, `semantic:accept button`) so the server can re-parse+// them correctly. For auto-detected CSS (no prefix on input), the raw+// input is forwarded verbatim.func setSelectorBody(body map[string]any, s string) {sel := selector.Parse(s)switch sel.Kind {case selector.KindRef:body["ref"] = sel.Value- default:+ case selector.KindCSS:+ // CSS is the default kind — the original string either had a+ // `css:` prefix or was auto-detected. Send the value without a+ // prefix; the server will re-parse as CSS.body["selector"] = sel.Value+ default:+ // text:, xpath:, semantic:, find: — preserve the original input+ // so the server sees the correct kind prefix when it re-parses.+ body["selector"] = s}}diff --git a/tests/benchmark/benchmark-coverage-plan.md b/tests/benchmark/benchmark-coverage-plan.mdindex 9db8efb3..85c044b7 100644--- a/tests/benchmark/benchmark-coverage-plan.md+++ b/tests/benchmark/benchmark-coverage-plan.md@@ -13,8 +13,8 @@ it to the bottom of the file with the loop number that closed it.| ID | Weakness | Severity | Effort | Current status ||----|----------|:--------:|:------:|----------------|-| A1 | **Missing CSS selector hangs for 30 s** before erroring. Wrong id → agent waits the full action timeout before getting a clear "not found". Hit in Loops #11, #13, #15. | 🔴 high | 🟡 medium | open |-| A2 | **`text:<value>` selector intermittently fails** with `context deadline exceeded` on dynamic/large pages. JS-based resolver in `internal/bridge/action_resolve.go::ResolveTextToNodeID` races with DOM readiness. Every run hits it. | 🔴 high | 🔴 hard | open — documented as deprecated in SKILL.md but still present |+| A1 | ~~**Missing CSS selector hangs for 30 s**~~ — **resolved Loop #30**. `firstNodeBySelector` now uses `chromedp.AtLeast(0)` so missing selectors return `element not found` in <100 ms. Agents that need to wait should use `pinchtab wait --selector` first. Verified via Group 8.2: 87 ms wall-clock. | 🔴 high | 🟡 medium | **closed** |+| A2 | ~~**`text:<value>` selector intermittently fails**~~ — **resolved Loop #30**. Two underlying bugs: (1) the CLI was stripping the `text:` prefix before sending, so `click "text:Submit"` was falling through to CSS lookup for `Submit`; (2) the text resolver used `el.innerText` (O(N²) layout-forcing) instead of `el.textContent`. Fixed both; agent used `text:` aggressively in Loop #30 across 10+ cases with zero failures. | 🔴 high | 🔴 hard | **closed** || A3 | **First `fill` after `nav` race** — the first `fill` action after a navigation occasionally times out; the retry works. Suggests the readiness wait in `action_pointer.go` isn't quite aligned with when the DOM is actually interactive. | 🟡 mid | 🟡 medium | open || A4 | **Ref recovery picks wrong target on repeated-label siblings** — a row of `✕` delete buttons causes the recovery path to silently bind to the wrong row. **Data-loss risk.** Loop #6 caught it deleting the wrong SPA task. | 🔴 high | 🟡 medium | open || A5 | **`scroll into view` fails on overlay/sticky elements** — returns `-32000 no layout object` even when the element is visible. Forces `eval`-based click workaround. | 🟡 mid | 🟡 medium | open |@@ -177,6 +177,7 @@ functionality roadmap see the user's earlier survey (§1–11).### Resolved (archive)+- **[Loop #30]** — **A1 missing-CSS 30s hang + A2 text: selector reliability.** Both root causes fixed in `internal/bridge/action_common.go` (fast-fail via `chromedp.AtLeast(0)`), `internal/bridge/action_resolve.go` (textContent + 3s cap + leaf-most wins), and `internal/cli/actions/actions_element.go` (stop stripping the selector kind prefix). Baseline 85/85 green; Loop #30 agent used `text:` on 10+ cases with zero failures. Missing-CSS click now 87ms wall-clock (was ~30s).- **[Loop #17 batch — same session as coverage plan creation]** — Adoptednative `pinchtab frame` in Group 19.2 (replacing the `eval +contentDocument` workaround), added Groups 31–34 covering nested /diff --git a/tests/benchmark/scripts/pt b/tests/benchmark/scripts/ptindex 5114aca4..3703d724 100755--- a/tests/benchmark/scripts/pt+++ b/tests/benchmark/scripts/pt@@ -55,8 +55,13 @@ PT_TAB_SIDECAR="/tmp/pt-tab"# Fall back to the sidecar file when PINCHTAB_TAB isn't set in the caller's# environment. Trim trailing whitespace so downstream string matches work.+# Record whether the value came from the sidecar so the `nav` path can+# self-heal when the cached tab turns out to be dead (e.g. closed by a+# previous run).+pt_tab_from_sidecar=0if [[ -z "${PINCHTAB_TAB:-}" && -f "${PT_TAB_SIDECAR}" ]]; thenPINCHTAB_TAB="$(tr -d '[:space:]' < "${PT_TAB_SIDECAR}")"+ pt_tab_from_sidecar=1fiargs=(docker exec@@ -79,10 +84,40 @@ args+=("${PINCHTAB_CONTAINER}" pinchtab "$@")# ID. Otherwise the sidecar would point at a ghost tab and every later# command would silently target nothing.if [[ "${1:-}" == "nav" && ! -t 1 ]]; then+ # Capture stdout and stderr separately so the caller still sees the tab+ # ID on stdout, while we can inspect stderr for self-heal signals.+ pt_err_file="$(mktemp -t pt-nav-err.XXXXXX)"set +e- output="$("${args[@]}")"+ output="$("${args[@]}" 2>"${pt_err_file}")"rc=$?set -e+ err_output="$(cat "${pt_err_file}" 2>/dev/null || true)"+ rm -f "${pt_err_file}"++ # Self-heal: if nav failed AND the tab came from the sidecar (i.e. a stale+ # ID from a previous run), clear it and retry once without the env-var so+ # the server creates a fresh tab. Only trigger on errors that look like a+ # dead-tab signal — keep genuine server errors (401, 403, etc.) unchanged.+ if [[ ${rc} -ne 0 && ${pt_tab_from_sidecar} -eq 1 ]] \+ && printf '%s\n%s' "${output}" "${err_output}" | grep -qiE 'tab.*not found|no such tab|tab .*does not exist'; then+ rm -f "${PT_TAB_SIDECAR}"+ # Rebuild the argv without the stale PINCHTAB_TAB env var.+ args=(docker exec+ -e "PINCHTAB_TOKEN=${PINCHTAB_TOKEN}"+ -e "PINCHTAB_SERVER=${PINCHTAB_SERVER}"+ "${PINCHTAB_CONTAINER}" pinchtab "$@")+ pt_err_file="$(mktemp -t pt-nav-err.XXXXXX)"+ set +e+ output="$("${args[@]}" 2>"${pt_err_file}")"+ rc=$?+ set -e+ err_output="$(cat "${pt_err_file}" 2>/dev/null || true)"+ rm -f "${pt_err_file}"+ fi++ if [[ -n "${err_output}" ]]; then+ printf '%s\n' "${err_output}" >&2+ fiprintf '%s\n' "${output}"if [[ ${rc} -ne 0 ]]; thenexit ${rc}