High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: ghost-chrome capped the accessible name on one of five sources while chrome and cloak emit it in full, so a matching key meant different things per provider (PIN-155)
Luigi Agosti0ecc1eb0bd07
diff --git a/internal/browsers/ghostchrome/staticfetch/semantics.go b/internal/browsers/ghostchrome/staticfetch/semantics.goindex ae39e53c..e8b2206e 100644--- a/internal/browsers/ghostchrome/staticfetch/semantics.go+++ b/internal/browsers/ghostchrome/staticfetch/semantics.go@@ -3,8 +3,6 @@ package staticfetchimport ("strings"- "github.com/pinchtab/pinchtab/internal/sanitize"-"github.com/gost-dom/browser/dom""github.com/gost-dom/browser/html")@@ -101,13 +99,23 @@ func getRole(el dom.Element) string {return "generic"}-// accessibleNameMaxBytes is the TOTAL byte budget for an accessible name,-// marker included — sanitize.TruncateUTF8Bytes spends len(TruncationSuffix) of it-// rather than adding to it. The name comes from arbitrary page text, so the cut-// must land on a rune boundary: an invalid-UTF-8 name is the identity an agent-// matches on to decide what to click, and it ends up in a JSON response.-const accessibleNameMaxBytes = 100-+// getAccessibleName returns every source UNCUT, and that is deliberate: the name is+// the identity an agent matches on to decide what to click, so it has to mean the+// same thing on every provider. The CDP path (internal/bridge/observe) takes the+// accessibility node's name straight through, so chrome and cloak emit names in+// full; a per-name byte cap here made the same button match on one provider and not+// another, silently.+//+// A ceiling still exists, and it is the right one: internal/handlers/snapshot.go+// applies TruncateToTokens to the flattened nodes of whatever provider produced+// them, gated on the caller's maxTokens. That is provider-neutral and bounds the+// payload the caller actually asked to bound. It is now the ONLY ceiling on name+// length here — accepted, since it is the position chrome and cloak have always been+// in.+//+// The cap it replaces covered one of five sources (TextContent), so the four+// attribute paths were already unbounded; removing it takes away no property the+// code had.func getAccessibleName(el dom.Element) string {if label, ok := el.GetAttribute("aria-label"); ok {return label@@ -127,7 +135,7 @@ func getAccessibleName(el dom.Element) string {}}if isInteractive(el) {- return sanitize.TruncateUTF8Bytes(strings.TrimSpace(el.TextContent()), accessibleNameMaxBytes)+ return strings.TrimSpace(el.TextContent())}return ""}diff --git a/internal/browsers/ghostchrome/staticfetch/semantics_name_parity_test.go b/internal/browsers/ghostchrome/staticfetch/semantics_name_parity_test.gonew file mode 100644index 00000000..052b400b--- /dev/null+++ b/internal/browsers/ghostchrome/staticfetch/semantics_name_parity_test.go@@ -0,0 +1,107 @@+// An external test package on purpose: this compares the static-fetch name against+// the CDP one, and internal/testbrowser reaches staticfetch through+// internal/browsers/ghostchrome, so an in-package test importing it would cycle.+package staticfetch_test++import (+ "context"+ "encoding/base64"+ "net/http"+ "net/http/httptest"+ "os"+ "strings"+ "testing"+ "time"++ "github.com/chromedp/chromedp"+ "github.com/pinchtab/pinchtab/internal/bridge/observe"+ "github.com/pinchtab/pinchtab/internal/browsers/ghostchrome/staticfetch"+ "github.com/pinchtab/pinchtab/internal/testbrowser"+)++// This is the criterion the product decision exists for: the accessible name is a+// matching key, so the SAME element on the SAME page must give an agent the same+// string whichever provider produced the snapshot. A test comparing ghost-chrome+// against itself cannot see the divergence being closed — the CDP side has to be+// the oracle.+func TestAccessibleNameMatchesTheCDPPathForTheSameElement(t *testing.T) {+ const marker = "PARITY"+ label := marker + strings.Repeat("x", 400) + "-end"+ body := `<!doctype html><html><head><title>parity</title></head><body>` ++ `<button id="target">` + label + `</button></body></html>`++ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {+ w.Header().Set("Content-Type", "text/html; charset=utf-8")+ _, _ = w.Write([]byte(body))+ }))+ defer ts.Close()++ lite := staticfetch.NewBrowser()+ defer func() { _ = lite.Close() }()+ if _, err := lite.Navigate(context.Background(), ts.URL); err != nil {+ t.Fatalf("Navigate: %v", err)+ }+ result, err := lite.Snapshot(context.Background(), "", "all")+ if err != nil {+ t.Fatalf("Snapshot: %v", err)+ }+ staticName := ""+ for _, node := range result.Nodes {+ if strings.HasPrefix(node.Name, marker) {+ staticName = node.Name+ break+ }+ }+ if staticName == "" {+ t.Fatal("the static snapshot carries no node named from the fixture button")+ }++ cdpName := cdpAccessibleName(t, body, marker)++ if staticName != cdpName {+ t.Errorf("provider divergence on a matching key:\n static-fetch (%d bytes) = %q\n CDP (%d bytes) = %q\nAn agent matching on `name` must get one string per element regardless of provider",+ len(staticName), staticName, len(cdpName), cdpName)+ }+}++func cdpAccessibleName(t *testing.T, body, marker string) string {+ t.Helper()+ chromePath := testbrowser.Path(t)+ profile := testbrowser.ProfileDir(t)+ alloc, cancelAlloc := chromedp.NewExecAllocator(context.Background(), append(+ chromedp.DefaultExecAllocatorOptions[:],+ chromedp.ExecPath(chromePath),+ chromedp.UserDataDir(profile),+ chromedp.Flag("headless", true),+ chromedp.Flag("no-sandbox", true),+ )...)+ ctx, cancelBrowser := chromedp.NewContext(alloc)+ ctx, cancelTimeout := context.WithTimeout(ctx, 30*time.Second)+ t.Cleanup(func() {+ cancelTimeout()+ cancelBrowser()+ cancelAlloc()+ _ = os.RemoveAll(profile)+ })++ dataURL := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte(body))+ if err := chromedp.Run(ctx,+ chromedp.Navigate(dataURL),+ chromedp.WaitVisible("#target", chromedp.ByID),+ ); err != nil {+ t.Fatal(err)+ }++ raw, err := observe.FetchAXTree(ctx)+ if err != nil {+ t.Fatalf("FetchAXTree: %v", err)+ }+ flat, _ := observe.BuildSnapshot(raw, "all", -1)+ for _, node := range flat {+ if strings.HasPrefix(node.Name, marker) {+ return node.Name+ }+ }+ t.Fatal("the CDP snapshot carries no node named from the fixture button; the oracle read nothing")+ return ""+}diff --git a/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go b/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.goindex 04c2fc74..75125fd9 100644--- a/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go+++ b/internal/browsers/ghostchrome/staticfetch/semantics_name_truncate_test.go@@ -6,25 +6,24 @@ import ("net/http/httptest""strings""testing"- "unicode/utf8"-- "github.com/pinchtab/pinchtab/internal/sanitize")-// The accessible name is the identity an agent matches on to decide what to-// click, so a cut that lands inside a rune does not merely look wrong — it puts-// invalid UTF-8 into a JSON response and renders as U+FFFD.-//-// The label is 99 ASCII bytes followed by a two-byte rune, so the byte cap falls-// between that rune's two bytes. A hand-rolled text[:100] keeps the lead byte-// alone; a rune-aware cut cannot.-func TestAccessibleNameIsTruncatedOnARuneBoundary(t *testing.T) {- label := strings.Repeat("A", accessibleNameMaxBytes-1) + "é"- html := `<!doctype html><html><body><button>` + label + `</button></body></html>`-- ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+// TestAccessibleNameIsTruncatedOnARuneBoundary was deleted with the byte cap it+// asserted: there is no cut left to land on a rune boundary. Its underlying concern+// — an accessible name reaching a JSON response as invalid UTF-8 — survives the cap+// and is NOT what that test covered. Measured through this harness: a name whose+// page bytes are invalid UTF-8 arrives uncut on both the aria-label path (never+// capped) and the TextContent path when it is under the old budget (so truncation+// never fired), and json.Marshal silently substitutes U+FFFD rather than erroring.+// The cap only ever guaranteed WHERE a cut landed, never that the input was valid,+// so that defect predates and outlives it and is filed as its own card rather than+// smuggled in here.++func namedSnapshotNode(t *testing.T, body, marker string) string {+ t.Helper()+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {w.Header().Set("Content-Type", "text/html; charset=utf-8")- _, _ = w.Write([]byte(html))+ _, _ = w.Write([]byte(body))}))defer ts.Close()@@ -34,27 +33,66 @@ func TestAccessibleNameIsTruncatedOnARuneBoundary(t *testing.T) {t.Fatalf("Navigate: %v", err)}- var name stringfor _, node := range snapshotNodes(t, lite, "all") {- if strings.HasPrefix(node.Name, "AAAA") {- name = node.Name- break+ if strings.HasPrefix(node.Name, marker) {+ return node.Name}}- if name == "" {- t.Fatal("no snapshot node carries the long button label — the fixture no longer reaches getAccessibleName")- }+ t.Fatalf("no snapshot node carries a name starting %q — the fixture no longer reaches getAccessibleName", marker)+ return ""+}- if !utf8.ValidString(name) {- t.Errorf("accessible name is not valid UTF-8: %q", name)- }- if strings.ContainsRune(name, utf8.RuneError) {- t.Errorf("accessible name carries the replacement character: %q", name)- }- if !strings.HasSuffix(name, sanitize.TruncationSuffix) {- t.Errorf("accessible name %q does not carry the truncation marker %q", name, sanitize.TruncationSuffix)+// The accessible name is a MATCHING key, so it must arrive exactly as the page wrote+// it: an agent comparing it against a CDP-provider snapshot of the same button has to+// see the same string. TextContent is the source that was capped, so it is the row+// that fails against the pre-decision code; the attribute row is the negative control+// that was already uncut and must stay that way.+func TestEveryAccessibleNameSourceArrivesUncut(t *testing.T) {+ long := strings.Repeat("A", 400) + "-end"++ for _, tc := range []struct {+ name string+ body string+ }{+ {+ name: "text content, the source that carried the cap",+ body: `<!doctype html><html><body><button>` + long + `</button></body></html>`,+ },+ {+ name: "aria-label, uncut before and after",+ body: `<!doctype html><html><body><button aria-label="` + long + `">x</button></body></html>`,+ },+ {+ name: "title",+ body: `<!doctype html><html><body><button title="` + long + `">x</button></body></html>`,+ },+ {+ name: "alt on an image",+ body: `<!doctype html><html><body><img src="x.png" alt="` + long + `"></body></html>`,+ },+ {+ name: "placeholder on an input",+ body: `<!doctype html><html><body><input placeholder="` + long + `"></body></html>`,+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ got := namedSnapshotNode(t, tc.body, "AAAA")+ if got != long {+ t.Errorf("name is %d bytes and %s the page's string; a snapshot name is a matching key and must arrive uncut, since the CDP providers emit it in full",+ len(got), map[bool]string{true: "truncates", false: "does not equal"}[len(got) < len(long)])+ }+ if strings.Contains(got, "...") {+ t.Errorf("name carries a truncation marker: %q", got)+ }+ })}- if len(name) > accessibleNameMaxBytes {- t.Errorf("accessible name is %d bytes, over the %d-byte total budget: %q", len(name), accessibleNameMaxBytes, name)+}++// A short name must still arrive whole and untouched — the negative control that+// stops "uncut" being satisfied by a name the code mangles some other way.+func TestAShortAccessibleNameIsUnchanged(t *testing.T) {+ got := namedSnapshotNode(t, `<!doctype html><html><body><button>AAAA short label</button></body></html>`, "AAAA")+ if got != "AAAA short label" {+ t.Errorf("name = %q, want the page's own short label unchanged", got)}}