Skip to content

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

fix: guard the post-step intent check in trySemantic with intentTypeOf (PIN-055)

Luigi Agostibde4dece2e4b
diff --git a/internal/autosolver/autosolver.go b/internal/autosolver/autosolver.go
index bb72ca8f..a6612c58 100644
--- a/internal/autosolver/autosolver.go
+++ b/internal/autosolver/autosolver.go
@@ -67,9 +67,15 @@ func (as *AutoSolver) Solve(ctx context.Context, page Page, executor ActionExecu
"err", err, "url", page.URL())
intent = &Intent{Type: IntentUnknown, Confidence: 0}
}
- result.Intent = intent.Type
+ // A SemanticEngine may report (nil, nil): no error, no reading. That is the
+ // same "we know nothing" the error branch above substitutes for, so it gets
+ // the same substitution here rather than a nil check at every later read.
+ if intent == nil {
+ intent = &Intent{Type: IntentUnknown, Confidence: 0}
+ }
+ result.Intent = intentTypeOf(intent)
- if intent.Type == IntentNormal {
+ if intentTypeOf(intent) == IntentNormal {
result.Solved = true
result.TotalDuration = time.Since(start)
slog.Info("autosolver_done",
@@ -81,7 +87,7 @@ func (as *AutoSolver) Solve(ctx context.Context, page Page, executor ActionExecu
}
slog.Info("autosolver: challenge detected",
- "type", intent.Type,
+ "type", intentTypeOf(intent),
"confidence", intent.Confidence,
"url", page.URL())
diff --git a/internal/autosolver/autosolver_test.go b/internal/autosolver/autosolver_test.go
index 088bde4c..642220ac 100644
--- a/internal/autosolver/autosolver_test.go
+++ b/internal/autosolver/autosolver_test.go
@@ -1304,3 +1304,36 @@ func TestTrySemanticSurvivesANilIntentFromTheEngine(t *testing.T) {
t.Errorf("attempt status = %q, want %q", entry.Status, StatusFailed)
}
}
+
+// Solve is the exported entry point and reads the intent BEFORE any solving
+// starts, so the (nil, nil) engine reaches it earlier than it reaches
+// trySemantic. The trySemantic test above walked past this because it calls the
+// unexported step directly.
+func TestSolveSurvivesANilIntentFromTheEngine(t *testing.T) {
+ cfg := DefaultConfig()
+ cfg.MaxAttempts = 1
+ semantic := &mockSemantic{
+ detectSeq: []*Intent{nil},
+ action: &SuggestedAction{Action: ActionClick, Selector: "#anything"},
+ }
+
+ as := New(cfg, semantic, nil)
+ page := &mockPage{title: "Verify your identity", url: "https://example.com/gate"}
+
+ result, err := as.Solve(context.Background(), page, &mockExecutor{})
+ if err != nil {
+ t.Fatalf("Solve returned an error: %v", err)
+ }
+ if result == nil {
+ t.Fatal("Solve returned no result")
+ }
+ // An engine that read nothing must not be recorded as reporting a normal
+ // page: IntentNormal is the one value that short-circuits Solve as solved,
+ // which would turn "we know nothing" into "there is no challenge".
+ if result.Intent != IntentUnknown {
+ t.Errorf("result.Intent = %q, want %q for an engine that reported nothing", result.Intent, IntentUnknown)
+ }
+ if result.Solved {
+ t.Error("a nil intent cannot be a solved page; the engine reported nothing")
+ }
+}
diff --git a/internal/autosolver/intent_census_test.go b/internal/autosolver/intent_census_test.go
new file mode 100644
index 00000000..6354572f
--- /dev/null
+++ b/internal/autosolver/intent_census_test.go
@@ -0,0 +1,98 @@
+package autosolver
+
+import (
+ "go/ast"
+ "go/importer"
+ "go/parser"
+ "go/token"
+ "go/types"
+ "os"
+ "strings"
+ "testing"
+)
+
+// intentTypeReadCensus reports every read of an Intent's Type field in the
+// package's production sources, keyed by the function that performs it. It type-
+// checks rather than grepping: the defect this guards was three bare reads on a
+// variable named something else, which a census keyed on a spelling walks past.
+func intentTypeReadCensus(t *testing.T) (violations []string, allowed int) {
+ t.Helper()
+
+ fset := token.NewFileSet()
+ entries, err := os.ReadDir(".")
+ if err != nil {
+ t.Fatal(err)
+ }
+ var files []*ast.File
+ for _, entry := range entries {
+ name := entry.Name()
+ if !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
+ continue
+ }
+ file, err := parser.ParseFile(fset, name, nil, 0)
+ if err != nil {
+ t.Fatalf("parse %s: %v", name, err)
+ }
+ files = append(files, file)
+ }
+ if len(files) == 0 {
+ t.Fatal("census found no production sources; it would pass over nothing")
+ }
+
+ info := &types.Info{Types: map[ast.Expr]types.TypeAndValue{}}
+ conf := types.Config{Importer: importer.ForCompiler(fset, "source", nil)}
+ if _, err := conf.Check("github.com/pinchtab/pinchtab/internal/autosolver", fset, files, info); err != nil {
+ t.Fatalf("type check: %v", err)
+ }
+
+ for _, file := range files {
+ for _, decl := range file.Decls {
+ fn, ok := decl.(*ast.FuncDecl)
+ if !ok || fn.Body == nil {
+ continue
+ }
+ ast.Inspect(fn.Body, func(node ast.Node) bool {
+ sel, ok := node.(*ast.SelectorExpr)
+ if !ok || sel.Sel.Name != "Type" || !isIntentExpr(info, sel.X) {
+ return true
+ }
+ if fn.Name.Name == "intentTypeOf" {
+ allowed++
+ return true
+ }
+ violations = append(violations, fset.Position(sel.Pos()).String()+" in "+fn.Name.Name)
+ return true
+ })
+ }
+ }
+ return violations, allowed
+}
+
+func isIntentExpr(info *types.Info, expr ast.Expr) bool {
+ typ := info.Types[expr].Type
+ if typ == nil {
+ return false
+ }
+ if ptr, ok := typ.Underlying().(*types.Pointer); ok {
+ typ = ptr.Elem()
+ }
+ named, ok := typ.(*types.Named)
+ return ok && named.Obj() != nil && named.Obj().Name() == "Intent"
+}
+
+// A SemanticEngine may return (nil, nil), so intentTypeOf exists to answer
+// IntentUnknown for a nil intent. It only holds while it is the ONLY place that
+// touches the field: the original fix routed one dereference in trySemantic
+// through it and left three in Solve, because the check was a grep for one
+// variable name. This pins the meaning instead — no function but intentTypeOf
+// reads an Intent's Type, whatever the variable is called.
+func TestIntentTypeIsReadOnlyThroughIntentTypeOf(t *testing.T) {
+ violations, allowed := intentTypeReadCensus(t)
+
+ for _, violation := range violations {
+ t.Errorf("bare Intent.Type read at %s; call intentTypeOf(intent) instead — a nil intent panics here", violation)
+ }
+ if allowed == 0 {
+ t.Error("intentTypeOf no longer reads Intent.Type; if the field or the helper was renamed, this census now bans nothing")
+ }
+}