High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: stale-ref recovery covers a drag's source but not its destination, so drag <from> <to> heals one end and 404s on the other (PIN-203)
Luigi Agostiee5e5d2ade59
diff --git a/internal/handlers/action_drag_destination_test.go b/internal/handlers/action_drag_destination_test.goindex 695fbf73..6b2135bf 100644--- a/internal/handlers/action_drag_destination_test.go+++ b/internal/handlers/action_drag_destination_test.go@@ -2,12 +2,17 @@ package handlersimport ("context"+ "encoding/base64""net/http/httptest"+ "os""strings""testing"+ "time"+ "github.com/chromedp/chromedp""github.com/pinchtab/pinchtab/internal/bridge""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/testbrowser")// A drag destination is a selector in the same vocabulary as the source, so it goes through@@ -87,3 +92,185 @@ func TestTheQueryFormOfActionCarriesTheDragDestination(t *testing.T) {t.Errorf("destination = (%v,%v) hasToXY=%v, want (400,320) hasToXY=true", point.ToX, point.ToY, point.HasToXY)}}++const dragRecoveryFixtureHTML = `<body style="margin:0">+<button id="a" style="position:absolute;left:10px;top:10px;width:120px;height:30px">Alpha</button>+<button id="b" style="position:absolute;left:10px;top:60px;width:120px;height:30px">Bravo</button>+<button id="c" style="position:absolute;left:10px;top:110px;width:120px;height:30px">Charlie</button>+<script>+window.__drops = [];+document.addEventListener('mouseup', e => {+ const el = document.elementFromPoint(e.clientX, e.clientY);+ window.__drops.push(el ? el.id : '');+});+</script>+</body>`++func newDragRecoveryFixture(t *testing.T) (context.Context, *bridge.Bridge, *Handlers) {+ 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, 40*time.Second)+ t.Cleanup(func() {+ cancelTimeout()+ cancelBrowser()+ cancelAlloc()+ _ = os.RemoveAll(profile)+ })+ if err := chromedp.Run(ctx, chromedp.Navigate("data:text/html;base64,"+base64.StdEncoding.EncodeToString([]byte(dragRecoveryFixtureHTML)))); err != nil {+ t.Fatal(err)+ }+ cfg := &config.RuntimeConfig{ActionTimeout: 8 * time.Second, DefaultBrowser: config.BrowserChrome, StateDir: t.TempDir()}+ b := bridge.New(context.Background(), ctx, cfg)+ b.RegisterTab("tab-drag", ctx)+ h := New(b, cfg, nil, nil, nil)+ return ctx, b, h+}++// seedDragSnapshot plays the role of an agent's earlier /snapshot: refs with the+// descriptors a real snapshot carries, so recovery has something to match with.+func seedDragSnapshot(t *testing.T, ctx context.Context, b *bridge.Bridge) {+ t.Helper()+ nids := map[string]int64{}+ for id := range map[string]bool{"a": true, "b": true} {+ nid, err := bridge.ResolveCSSToNodeID(ctx, "#"+id)+ if err != nil {+ t.Fatalf("resolve #%s: %v", id, err)+ }+ nids[id] = nid+ }+ b.SetRefCache("tab-drag", &bridge.RefCache{+ Targets: map[string]bridge.RefTarget{+ "e5": {BackendNodeID: nids["a"]},+ "e9": {BackendNodeID: nids["b"]},+ },+ Nodes: []bridge.A11yNode{+ {Ref: "e5", Role: "button", Name: "Alpha", NodeID: nids["a"]},+ {Ref: "e9", Role: "button", Name: "Bravo", NodeID: nids["b"]},+ },+ })+}++func recreateDragElements(t *testing.T, ctx context.Context, ids ...string) {+ t.Helper()+ for _, id := range ids {+ script := `(() => { const el = document.getElementById("` + id + `");+ const clone = el.cloneNode(true); el.remove(); document.body.appendChild(clone); })()`+ if err := chromedp.Run(ctx, chromedp.Evaluate(script, nil)); err != nil {+ t.Fatal(err)+ }+ }+}++func recordedDrops(t *testing.T, ctx context.Context) []string {+ t.Helper()+ var drops []string+ if err := chromedp.Run(ctx, chromedp.Evaluate(`window.__drops`, &drops)); err != nil {+ t.Fatal(err)+ }+ return drops+}++func postDrag(t *testing.T, h *Handlers, body string) *httptest.ResponseRecorder {+ t.Helper()+ w := httptest.NewRecorder()+ h.HandleAction(w, httptest.NewRequest("POST", "/action", strings.NewReader(body)))+ return w+}++// The asymmetry this card removes: the destination now gets the refresh-and-recover+// the source always had. Asserted against the page's own record of where the drop+// landed — a drag that reports dragged:true while dropping elsewhere is the failure+// being ruled out.+func TestADragWithAStaleDestinationRecoversAndLandsOnIt(t *testing.T) {+ ctx, b, h := newDragRecoveryFixture(t)+ seedDragSnapshot(t, ctx, b)+ recreateDragElements(t, ctx, "b")++ w := postDrag(t, h, `{"kind":"drag","selector":"#a","toSelector":"e9","tabId":"tab-drag"}`)++ if w.Code != 200 {+ t.Fatalf("stale destination did not recover: status=%d body=%s", w.Code, w.Body.String())+ }+ drops := recordedDrops(t, ctx)+ if len(drops) == 0 || drops[len(drops)-1] != "b" {+ t.Fatalf("drop landed on %v, want the re-created #b", drops)+ }+}++// Both refs come from one snapshot, so they go stale together far more often than+// separately — the common case.+func TestADragWithBothEndsStaleRecoversBothFromOneSnapshot(t *testing.T) {+ ctx, b, h := newDragRecoveryFixture(t)+ seedDragSnapshot(t, ctx, b)+ recreateDragElements(t, ctx, "a", "b")++ w := postDrag(t, h, `{"kind":"drag","selector":"ref:e5","toSelector":"e9","tabId":"tab-drag"}`)++ if w.Code != 200 {+ t.Fatalf("both-ends-stale drag did not recover: status=%d body=%s", w.Code, w.Body.String())+ }+ drops := recordedDrops(t, ctx)+ if len(drops) == 0 || drops[len(drops)-1] != "b" {+ t.Fatalf("drop landed on %v, want the re-created #b", drops)+ }+}++// Recovery still refuses where it must: a destination that genuinely no longer+// exists after a refresh 404s, naming the destination rather than the source.+func TestADragDestinationGoneAfterRefreshStillRefuses(t *testing.T) {+ ctx, b, h := newDragRecoveryFixture(t)+ seedDragSnapshot(t, ctx, b)+ if err := chromedp.Run(ctx, chromedp.Evaluate(`document.getElementById("b").remove()`, nil)); err != nil {+ t.Fatal(err)+ }++ w := postDrag(t, h, `{"kind":"drag","selector":"#a","toSelector":"e9","tabId":"tab-drag"}`)++ if w.Code == 200 {+ t.Fatalf("a drag to a destination that no longer exists reported success: %s", w.Body.String())+ }+ if !strings.Contains(w.Body.String(), "drag destination e9") {+ t.Errorf("response = %s, want the refusal to name the destination, not the source", w.Body.String())+ }+ if drops := recordedDrops(t, ctx); len(drops) != 0 {+ t.Errorf("a refused drag still dropped on %v", drops)+ }+}++// The browserless half (CI installs no browser): every site that re-resolves after+// the DOM changed re-resolves EVERY selector the request carries, through the one+// owner. A future re-resolution site that forgets the secondary targets is exactly+// how the destination was left behind the first time.+func TestEveryReResolutionSiteRefreshesSecondaryTargets(t *testing.T) {+ execution, err := os.ReadFile("action_execution.go")+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(execution), "func (h *Handlers) refreshActionSecondaryTargets(") {+ t.Fatal("refreshActionSecondaryTargets was renamed or moved; re-point this census at whatever replaced it rather than deleting it")+ }+ body := string(execution)+ start := strings.Index(body, "func (h *Handlers) executeActionResilient(")+ end := strings.Index(body[start:], "\nfunc ")+ resilient := body[start : start+end]+ if got := strings.Count(resilient, "h.refreshActionSecondaryTargets("); got != 3 {+ t.Errorf("executeActionResilient calls refreshActionSecondaryTargets %d times, want 3 — the recovery-first callback, the pointer retry, and the post-failure heal each re-resolve after the snapshot may have changed", got)+ }++ actions, err := os.ReadFile("actions.go")+ if err != nil {+ t.Fatal(err)+ }+ if !strings.Contains(string(actions), "h.refreshActionSecondaryTargets(") {+ t.Error("HandleAction no longer routes a missing drag destination through the shared re-resolution; the unconditional 404 this card removed is the likely regression")+ }+}diff --git a/internal/handlers/action_execution.go b/internal/handlers/action_execution.goindex a9d669ce..8d6a050f 100644--- a/internal/handlers/action_execution.go+++ b/internal/handlers/action_execution.go@@ -9,6 +9,7 @@ import ("github.com/pinchtab/pinchtab/internal/bridge""github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/selector""github.com/pinchtab/semantic""github.com/pinchtab/semantic/recovery")@@ -174,6 +175,9 @@ func (h *Handlers) executeActionResilient(ctx context.Context, req *bridge.Actioctx, resolvedTabID, req.Ref, req.Kind,func(c context.Context, _ string, nodeID int64) (map[string]any, error) {req.NodeID = nodeID+ if secErr := h.refreshActionSecondaryTargets(c, resolvedTabID, req); secErr != nil {+ return nil, secErr+ }res, _, err := h.executeAction(c, *req, cfg)return res, err},@@ -186,18 +190,22 @@ func (h *Handlers) executeActionResilient(ctx context.Context, req *bridge.Actioresult, backend, err := h.executeAction(ctx, *req, cfg)if err != nil && !submitClick && shouldRetryPointerAction(*req, err) {- if req.Ref != "" && shouldRetryStaleRef(err) {+ if (req.Ref != "" || req.ToSelector != "") && shouldRetryStaleRef(err) {recordStaleRefRetry()h.refreshRefCache(ctx, resolvedTabID)- if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil {+ if cache := h.Bridge.GetRefCache(resolvedTabID); cache != nil && req.Ref != "" {if target, ok := cache.Lookup(req.Ref); ok {req.NodeID = target.BackendNodeID}}}h.refreshActionNodeIDFromSelector(ctx, req)- time.Sleep(pointerRetryDelay)- result, backend, err = h.executeAction(ctx, *req, cfg)+ if secErr := h.refreshActionSecondaryTargets(ctx, resolvedTabID, req); secErr != nil {+ err = secErr+ } else {+ time.Sleep(pointerRetryDelay)+ result, backend, err = h.executeAction(ctx, *req, cfg)+ }}var rr *recovery.RecoveryResult@@ -207,6 +215,9 @@ func (h *Handlers) executeActionResilient(ctx context.Context, req *bridge.Actiorecovery.ClassifyFailure(err),func(c context.Context, _ string, nodeID int64) (map[string]any, error) {req.NodeID = nodeID+ if secErr := h.refreshActionSecondaryTargets(c, resolvedTabID, req); secErr != nil {+ return nil, secErr+ }res, _, e := h.executeAction(c, *req, cfg)return res, e},@@ -249,6 +260,71 @@ func shouldRetryPointerAction(req bridge.ActionRequest, err error) bool {return shouldRetryStaleRef(err)}+// refreshActionSecondaryTargets re-resolves every selector-bearing field the request+// carries BEYOND the primary target, against the current snapshot. The primary is+// re-resolved by the caller (recovery's descriptor match, or the ref-cache lookup on+// the pointer retry); everything else must be re-derived rather than carried over,+// because a node id resolved before a refresh can denote a node the DOM change+// destroyed. Today the only secondary field is the drag destination; a future+// selector-bearing field is re-resolved by adding it HERE, not at each site — the+// census in action_drag_destination_test.go pins that every re-resolution site calls+// this.+//+// A destination ref that no longer resolves is re-found by EXACT identity — the+// unique node in the refreshed snapshot with the intent-cached descriptor's role and+// name — never by the fuzzy heal the source gets. Refs are positional per snapshot,+// so an ordinal re-lookup can denote a different element; and a drop is positional+// too, so a fuzzy lookalike match would land the payload somewhere the caller never+// named while reporting dragged:true. Measured: the default 0.4 match confidence+// happily "recovers" a deleted destination onto its sibling. Whether the source's+// fuzzy heal should report what it did is a separate, pending question; nothing here+// classifies recovery outcomes.+func (h *Handlers) refreshActionSecondaryTargets(ctx context.Context, tabID string, req *bridge.ActionRequest) error {+ toSelector := strings.TrimSpace(req.ToSelector)+ if toSelector == "" {+ return nil+ }+ req.ToNodeID = 0+ if resolution, err := h.resolveActionRequestDestination(ctx, tabID, req); err == nil && !resolution.refMissing && req.ToNodeID != 0 {+ return nil+ }+ req.ToNodeID = 0+ if h.Recovery != nil {+ if sel := selector.Parse(toSelector); sel.Kind == selector.KindRef {+ if entry, ok := h.Recovery.IntentCache.Lookup(tabID, sel.Value); ok {+ if nid, found := uniqueNodeForDescriptor(h.Bridge.GetRefCache(tabID), entry.Descriptor); found {+ req.ToNodeID = nid+ return nil+ }+ }+ }+ }+ return fmt.Errorf("drag destination %s not found - take a /snapshot first", toSelector)+}++// uniqueNodeForDescriptor returns the one node matching the descriptor's role and+// name exactly, and refuses when zero or several do — an ambiguous destination is a+// coin flip over where the payload lands.+func uniqueNodeForDescriptor(cache *bridge.RefCache, desc semantic.ElementDescriptor) (int64, bool) {+ if cache == nil || (desc.Role == "" && desc.Name == "") {+ return 0, false+ }+ var match int64+ count := 0+ for i := range cache.Nodes {+ node := cache.Nodes[i]+ if node.Role != desc.Role || node.Name != desc.Name || node.NodeID == 0 {+ continue+ }+ match = node.NodeID+ count+++ }+ if count != 1 {+ return 0, false+ }+ return match, true+}+func (h *Handlers) refreshActionNodeIDFromSelector(ctx context.Context, req *bridge.ActionRequest) {if req == nil || req.NodeID > 0 || strings.TrimSpace(req.Selector) == "" {return@@ -270,8 +346,13 @@ func shouldRetryStaleRef(err error) bool {// Fallback string matching is still needed for stale failures that can bypass// bridge.ExecuteAction classification (for example, static paths or other// non-bridge error surfaces that return raw backend-node messages).+ // "is detached" is CDP's spelling for a node that was removed and re-created —+ // measured from a drag against a re-rendered list ("Node is detached from+ // document (-32000)") — and without it neither the retry nor recovery ever ran+ // for exactly the DOM change stale-ref recovery exists for.e := strings.ToLower(err.Error())- return strings.Contains(e, "could not find node") || strings.Contains(e, "node with given id") || strings.Contains(e, "no node")+ return strings.Contains(e, "could not find node") || strings.Contains(e, "node with given id") ||+ strings.Contains(e, "no node") || strings.Contains(e, "is detached")}func (h *Handlers) refreshRefCache(ctx context.Context, tabID string) {diff --git a/internal/handlers/actions.go b/internal/handlers/actions.goindex 69e8cd39..0bddd7a6 100644--- a/internal/handlers/actions.go+++ b/internal/handlers/actions.go@@ -17,6 +17,7 @@ import ("github.com/pinchtab/pinchtab/internal/config""github.com/pinchtab/pinchtab/internal/httpx""github.com/pinchtab/pinchtab/internal/routes"+ "github.com/pinchtab/pinchtab/internal/selector""github.com/pinchtab/pinchtab/internal/session")@@ -353,9 +354,22 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {h.errorWithCrashContext(w, destinationResolution.httpStatus(), err)return}+ // Both ends of a drag come from one snapshot, so a stale destination gets the+ // same refresh-and-recover the source does instead of an unconditional 404;+ // only a destination that still cannot be found refuses, naming the+ // destination. A destination that resolved is intent-cached like the source,+ // so a later recovery refresh can descriptor-match it rather than trusting a+ // positional ref against a new snapshot.if destinationResolution.refMissing {- httpx.Error(w, 404, fmt.Errorf("drag destination %s not found - take a /snapshot first", req.ToSelector))- return+ h.refreshRefCache(tCtx, resolvedTabID)+ if err := h.refreshActionSecondaryTargets(tCtx, resolvedTabID, &req); err != nil {+ httpx.Error(w, 404, err)+ return+ }+ } else if req.ToSelector != "" && req.ToNodeID != 0 && h.Recovery != nil {+ if toSel := selector.Parse(req.ToSelector); toSel.Kind == selector.KindRef {+ h.cacheActionIntent(resolvedTabID, bridge.ActionRequest{Ref: toSel.Value})+ }}refMissing := selectorResolution.refMissingsubmitClick := bridge.IsSubmitClick(req.Kind, req)