High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: a ref with a zero backend node id resolves as success in the frame scope (PIN-087)
Luigi Agostib5b244df2d36
diff --git a/internal/bridge/action_resolve.go b/internal/bridge/action_resolve.goindex ffc7fb45..9b4c77e4 100644--- a/internal/bridge/action_resolve.go+++ b/internal/bridge/action_resolve.go@@ -657,7 +657,7 @@ func (s nodeScope) resolveAt(ctx context.Context, sel selector.Selector, index ireturn resolveSelectorAtWithinNode(ctx, s.backendNodeID, sel, index, fromEnd)}-// resolveRef is where the two scopes genuinely differ. A cached ref still exists+// resolveRef differs from the frame scope only in containment. A cached ref still exists// while the dialog owns the interaction surface, so it must be proven to lie// inside the scope subtree — otherwise a dialog-scoped action silently reaches// an element behind the dialog. The outside-scope sentinel is distinct from@@ -667,7 +667,7 @@ func (s nodeScope) resolveRef(ctx context.Context, sel selector.Selector, refCacreturn 0, fmt.Errorf("ref %s not in snapshot cache: %w", sel.Value, ErrSelectorNoMatch)}target, ok := refCache.Lookup(sel.Value)- if !ok || target.BackendNodeID == 0 {+ if !ok {return 0, fmt.Errorf("ref %s not in snapshot cache: %w", sel.Value, ErrSelectorNoMatch)}inside, err := BackendNodeWithinScope(ctx, s.backendNodeID, target.BackendNodeID)diff --git a/internal/bridge/action_resolve_scope_test.go b/internal/bridge/action_resolve_scope_test.goindex cb90eda0..cbe558d6 100644--- a/internal/bridge/action_resolve_scope_test.go+++ b/internal/bridge/action_resolve_scope_test.go@@ -127,10 +127,10 @@ func TestResolveWrapperRejectionsUnchanged(t *testing.T) {// back whatever the cache holds; nodeScope proves containment first. This pins// the half that needs no browser — the containment half is pinned against a real// dialog in the modal test.-func TestScopeRefBehaviourDiffersByDesign(t *testing.T) {- cache := &RefCache{Targets: map[string]RefTarget{"e0": {BackendNodeID: 99}}}+func TestBothScopesRefuseAZeroBackendNodeIDRef(t *testing.T) {sel := selector.Parse("ref:e0")+ cache := &RefCache{Targets: map[string]RefTarget{"e0": {BackendNodeID: 99}}}got, err := (frameScope{}).resolveRef(context.Background(), sel, cache)if err != nil || got != 99 {t.Fatalf("frame scope ref = (%d, %v), want (99, nil)", got, err)@@ -143,15 +143,62 @@ func TestScopeRefBehaviourDiffersByDesign(t *testing.T) {t.Errorf("node scope with no cache = %v, want ErrSelectorNoMatch", err)}- // Divergence between the former copies, preserved rather than reconciled:- // a cached ref carrying a zero backend node id is a successful resolution to- // 0 in the frame scope and a not-in-cache error in the node scope.- zero := &RefCache{Targets: map[string]RefTarget{"e0": {BackendNodeID: 0}}}- if got, err := (frameScope{}).resolveRef(context.Background(), sel, zero); err != nil || got != 0 {- t.Errorf("frame scope zero-id ref = (%d, %v), want (0, nil) as before", got, err)+ // The two scopes used to disagree here: a zero backend node id was a+ // successful resolution to node 0 in the frame scope and a not-in-cache error+ // in the node scope. Zero is not a node, so both now refuse it, and the rule+ // lives in RefCache.Lookup rather than in either resolver.+ //+ // Both maps are driven, because a cache built from only one of them cannot+ // tell which branch of Lookup enforces the rule. The ghost-chrome static+ // snapshot route writes a zero into BOTH, so fixing only the Refs fallback+ // would leave the real-world case resolving to node 0 through Targets.+ for _, tc := range []struct {+ name string+ cache *RefCache+ }{+ {name: "zero in Targets", cache: &RefCache{Targets: map[string]RefTarget{"e0": {BackendNodeID: 0}}}},+ {name: "zero in Refs", cache: &RefCache{Refs: map[string]int64{"e0": 0}}},+ {+ name: "zero in both, as the ghost-chrome static route writes it",+ cache: &RefCache{+ Refs: map[string]int64{"e0": 0},+ Targets: map[string]RefTarget{"e0": {}},+ },+ },+ } {+ t.Run(tc.name, func(t *testing.T) {+ if _, ok := tc.cache.Lookup("e0"); ok {+ t.Errorf("Lookup reported success for a zero backend node id")+ }+ if got, err := (frameScope{}).resolveRef(context.Background(), sel, tc.cache); !errors.Is(err, ErrSelectorNoMatch) {+ t.Errorf("frame scope zero-id ref = (%d, %v), want ErrSelectorNoMatch", got, err)+ }+ if _, err := (nodeScope{backendNodeID: 1}).resolveRef(context.Background(), sel, tc.cache); !errors.Is(err, ErrSelectorNoMatch) {+ t.Errorf("node scope zero-id ref = %v, want ErrSelectorNoMatch", err)+ }+ })+ }+}++// A ref that later gains a real node id must resolve again: the ghost-chrome+// escalation remap is what turns a static zero into a live Chrome node, so making+// zero unresolvable must not make the ref permanently dead.+func TestARefRemappedFromZeroToALiveNodeResolvesAgain(t *testing.T) {+ sel := selector.Parse("ref:e0")+ cache := &RefCache{+ Refs: map[string]int64{"e0": 0},+ Targets: map[string]RefTarget{"e0": {}},}- if _, err := (nodeScope{backendNodeID: 1}).resolveRef(context.Background(), sel, zero); !errors.Is(err, ErrSelectorNoMatch) {- t.Errorf("node scope zero-id ref = %v, want ErrSelectorNoMatch as before", err)+ if _, err := (frameScope{}).resolveRef(context.Background(), sel, cache); !errors.Is(err, ErrSelectorNoMatch) {+ t.Fatalf("precondition: the static ref must start unresolvable, got %v", err)+ }++ cache.Refs["e0"] = 77+ cache.Targets["e0"] = RefTarget{BackendNodeID: 77}++ got, err := (frameScope{}).resolveRef(context.Background(), sel, cache)+ if err != nil || got != 77 {+ t.Errorf("after escalation, frame scope ref = (%d, %v), want (77, nil)", got, err)}}diff --git a/internal/bridge/bridge_types.go b/internal/bridge/bridge_types.goindex 035f6ef2..4639f7bd 100644--- a/internal/bridge/bridge_types.go+++ b/internal/bridge/bridge_types.go@@ -45,17 +45,29 @@ type RefCache struct {DomEpoch string}+// Lookup answers whether a ref names a node that can be acted on. A zero backend+// node id is not such a node, so an entry carrying one is reported as absent —+// this is the single owner of that invariant, which is why neither resolveRef+// restates it.+//+// Both maps need the check, and for different reasons. RefTargetsFromNodes+// already skips zero-id nodes, but the ghost-chrome static-snapshot route writes+// Targets directly and deliberately stores a zero alongside a zero in Refs: those+// refs exist so an agent can read a statically fetched page, before anything has+// escalated to Chrome and given them real node ids. Zero there means "a known ref+// with no live node yet", which is a third state, not a defect — the snapshot+// keeps returning those refs, and only resolution refuses them.func (c *RefCache) Lookup(ref string) (RefTarget, bool) {if c == nil {return RefTarget{}, false}if c.Targets != nil {- if target, ok := c.Targets[ref]; ok {+ if target, ok := c.Targets[ref]; ok && target.BackendNodeID != 0 {return target, true}}if c.Refs != nil {- if nid, ok := c.Refs[ref]; ok {+ if nid, ok := c.Refs[ref]; ok && nid != 0 {return RefTarget{BackendNodeID: nid}, true}}