Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: an unsatisfiable drag body answers HTTP 500 marked retryable while the bad-button check in the same handler answers 400, so an agent retries a request that can never succeed (PIN-256)

Luigi Agostidede3b60d88b
diff --git a/internal/bridge/action_pointer.go b/internal/bridge/action_pointer.go
index ea60b359..de99b1e4 100644
--- a/internal/bridge/action_pointer.go
+++ b/internal/bridge/action_pointer.go
@@ -623,12 +623,12 @@ func (b *Bridge) actionScroll(ctx context.Context, req ActionRequest) (map[strin
func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]any, error) {
if req.hasDragDestination() {
if req.DragX != 0 || req.DragY != 0 {
- return nil, fmt.Errorf("drag takes a destination (toSelector/toNodeId/toX+toY) or an offset (dragX/dragY), not both")
+ return nil, NewInvalidActionRequestError("drag takes a destination (toSelector/toNodeId/toX+toY) or an offset (dragX/dragY), not both")
}
return b.dragToDestination(ctx, req)
}
if req.DragX == 0 && req.DragY == 0 {
- return nil, fmt.Errorf("dragX or dragY required for drag")
+ return nil, NewInvalidActionRequestError("dragX or dragY required for drag")
}
if req.NodeID > 0 {
err := DragByNodeID(ctx, req.NodeID, req.DragX, req.DragY, req.Button)
@@ -648,7 +648,7 @@ func (b *Bridge) actionDrag(ctx context.Context, req ActionRequest) (map[string]
}
return map[string]any{"dragged": true, "dragX": req.DragX, "dragY": req.DragY}, nil
}
- return nil, fmt.Errorf("need selector, ref, or nodeId")
+ return nil, NewInvalidActionRequestError("need selector, ref, or nodeId")
}
func (b *Bridge) dragToDestination(ctx context.Context, req ActionRequest) (map[string]any, error) {
diff --git a/internal/bridge/action_pointer_drag_test.go b/internal/bridge/action_pointer_drag_test.go
index 05ad12f1..8d81f47f 100644
--- a/internal/bridge/action_pointer_drag_test.go
+++ b/internal/bridge/action_pointer_drag_test.go
@@ -3,7 +3,9 @@ package bridge
import (
"context"
"encoding/base64"
+ "errors"
"os"
+ "strings"
"testing"
"time"
@@ -411,6 +413,45 @@ func TestDragRefusesADestinationAndAnOffsetTogether(t *testing.T) {
}
}
+func TestAnUnsatisfiableDragBodyRefusesWithTheCallerErrorSentinelWithoutActing(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ req ActionRequest
+ wantSays []string
+ }{
+ {
+ name: "a destination and an offset together",
+ req: ActionRequest{Kind: ActionDrag, Selector: "#src", ToSelector: "#dst", DragX: 180},
+ wantSays: []string{"toSelector/toNodeId/toX+toY", "dragX/dragY"},
+ },
+ {
+ name: "an offset drag with no offset",
+ req: ActionRequest{Kind: ActionDrag, Selector: "#src"},
+ wantSays: []string{"dragX or dragY"},
+ },
+ {
+ name: "no target at all",
+ req: ActionRequest{Kind: ActionDrag, DragX: 180},
+ wantSays: []string{"need selector, ref, or nodeId"},
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ result, err := (&Bridge{}).actionDrag(context.Background(), tc.req)
+ if result != nil {
+ t.Errorf("result = %v; a refusal must not report having acted", result)
+ }
+ if !errors.Is(err, ErrInvalidActionRequest) {
+ t.Fatalf("err = %v, want the ErrInvalidActionRequest sentinel; without it the handler reports this caller error as a retryable server fault", err)
+ }
+ for _, says := range tc.wantSays {
+ if !strings.Contains(err.Error(), says) {
+ t.Errorf("err = %q, want it to carry %q; the useful half of the message must survive the classification", err, says)
+ }
+ }
+ })
+ }
+}
+
// handAssembledDrag is the sequence the CLI used to post as four independent requests: one
// move to the source, a press, ONE move to the destination, a release.
func (f *dragFixture) handAssembledDrag(t *testing.T, from, to string) {
diff --git a/internal/bridge/guards.go b/internal/bridge/guards.go
index bff6b77e..861fa6af 100644
--- a/internal/bridge/guards.go
+++ b/internal/bridge/guards.go
@@ -15,9 +15,14 @@ var (
// but the page URL changed.
ErrUnexpectedNavigation = errors.New("unexpected page navigation")
// ErrElementStale indicates the targeted DOM/backend node is no longer valid.
- ErrElementStale = errors.New("element reference is stale")
+ ErrElementStale = errors.New("element reference is stale")
+ ErrInvalidActionRequest = errors.New("invalid action request")
)
+func NewInvalidActionRequestError(msg string) error {
+ return fmt.Errorf("%w: %s", ErrInvalidActionRequest, msg)
+}
+
// URLReader is used by guards to read the current tab URL from an action context.
type URLReader func(ctx context.Context) (string, error)
diff --git a/internal/handlers/action_caller_error_test.go b/internal/handlers/action_caller_error_test.go
new file mode 100644
index 00000000..794229ca
--- /dev/null
+++ b/internal/handlers/action_caller_error_test.go
@@ -0,0 +1,125 @@
+package handlers
+
+import (
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+)
+
+type actionFailure struct {
+ Code string `json:"code"`
+ Error string `json:"error"`
+ Retryable bool `json:"retryable"`
+}
+
+func postActionBody(t *testing.T, mb *mockBridge, body string) (*httptest.ResponseRecorder, actionFailure) {
+ t.Helper()
+ h := New(mb, &config.RuntimeConfig{ActionTimeout: time.Second}, nil, nil, nil)
+ w := httptest.NewRecorder()
+ h.HandleAction(w, httptest.NewRequest("POST", "/action", strings.NewReader(body)))
+
+ var failure actionFailure
+ if err := json.Unmarshal(w.Body.Bytes(), &failure); err != nil {
+ t.Fatalf("decode response: %v body=%s", err, w.Body.String())
+ }
+ return w, failure
+}
+
+func TestAnUnsatisfiableActionBodyIsACallerErrorNotAServerFault(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ body string
+ executeErr error
+ wantStatus int
+ wantCode string
+ wantRetry bool
+ wantSays string
+ }{
+ {
+ name: "a destination and an offset together",
+ body: `{"kind":"drag","nodeId":42,"toNodeId":43,"dragX":10,"tabId":"tab1"}`,
+ executeErr: fmt.Errorf("action drag: %w", bridge.NewInvalidActionRequestError("drag takes a destination (toSelector/toNodeId/toX+toY) or an offset (dragX/dragY), not both")),
+ wantStatus: http.StatusBadRequest,
+ wantCode: "invalid_action_request",
+ wantRetry: false,
+ wantSays: "not both",
+ },
+ {
+ name: "an offset drag with no offset",
+ body: `{"kind":"drag","nodeId":42,"tabId":"tab1"}`,
+ executeErr: fmt.Errorf("action drag: %w", bridge.NewInvalidActionRequestError("dragX or dragY required for drag")),
+ wantStatus: http.StatusBadRequest,
+ wantCode: "invalid_action_request",
+ wantRetry: false,
+ wantSays: "dragX or dragY",
+ },
+ {
+ name: "no target at all",
+ body: `{"kind":"drag","dragX":10,"tabId":"tab1"}`,
+ executeErr: fmt.Errorf("action drag: %w", bridge.NewInvalidActionRequestError("need selector, ref, or nodeId")),
+ wantStatus: http.StatusBadRequest,
+ wantCode: "invalid_action_request",
+ wantRetry: false,
+ wantSays: "need selector, ref, or nodeId",
+ },
+ {
+ name: "an unrecognised button name is still the handler's own 400",
+ body: `{"kind":"drag","nodeId":42,"dragX":10,"button":"rihgt","tabId":"tab1"}`,
+ wantStatus: http.StatusBadRequest,
+ wantCode: "invalid_mouse_button",
+ wantRetry: false,
+ wantSays: "rihgt",
+ },
+ {
+ name: "a genuine runtime failure is still a server fault the caller may retry",
+ body: `{"kind":"drag","nodeId":42,"dragX":10,"tabId":"tab1"}`,
+ executeErr: fmt.Errorf("action drag: dispatch mouse event: websocket closed"),
+ wantStatus: http.StatusInternalServerError,
+ wantCode: "action_failed",
+ wantRetry: true,
+ wantSays: "websocket closed",
+ },
+ } {
+ t.Run(tc.name, func(t *testing.T) {
+ mb := &mockBridge{
+ availableActions: []string{bridge.ActionDrag},
+ executeActionErr: tc.executeErr,
+ }
+ w, failure := postActionBody(t, mb, tc.body)
+
+ if w.Code != tc.wantStatus {
+ t.Errorf("status = %d, want %d; an unsatisfiable body must not be reported as a server fault, and a real fault must not be reported as the caller's error: %s",
+ w.Code, tc.wantStatus, w.Body.String())
+ }
+ if failure.Code != tc.wantCode {
+ t.Errorf("code = %q, want %q", failure.Code, tc.wantCode)
+ }
+ if failure.Retryable != tc.wantRetry {
+ t.Errorf("retryable = %v, want %v — retrying an unsatisfiable body can never succeed, and a transient fault is worth retrying", failure.Retryable, tc.wantRetry)
+ }
+ if !strings.Contains(w.Body.String(), tc.wantSays) {
+ t.Errorf("body = %s, want it to carry %q; the useful half of the message must survive the status change", w.Body.String(), tc.wantSays)
+ }
+ })
+ }
+}
+
+func TestTheCallerErrorArmKeysOnTheSentinelNotTheMessage(t *testing.T) {
+ mb := &mockBridge{
+ availableActions: []string{bridge.ActionDrag},
+ executeActionErr: fmt.Errorf("action drag: dragX or dragY required for drag"),
+ }
+ w, failure := postActionBody(t, mb, `{"kind":"drag","nodeId":42,"tabId":"tab1"}`)
+
+ if w.Code != http.StatusInternalServerError || failure.Code != "action_failed" {
+ t.Fatalf("status %d code %q for an unwrapped error carrying the same wording; the handler is matching the MESSAGE, so any action whose runtime failure happens to read like a body refusal would be mis-reported as a caller error",
+ w.Code, failure.Code)
+ }
+}
diff --git a/internal/handlers/actions.go b/internal/handlers/actions.go
index bc6b4f3d..5e5361fc 100644
--- a/internal/handlers/actions.go
+++ b/internal/handlers/actions.go
@@ -463,6 +463,10 @@ func (h *Handlers) HandleAction(w http.ResponseWriter, r *http.Request) {
})
return
}
+ if errors.Is(actionErr, bridge.ErrInvalidActionRequest) {
+ httpx.ErrorCode(w, http.StatusBadRequest, "invalid_action_request", fmt.Sprintf("action %s: %v", req.Kind, actionErr), false, nil)
+ return
+ }
if errors.Is(actionErr, bridge.ErrUnexpectedNavigation) {
httpx.ErrorCode(w, 409, "navigation_changed", actionErr.Error(), false, navigationChangedDetails(actionErr))
return