High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: a configured paid solver whose API key is missing is dropped at debug level (PIN-078)
Luigi Agosti7dafe136ac3c
diff --git a/internal/autosolver/autosolver.go b/internal/autosolver/autosolver.goindex b4e846a6..4bacc118 100644--- a/internal/autosolver/autosolver.go+++ b/internal/autosolver/autosolver.go@@ -211,12 +211,46 @@ func (as *AutoSolver) orderSolvers(matching []Solver) []Solver {return matching}- if len(missing) > 0 {+ as.reportMissingSolvers(missing, filtered)+ return filtered+}++// reportMissingSolvers splits the configured-but-unavailable names by whether the+// operator can act on them. A key-gated solver named without its API key is a+// misconfiguration the config deliberately accepts, so it warns and names the key+// to set; anything else is not a config error and stays at debug.+func (as *AutoSolver) reportMissingSolvers(missing []string, running []Solver) {+ if len(missing) == 0 {+ return+ }++ unactionable := make([]string, 0, len(missing))+ for _, name := range missing {+ gated, ok := keyGatedSolverNamed(name)+ if !ok {+ unactionable = append(unactionable, name)+ continue+ }+ slog.Warn(fmt.Sprintf("autosolver: %s is configured but its API key is not set, so it never runs", gated.Name),+ "solver", gated.Name,+ "config_key", gated.ConfigKey,+ "configured", as.config.Solvers,+ "running", solverNamesOf(running))+ }++ if len(unactionable) > 0 {slog.Debug("autosolver: some configured solvers unavailable; using matched subset","configured", as.config.Solvers,- "missing", missing)+ "missing", unactionable)}- return filtered+}++func solverNamesOf(solvers []Solver) []string {+ names := make([]string, 0, len(solvers))+ for _, s := range solvers {+ names = append(names, s.Name())+ }+ return names}func (as *AutoSolver) trySolvers(ctx context.Context, page Page, executor ActionExecutor) (bool, []AttemptEntry) {diff --git a/internal/autosolver/autosolver_test.go b/internal/autosolver/autosolver_test.goindex 8ff30932..a94e1e4c 100644--- a/internal/autosolver/autosolver_test.go+++ b/internal/autosolver/autosolver_test.go@@ -1002,14 +1002,6 @@ func TestExecuteAction_LLMNavigate(t *testing.T) {}}-func solverNames(solvers []Solver) []string {- names := make([]string, 0, len(solvers))- for _, s := range solvers {- names = append(names, s.Name())- }- return names-}-func TestOrderSolvers(t *testing.T) {newPriorityOrder := func() []Solver {return []Solver{@@ -1048,7 +1040,7 @@ func TestOrderSolvers(t *testing.T) {t.Run(tc.name, func(t *testing.T) {as := New(Config{Solvers: tc.configured}, nil, nil)- got := solverNames(as.orderSolvers(newPriorityOrder()))+ got := solverNamesOf(as.orderSolvers(newPriorityOrder()))if !reflect.DeepEqual(got, tc.want) {t.Fatalf("orderSolvers = %v, want %v", got, tc.want)@@ -1067,12 +1059,12 @@ func TestOrderSolversDoesNotMutateInput(t *testing.T) {&mockSolver{name: "alpha", priority: 10},&mockSolver{name: "beta", priority: 20},}- before := solverNames(priorityOrder)+ before := solverNamesOf(priorityOrder)as := New(Config{Solvers: []string{"beta", "alpha"}}, nil, nil)_ = as.orderSolvers(priorityOrder)- if after := solverNames(priorityOrder); !reflect.DeepEqual(before, after) {+ if after := solverNamesOf(priorityOrder); !reflect.DeepEqual(before, after) {t.Fatalf("input reordered in place: %v -> %v", before, after)}}@@ -1119,10 +1111,7 @@ func recordAttrs(rec slog.Record) string {// known but unavailable, e.g. capsolver named without its API key. The fallback// stays — refusing to solve is the worse failure — but it must be visible.func TestOrderSolversWarnsWhenNoConfiguredSolverIsAvailable(t *testing.T) {- recorder := &levelRecorder{}- previous := slog.Default()- slog.SetDefault(slog.New(recorder))- t.Cleanup(func() { slog.SetDefault(previous) })+ recorder := captureLogs(t)priorityOrder := []Solver{&mockSolver{name: "cloudflare", priority: 10},@@ -1130,7 +1119,7 @@ func TestOrderSolversWarnsWhenNoConfiguredSolverIsAvailable(t *testing.T) {}as := New(Config{Solvers: []string{"capsolver", "twocaptcha"}}, nil, nil)- got := solverNames(as.orderSolvers(priorityOrder))+ got := solverNamesOf(as.orderSolvers(priorityOrder))if want := []string{"cloudflare", "jschallenge"}; !reflect.DeepEqual(got, want) {t.Fatalf("fallback returned %v, want the priority order %v", got, want)@@ -1145,21 +1134,63 @@ func TestOrderSolversWarnsWhenNoConfiguredSolverIsAvailable(t *testing.T) {}}-// The partial-match case fires on the shipped default (semantic is a stage, not-// a registry solver, so it never matches), so it stays below warn — promoting it-// would put a warning in front of every operator on every run.-func TestOrderSolversDoesNotWarnOnPartialMatch(t *testing.T) {- recorder := &levelRecorder{}- previous := slog.Default()- slog.SetDefault(slog.New(recorder))- t.Cleanup(func() { slog.SetDefault(previous) })+// A missing name that is not key-gated is nothing the operator can act on, and+// the shipped default hits this path every run (semantic is a stage, not a+// registry solver, so it never matches). Promoting it would put a warning in+// front of every operator on every run.+func TestOrderSolversDoesNotWarnWhenTheMissingSolverIsNotKeyGated(t *testing.T) {+ recorder := captureLogs(t)- as := New(Config{Solvers: []string{"cloudflare", "capsolver"}}, nil, nil)+ as := New(Config{Solvers: []string{"cloudflare", SemanticSolverName}}, nil, nil)as.orderSolvers([]Solver{&mockSolver{name: "cloudflare", priority: 10}})+ if _, ok := keyGatedSolverNamed(SemanticSolverName); ok {+ t.Fatalf("%s is key-gated, so this test no longer covers the non-gated case", SemanticSolverName)+ }for _, rec := range recorder.records {if rec.Level >= slog.LevelWarn {- t.Errorf("partial match logged at %s: %q — the shipped default hits this path every run", rec.Level, rec.Message)+ t.Errorf("non-gated missing solver logged at %s: %q — the shipped default hits this path every run", rec.Level, rec.Message)}}}++// A key-gated solver named without its API key is the one misconfiguration+// solver-name validation deliberately admits, so the runtime is where the+// operator has to learn about it.+func TestOrderSolversWarnsWhenAKeyGatedSolverHasNoAPIKey(t *testing.T) {+ for _, gated := range KeyGatedSolvers() {+ t.Run(gated.Name, func(t *testing.T) {+ recorder := captureLogs(t)++ as := New(Config{Solvers: []string{"cloudflare", gated.Name}}, nil, nil)+ got := solverNamesOf(as.orderSolvers([]Solver{+ &mockSolver{name: "cloudflare", priority: 10},+ &mockSolver{name: "jschallenge", priority: 20},+ }))++ if want := []string{"cloudflare"}; !reflect.DeepEqual(got, want) {+ t.Fatalf("orderSolvers = %v, want the matched subset %v — the warning must not change which solvers run", got, want)+ }++ rec := recorder.find(t, slog.LevelWarn, gated.Name)+ if !strings.Contains(rec.Message, gated.Name) {+ t.Errorf("warn message %q does not name the solver", rec.Message)+ }+ if !strings.Contains(rec.Message, "API key") {+ t.Errorf("warn message %q does not state the missing API key as the reason", rec.Message)+ }+ if attrs := recordAttrs(rec); !strings.Contains(attrs, gated.ConfigKey) {+ t.Errorf("warn line does not name the config key %q to set: %s", gated.ConfigKey, attrs)+ }+ })+ }+}++func captureLogs(t *testing.T) *levelRecorder {+ t.Helper()+ recorder := &levelRecorder{}+ previous := slog.Default()+ slog.SetDefault(slog.New(recorder))+ t.Cleanup(func() { slog.SetDefault(previous) })+ return recorder+}diff --git a/internal/autosolver/catalog/catalog.go b/internal/autosolver/catalog/catalog.goindex 04c3b640..13bde980 100644--- a/internal/autosolver/catalog/catalog.go+++ b/internal/autosolver/catalog/catalog.go@@ -27,11 +27,16 @@ func registrable() []autosolver.Solver {// KeyGated reports the solvers that only register when their API key is set.// Naming one without its key is a missing-key mistake, not an unknown solver.+// The set itself is owned by autosolver.KeyGatedSolvers, which also carries the+// config key each one needs, so the runtime warning and this validation face of+// the same fact cannot drift.func KeyGated() []string {- return []string{- external.NewCapsolver(external.CapsolverConfig{}).Name(),- external.NewTwoCaptcha(external.TwoCaptchaConfig{}).Name(),+ gated := autosolver.KeyGatedSolvers()+ names := make([]string, 0, len(gated))+ for _, solver := range gated {+ names = append(names, solver.Name)}+ return names}// AlwaysRegistered reports the solvers that need no configuration to run.diff --git a/internal/autosolver/catalog/catalog_test.go b/internal/autosolver/catalog/catalog_test.gonew file mode 100644index 00000000..9c47b3f5--- /dev/null+++ b/internal/autosolver/catalog/catalog_test.go@@ -0,0 +1,59 @@+package catalog++import (+ "reflect"+ "sort"+ "testing"++ "github.com/pinchtab/pinchtab/internal/autosolver"+)++func TestKeyGatedMirrorsTheOwningSet(t *testing.T) {+ want := make([]string, 0)+ for _, gated := range autosolver.KeyGatedSolvers() {+ want = append(want, gated.Name)+ if gated.ConfigKey == "" {+ t.Errorf("key-gated solver %q has no config key, so the runtime warning cannot say what to set", gated.Name)+ }+ }+ if len(want) == 0 {+ t.Fatal("autosolver.KeyGatedSolvers is empty, so nothing pins this behaviour")+ }+ if got := KeyGated(); !reflect.DeepEqual(got, want) {+ t.Fatalf("KeyGated() = %v, want the owning set %v", got, want)+ }+}++func TestKeyGatedNamesAreValidConfigValues(t *testing.T) {+ for _, name := range KeyGated() {+ if !IsKnown(name) {+ t.Errorf("key-gated solver %q is rejected by config validation (known: %v)", name, Names())+ }+ }+}++func TestKeyGatedSolversAreExcludedFromAlwaysRegistered(t *testing.T) {+ gated := map[string]struct{}{}+ for _, name := range KeyGated() {+ gated[name] = struct{}{}+ }+ for _, name := range AlwaysRegistered() {+ if _, ok := gated[name]; ok {+ t.Errorf("%q is key-gated yet reported as always registered", name)+ }+ }+}++func TestRegistrableSolversAreAllKnownNames(t *testing.T) {+ names := Names()+ sorted := append([]string(nil), names...)+ sort.Strings(sorted)+ if !reflect.DeepEqual(names, sorted) {+ t.Fatalf("Names() = %v, want sorted output so messages read the same every run", names)+ }+ for _, s := range registrable() {+ if !IsKnown(s.Name()) {+ t.Errorf("registrable solver %q is not a known config value (known: %v)", s.Name(), names)+ }+ }+}diff --git a/internal/autosolver/external/capsolver.go b/internal/autosolver/external/capsolver.goindex ebd7ef2a..eec577e5 100644--- a/internal/autosolver/external/capsolver.go+++ b/internal/autosolver/external/capsolver.go@@ -31,7 +31,7 @@ func NewCapsolver(cfg CapsolverConfig) *Capsolver {return &Capsolver{config: cfg}}-func (c *Capsolver) Name() string { return "capsolver" }+func (c *Capsolver) Name() string { return autosolver.CapsolverSolverName }func (c *Capsolver) Priority() int { return 200 }// CanHandle checks if the page contains a supported CAPTCHA type.diff --git a/internal/autosolver/external/twocaptcha.go b/internal/autosolver/external/twocaptcha.goindex 1adcc3a9..6fac30cd 100644--- a/internal/autosolver/external/twocaptcha.go+++ b/internal/autosolver/external/twocaptcha.go@@ -28,7 +28,7 @@ func NewTwoCaptcha(cfg TwoCaptchaConfig) *TwoCaptcha {return &TwoCaptcha{config: cfg}}-func (t *TwoCaptcha) Name() string { return "twocaptcha" }+func (t *TwoCaptcha) Name() string { return autosolver.TwoCaptchaSolverName }func (t *TwoCaptcha) Priority() int { return 210 }// CanHandle checks if the page contains a supported CAPTCHA type.diff --git a/internal/autosolver/keygated.go b/internal/autosolver/keygated.gonew file mode 100644index 00000000..38995b8f--- /dev/null+++ b/internal/autosolver/keygated.go@@ -0,0 +1,32 @@+package autosolver++const (+ CapsolverSolverName = "capsolver"+ TwoCaptchaSolverName = "twocaptcha"+)++// KeyGatedSolver is a solver that only registers once its API key is set, paired+// with the config key that sets it. Naming one without its key is legal config,+// so the runtime says which key is missing rather than the config refusing to+// load. This is the single owner of both facts: catalog.KeyGated() and the+// registration sites read it instead of restating names.+type KeyGatedSolver struct {+ Name string+ ConfigKey string+}++func KeyGatedSolvers() []KeyGatedSolver {+ return []KeyGatedSolver{+ {Name: CapsolverSolverName, ConfigKey: "autoSolver.external.capsolverKey"},+ {Name: TwoCaptchaSolverName, ConfigKey: "autoSolver.external.twoCaptchaKey"},+ }+}++func keyGatedSolverNamed(name string) (KeyGatedSolver, bool) {+ for _, gated := range KeyGatedSolvers() {+ if gated.Name == name {+ return gated, true+ }+ }+ return KeyGatedSolver{}, false+}diff --git a/internal/handlers/autosolver.go b/internal/handlers/autosolver.goindex 8daf82ed..7b37af49 100644--- a/internal/handlers/autosolver.go+++ b/internal/handlers/autosolver.go@@ -257,10 +257,10 @@ func (h *Handlers) availableAutoSolverNames() []string {}if h != nil && h.Config != nil {if strings.TrimSpace(h.Config.AutoSolver.CapsolverKey) != "" {- available["capsolver"] = true+ available[coreautosolver.CapsolverSolverName] = true}if strings.TrimSpace(h.Config.AutoSolver.TwoCaptchaKey) != "" {- available["twocaptcha"] = true+ available[coreautosolver.TwoCaptchaSolverName] = true}}