High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(eval): detect the Promise subtype without returnByValue so the --await-promise hint fires on real Chrome, fold Evaluate onto EvaluateSubtype, and classify the hint call site (PIN-410)
Luigi Agosti32ac5ff37993
diff --git a/internal/bridge/evaluate.go b/internal/bridge/evaluate.goindex 40dd6e64..24a63d75 100644--- a/internal/bridge/evaluate.go+++ b/internal/bridge/evaluate.go@@ -8,31 +8,45 @@ import ("github.com/chromedp/chromedp")+// Evaluate runs expression in the tab and unmarshals the returned value into+// result. It delegates to EvaluateSubtype so a single decode body serves both;+// callers that only need the value ignore the subtype.func (b *Bridge) Evaluate(ctx context.Context, expression string, result any, opts EvalOpts) error {- var chromedpOpts []chromedp.EvaluateOption- if opts.AwaitPromise {- chromedpOpts = append(chromedpOpts, func(p *runtime.EvaluateParams) *runtime.EvaluateParams {- return p.WithAwaitPromise(true)- })- }- return chromedp.Run(ctx, chromedp.Evaluate(expression, result, chromedpOpts...))+ _, err := b.EvaluateSubtype(ctx, expression, result, opts)+ return err}-// EvaluateSubtype runs expression like Evaluate but also reports the returned-// RemoteObject's subtype (e.g. "promise"), which chromedp.Evaluate's by-value-// decode throws away. A Promise returned without AwaitPromise serialises to an-// empty object, so the subtype is the only thing that tells it apart from a-// genuine empty object — letting the caller hint --await-promise instead of-// silently handing back {}. The value decode and exception handling mirror-// chromedp.Evaluate, so behaviour is otherwise unchanged.+// EvaluateSubtype runs expression and reports the returned RemoteObject's subtype+// (e.g. "promise") alongside decoding its value into result. A Promise returned+// without AwaitPromise serialises to {} with no by-value payload, so the subtype+// is the only thing that tells it apart from a genuine empty object — letting the+// caller hint --await-promise instead of silently handing back {}.+//+// Detection and decode share one evaluation, so a side-effecting snippet runs+// exactly once. With AwaitPromise the resolved value is returned by value as+// before. Otherwise the expression is evaluated WITHOUT returnByValue — the only+// mode under which CDP populates subtype:"promise" (returnByValue serialises the+// Promise and drops the subtype) — and the value is read back from the same+// result: a primitive carries it inline, an object is serialised from its own+// handle via CallFunctionOn, which is byte-identical to a direct by-value return+// (a Promise serialises to {}, matching the prior output) and never re-runs the+// expression.func (b *Bridge) EvaluateSubtype(ctx context.Context, expression string, result any, opts EvalOpts) (string, error) {var subtype stringerr := chromedp.Run(ctx, chromedp.ActionFunc(func(ctx context.Context) error {- p := runtime.Evaluate(expression).WithReturnByValue(true)if opts.AwaitPromise {- p = p.WithAwaitPromise(true)+ obj, exc, err := runtime.Evaluate(expression).WithAwaitPromise(true).WithReturnByValue(true).Do(ctx)+ if err != nil {+ return err+ }+ if exc != nil {+ return exc+ }+ subtype = string(obj.Subtype)+ return decodeRemoteValue(obj.Value, result)}- obj, exc, err := p.Do(ctx)++ obj, exc, err := runtime.Evaluate(expression).Do(ctx)if err != nil {return err}@@ -43,11 +57,33 @@ func (b *Bridge) EvaluateSubtype(ctx context.Context, expression string, resultif result == nil {return nil}- value := []byte(obj.Value)- if len(value) == 0 {- value = []byte("null")+ if len(obj.Value) > 0 || obj.ObjectID == "" {+ // A primitive (or null) carries its value inline; undefined and the+ // like have neither a value nor a handle.+ return decodeRemoteValue(obj.Value, result)}- return json.Unmarshal(value, result)+ // An object (a Promise included) carries no inline value here. Serialise it+ // from its own handle so the bytes match a by-value return without running+ // the expression a second time, then release the handle.+ value, _, cferr := runtime.CallFunctionOn("function(){return this;}").+ WithObjectID(obj.ObjectID).WithReturnByValue(true).Do(ctx)+ _ = runtime.ReleaseObject(obj.ObjectID).Do(ctx)+ if cferr != nil {+ return cferr+ }+ return decodeRemoteValue(value.Value, result)}))return subtype, err}++// decodeRemoteValue unmarshals a RemoteObject's by-value JSON into result,+// treating an absent value as JSON null (matching chromedp.Evaluate's decode).+func decodeRemoteValue(value []byte, result any) error {+ if result == nil {+ return nil+ }+ if len(value) == 0 {+ value = []byte("null")+ }+ return json.Unmarshal(value, result)+}diff --git a/internal/bridge/evaluate_subtype_test.go b/internal/bridge/evaluate_subtype_test.gonew file mode 100644index 00000000..cd855639--- /dev/null+++ b/internal/bridge/evaluate_subtype_test.go@@ -0,0 +1,105 @@+package bridge++import (+ "context"+ "encoding/base64"+ "os"+ "testing"+ "time"++ "github.com/chromedp/chromedp"++ "github.com/pinchtab/pinchtab/internal/config"+ "github.com/pinchtab/pinchtab/internal/testbrowser"+)++// EvaluateSubtype's whole job is telling a Promise apart from a genuine empty+// object, and that only works against real Chrome: under WithReturnByValue(true)+// CDP serialises the Promise and drops subtype:"promise", so a mock cannot expose+// the bug this pins. It drives a live browser end to end — a Promise reports the+// subtype while still decoding to {} (the prior output), a plain object decodes+// with no promise subtype, a primitive is unaffected, and --await-promise resolves+// the value. Fails against a WithReturnByValue(true) detection path.+func TestEvaluateSubtypeDetectsPromiseAgainstRealChrome(t *testing.T) {+ 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, cancel := chromedp.NewContext(alloc)+ ctx, cancelTimeout := context.WithTimeout(ctx, 20*time.Second)+ t.Cleanup(func() {+ cancelTimeout()+ cancel()+ cancelAlloc()+ _ = os.RemoveAll(profile)+ })++ dataURL := "data:text/html;base64," + base64.StdEncoding.EncodeToString([]byte("<html><body></body></html>"))+ if err := chromedp.Run(ctx, chromedp.Navigate(dataURL)); err != nil {+ t.Fatal(err)+ }++ b := New(context.Background(), nil, &config.RuntimeConfig{})++ t.Run("un-awaited promise reports the subtype and still decodes to {}", func(t *testing.T) {+ var result any+ subtype, err := b.EvaluateSubtype(ctx, "Promise.resolve(42)", &result, EvalOpts{})+ if err != nil {+ t.Fatalf("EvaluateSubtype: %v", err)+ }+ if subtype != "promise" {+ t.Fatalf("subtype = %q, want promise; the hint can never fire without it", subtype)+ }+ if m, ok := result.(map[string]any); !ok || len(m) != 0 {+ t.Errorf("result = %#v, want the empty object the prior path returned", result)+ }+ })++ t.Run("plain object decodes with no promise subtype", func(t *testing.T) {+ var result any+ subtype, err := b.EvaluateSubtype(ctx, "({a:1})", &result, EvalOpts{})+ if err != nil {+ t.Fatalf("EvaluateSubtype: %v", err)+ }+ if subtype == "promise" {+ t.Errorf("a plain object reported subtype promise: %q", subtype)+ }+ m, ok := result.(map[string]any)+ if !ok || m["a"] != float64(1) {+ t.Errorf("result = %#v, want {a:1} preserved (no regression)", result)+ }+ })++ t.Run("primitive is unaffected", func(t *testing.T) {+ var result any+ subtype, err := b.EvaluateSubtype(ctx, "42", &result, EvalOpts{})+ if err != nil {+ t.Fatalf("EvaluateSubtype: %v", err)+ }+ if subtype == "promise" {+ t.Errorf("a number reported subtype promise: %q", subtype)+ }+ if result != float64(42) {+ t.Errorf("result = %#v, want 42", result)+ }+ })++ t.Run("await-promise resolves the value", func(t *testing.T) {+ var result any+ subtype, err := b.EvaluateSubtype(ctx, "Promise.resolve(42)", &result, EvalOpts{AwaitPromise: true})+ if err != nil {+ t.Fatalf("EvaluateSubtype: %v", err)+ }+ if subtype == "promise" {+ t.Errorf("an awaited promise still reported subtype promise: %q", subtype)+ }+ if result != float64(42) {+ t.Errorf("awaited result = %#v, want 42", result)+ }+ })+}diff --git a/internal/cli/output/hint_census_test.go b/internal/cli/output/hint_census_test.goindex 60d80c32..9c68a69a 100644--- a/internal/cli/output/hint_census_test.go+++ b/internal/cli/output/hint_census_test.go@@ -40,6 +40,7 @@ var hintSites = map[string]struct {"../actions/actions_element.go:printStaleRefsHint": {occurrence, 1, "this action invalidated the snapshot the caller is holding"},"../actions/actions_capture.go:Capture": {occurrence, 1, "this page tripped the IDPI guard"},"../actions/actions_text.go:Text": {occurrence, 1, "readability collapsed on this page"},+ "../actions/actions_evaluate.go:Evaluate": {occurrence, 1, "this eval returned a Promise without --await-promise"},"../actions/actions_tabs.go:TabHandoff": {occurrence, 1, "the reason this handoff carried"},"../actions/actions_tabs.go:TabHandoffStatus": {occurrence, 1, "the reason this handoff carried"},"../../../cmd/pinchtab/cmd_cli_runtime.go:resolveCLIBase": {advisory, 2, "the caller's own --server/PINCHTAB_SERVER is redundant: their setting, not an event"},