Skip to content

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

fix(cdptk): bring every screenshot capture under the surface-fallback rule

Luigi Agosti6e1762eb3514
diff --git a/internal/autosolver/adapters/pinchtab.go b/internal/autosolver/adapters/pinchtab.go
index c94b59e1..7ed59fec 100644
--- a/internal/autosolver/adapters/pinchtab.go
+++ b/internal/autosolver/adapters/pinchtab.go
@@ -65,13 +65,17 @@ func (p *PinchtabPage) HTMLWithin(timeout time.Duration) (string, error) {
return html, nil
}
+// Screenshot goes through the bridge this adapter already holds rather than
+// chromedp's helper. The helper leaves fromSurface at CDP's default of true,
+// which waits for a fresh compositor frame — and an idle headed page (a captcha
+// challenge sitting still is the autosolver's whole subject) stops swapping
+// frames, so the capture blocks until the action deadline. The bridge engine
+// owns that rule for every caller.
func (p *PinchtabPage) Screenshot() ([]byte, error) {
- var buf []byte
- err := chromedp.Run(p.ctx, chromedp.CaptureScreenshot(&buf))
- if err != nil {
- return nil, fmt.Errorf("screenshot: %w", err)
+ if p.b == nil {
+ return nil, fmt.Errorf("screenshot: no bridge")
}
- return buf, nil
+ return p.b.CaptureScreenshot(p.ctx, "png", 0, nil)
}
// PinchtabExecutor implements autosolver.ActionExecutor by delegating
diff --git a/internal/autosolver/adapters/pinchtab_test.go b/internal/autosolver/adapters/pinchtab_test.go
index e2fd6e0f..c2a2beed 100644
--- a/internal/autosolver/adapters/pinchtab_test.go
+++ b/internal/autosolver/adapters/pinchtab_test.go
@@ -8,6 +8,8 @@ import (
"time"
"github.com/chromedp/chromedp"
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/cdptk"
)
// stalledEndpoint listens on a TCP port, accepts connections, and then never
@@ -112,3 +114,54 @@ func TestHTMLWithin_StalledFetchDoesNotLeakGoroutine(t *testing.T) {
"a stalled HTML fetch appears to leak a worker", grew, iterations, before, after)
}
}
+
+// stubBridge answers CaptureScreenshot and embeds the interface for everything
+// else, so the test states only the method under test.
+type stubBridge struct {
+ bridge.BridgeAPI
+ format string
+ clip *cdptk.ScreenshotClip
+ called bool
+}
+
+func (s *stubBridge) CaptureScreenshot(_ context.Context, format string, _ int, clip *cdptk.ScreenshotClip) ([]byte, error) {
+ s.called = true
+ s.format = format
+ s.clip = clip
+ return []byte("png-bytes"), nil
+}
+
+// The adapter holds a bridge whose capture engine owns the fromSurface rule.
+// Capturing through chromedp's helper instead left fromSurface at CDP's default
+// of true, which waits for a compositor frame an idle headed page never produces
+// — and an idle challenge page is exactly what the autosolver looks at.
+func TestPageScreenshotGoesThroughTheBridgeEngine(t *testing.T) {
+ b := &stubBridge{}
+ page := NewPinchtabPage(context.Background(), "tab1", b)
+
+ got, err := page.Screenshot()
+ if err != nil {
+ t.Fatalf("Screenshot: %v", err)
+ }
+ if !b.called {
+ t.Fatal("Screenshot did not go through the bridge; it captured on its own and skipped the fromSurface rule")
+ }
+ if string(got) != "png-bytes" {
+ t.Errorf("Screenshot returned %q, want the bridge's bytes", got)
+ }
+ if b.format != "png" {
+ t.Errorf("captured format %q, want png — the interface documents a PNG", b.format)
+ }
+ if b.clip != nil {
+ t.Errorf("captured with clip %+v, want the whole page", b.clip)
+ }
+}
+
+// The adapter is constructed with a nil bridge in this package's own stalled-page
+// test, so the absence must answer rather than panic.
+func TestPageScreenshotWithoutABridgeReportsIt(t *testing.T) {
+ page := NewPinchtabPage(context.Background(), "tab1", nil)
+ if _, err := page.Screenshot(); err == nil {
+ t.Fatal("Screenshot with no bridge returned no error")
+ }
+}
diff --git a/internal/cdptk/screencast.go b/internal/cdptk/screencast.go
index 169299de..fd6585c9 100644
--- a/internal/cdptk/screencast.go
+++ b/internal/cdptk/screencast.go
@@ -9,7 +9,6 @@ import (
"github.com/chromedp/cdproto/cdp"
"github.com/chromedp/cdproto/page"
"github.com/chromedp/cdproto/runtime"
- "github.com/chromedp/chromedp"
"github.com/pinchtab/pinchtab/internal/assets"
)
@@ -27,24 +26,6 @@ var EvaluateInWorld = func(ctx context.Context, params *runtime.EvaluateParams)
return params.Do(ctx)
}
-func CaptureScreenshotJPEG(ctx context.Context, quality int) ([]byte, error) {
- var buf []byte
- err := chromedp.Run(ctx,
- chromedp.ActionFunc(func(c context.Context) error {
- var err error
- buf, err = page.CaptureScreenshot().
- WithFormat(page.CaptureScreenshotFormatJpeg).
- WithQuality(int64(quality)).
- Do(c)
- return err
- }),
- )
- if err != nil {
- return nil, err
- }
- return buf, nil
-}
-
func StartRepaintLoop(ctx context.Context) func() {
execCtxID, err := CreateExecutionContext(ctx)
if err != nil {
diff --git a/internal/cdptk/screenshot_fallback_test.go b/internal/cdptk/screenshot_fallback_test.go
index 4efbc90e..00a561eb 100644
--- a/internal/cdptk/screenshot_fallback_test.go
+++ b/internal/cdptk/screenshot_fallback_test.go
@@ -102,12 +102,10 @@ func TestTheRefusalIsRecognisedInTheErrorTheBrowserSends(t *testing.T) {
// already failing. A source census is the only guard available, and without it this fix is
// unwired the first time someone rewrites a capture site.
func TestEveryCaptureSiteRoutesThroughTheSurfaceFallback(t *testing.T) {
- // Screencast polls frames continuously rather than answering one request, so a retry
- // there belongs to that loop's own cadence rather than to a per-call fallback. Exempt
- // with the reason, not silently skipped.
- exempt := map[string]string{
- "internal/cdptk/screencast.go": "a polling frame loop, not a one-shot capture",
- }
+ // No site is exempt today. An exemption records a capture that legitimately
+ // answers to a different cadence — a polling frame loop rather than one
+ // request — and must carry its reason rather than be silently skipped.
+ exempt := map[string]string{}
sites := 0
for _, file := range srccensus.Tree(t, "../..", moduleFileFloor) {
@@ -146,6 +144,16 @@ const moduleFileFloor = 200
const cdprotoPage = "github.com/chromedp/cdproto/page"
+// captureSpellings are the two ways this module reaches Page.captureScreenshot:
+// cdproto's builder and chromedp's helper. Both land on the same CDP command and
+// both need the fallback, but a census that knew only the builder could not see a
+// helper call at all — which is how a raw capture lived on in an adapter that
+// already held a bridge whose engine owns the rule.
+var captureSpellings = map[string]string{
+ cdprotoPage: ".CaptureScreenshot()",
+ "github.com/chromedp/chromedp": ".CaptureScreenshot(",
+}
+
// capturesAScreenshot resolves the file's OWN local name for cdproto/page before matching
// the call, rather than matching the literal "page.CaptureScreenshot()".
//
@@ -155,7 +163,7 @@ const cdprotoPage = "github.com/chromedp/cdproto/page"
// count still passes. Reading the import makes every spelling of the call one case.
func capturesAScreenshot(t *testing.T, name, text string) bool {
t.Helper()
- if !strings.Contains(text, ".CaptureScreenshot()") {
+ if !strings.Contains(text, ".CaptureScreenshot(") {
return false
}
parsed, err := parser.ParseFile(token.NewFileSet(), name, text, parser.ImportsOnly)
@@ -164,14 +172,18 @@ func capturesAScreenshot(t *testing.T, name, text string) bool {
}
for _, spec := range parsed.Imports {
path, err := strconv.Unquote(spec.Path.Value)
- if err != nil || path != cdprotoPage {
+ if err != nil {
+ continue
+ }
+ call, spelled := captureSpellings[path]
+ if !spelled {
continue
}
- local := "page"
+ local := path[strings.LastIndex(path, "/")+1:]
if spec.Name != nil {
local = spec.Name.Name
}
- if strings.Contains(text, local+".CaptureScreenshot()") {
+ if strings.Contains(text, local+call) {
return true
}
}