High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: align doctor, mcp tools, and cli with browser abstraction
Bosch34fe191a39d8
diff --git a/cmd/pinchtab/cmd_doctor_browser.go b/cmd/pinchtab/cmd_doctor_browser.goindex d43f0b60..052e2e07 100644--- a/cmd/pinchtab/cmd_doctor_browser.go+++ b/cmd/pinchtab/cmd_doctor_browser.go@@ -5,6 +5,7 @@ import ("io""strings"+ "github.com/pinchtab/pinchtab/internal/browsers"_ "github.com/pinchtab/pinchtab/internal/browsers/all""github.com/pinchtab/pinchtab/internal/config""github.com/pinchtab/pinchtab/internal/doctor"@@ -42,11 +43,16 @@ func runDoctorBrowser(cmd *cobra.Command, args []string) error {target := strings.TrimSpace(args[0])- // If the argument is a bare browser ID (not a configured target),- // show the overview focused on that browser.+ // A bare known browser ID (not a configured target) gets the overview+ // focused on that browser; anything else is an error so scripts can+ // rely on the documented exit contract.resolved, err := config.ResolveExplicitBrowserTarget(cfg, target)if err != nil {- return runDoctorBrowserOverview(cmd, cfg, target)+ id := strings.ToLower(target)+ if _, known := browsers.Get(id); known {+ return runDoctorBrowserOverview(cmd, cfg, id)+ }+ return newCommandExitError(2, fmt.Errorf("pinchtab doctor browser: unknown browser or target %q: %w", target, err))}cfg = resolved.Configdiff --git a/cmd/pinchtab/cmd_doctor_test.go b/cmd/pinchtab/cmd_doctor_test.goindex 5b98c8fe..62e9be1b 100644--- a/cmd/pinchtab/cmd_doctor_test.go+++ b/cmd/pinchtab/cmd_doctor_test.go@@ -3,6 +3,7 @@ package mainimport ("bytes""context"+ "errors""path/filepath""strings""testing"@@ -11,7 +12,10 @@ import ("github.com/spf13/cobra")-func TestRunDoctorBrowserShowsOverviewForUnknownTarget(t *testing.T) {+// M7 regression: an unknown target/browser must return an error (non-zero+// exit) so scripts can rely on the documented exit contract, while a bare+// KNOWN browser ID still gets the focused overview.+func TestRunDoctorBrowserUnknownTargetReturnsError(t *testing.T) {writeDoctorTestConfig(t, func(fc *config.FileConfig) {fc.Browser.Targets = config.BrowserTargetsConfig{"chrome": {Provider: config.BrowserChrome},@@ -21,15 +25,20 @@ func TestRunDoctorBrowserShowsOverviewForUnknownTarget(t *testing.T) {var out bytes.Buffererr := runDoctorBrowser(doctorTestCommand(&out), []string{"nonexistent"})- if err != nil {- t.Fatalf("runDoctorBrowser() unexpected error: %v", err)+ if err == nil {+ t.Fatal("expected error for unknown browser/target")}- output := out.String()- if !strings.Contains(output, "not a known provider") {- t.Fatalf("expected unknown browser message, got %q", output)+ var coded *commandExitError+ if !errors.As(err, &coded) || coded.ExitCode() == 0 {+ t.Fatalf("expected non-zero commandExitError, got %v", err)+ }+ if !strings.Contains(err.Error(), "nonexistent") {+ t.Fatalf("error should name the unknown target: %v", err)}- if !strings.Contains(output, "Known browsers:") {- t.Fatalf("expected known browsers list, got %q", output)++ out.Reset()+ if err := runDoctorBrowser(doctorTestCommand(&out), []string{"cloak"}); err != nil {+ t.Fatalf("bare known browser ID should show the focused overview: %v", err)}}diff --git a/internal/doctor/browsers.go b/internal/doctor/browsers.goindex 5e181a46..a7b748ad 100644--- a/internal/doctor/browsers.go+++ b/internal/doctor/browsers.go@@ -52,8 +52,8 @@ func ReportBrowsers(ctx context.Context, cfg *config.RuntimeConfig) BrowsersRepoconfigured := cfgBrowsersAvailable(cfg)defaultBrowser := cfgDefaultBrowser(cfg)- // Build a union of configured + known browser IDs, preserving order:- // configured first, then any known-only IDs.+ // Build a deduplicated union of configured + known browser IDs; the+ // sort below makes the report order deterministic.seen := map[string]bool{}var union []stringfor _, id := range configured {@@ -70,8 +70,6 @@ func ReportBrowsers(ctx context.Context, cfg *config.RuntimeConfig) BrowsersRepo}sort.Strings(union)- env := buildDoctorEnv(cfg)-infos := make([]BrowserInfo, 0, len(union))for _, id := range union {info := BrowserInfo{@@ -84,6 +82,7 @@ func ReportBrowsers(ctx context.Context, cfg *config.RuntimeConfig) BrowsersRepoif ok {info.Registered = true+ env := doctorEnvForBrowser(cfg, id)for _, dc := range b.DoctorChecks(browsers.TargetConfig{Provider: id}) {r := dc.Fn(ctx, env)info.Checks = append(info.Checks, CheckResult{@@ -190,3 +189,34 @@ func errMsg(err error) string {}return err.Error()}++// doctorEnvForBrowser resolves the provider's effective config (its target's+// binary/cloak settings) before building the check env — checking cloak+// against the global chrome binary yields meaningless PASS/FAIL results.+// Providers without a configured target fall back to the global config.+func doctorEnvForBrowser(cfg *config.RuntimeConfig, browserID string) *browsers.DoctorEnv {+ if cfg == nil {+ return nil+ }+ matches := config.TargetsForBrowser(cfg, browserID)+ name := ""+ switch len(matches) {+ case 0:+ case 1:+ name = matches[0]+ default:+ dt := config.ResolveDefaultTarget(cfg)+ for _, m := range matches {+ if m == dt {+ name = dt+ break+ }+ }+ }+ if name != "" {+ if resolved, err := config.ResolveExplicitBrowserTarget(cfg, name); err == nil {+ return buildDoctorEnv(resolved.Config)+ }+ }+ return buildDoctorEnv(cfg)+}diff --git a/internal/doctor/browsers_test.go b/internal/doctor/browsers_test.goindex e604d3fc..526cd410 100644--- a/internal/doctor/browsers_test.go+++ b/internal/doctor/browsers_test.go@@ -341,3 +341,25 @@ func TestReportBrowsers_NeverModifiesConfig(t *testing.T) {t.Errorf("DefaultBrowser changed: %q -> %q", origDefault, cfg.DefaultBrowser)}}++// M7 regression: each provider's doctor checks must run against ITS+// target-resolved binary — checking cloak against the global chrome binary+// yields meaningless results.+func TestDoctorEnvForBrowser_ResolvesTargetBinary(t *testing.T) {+ cfg := &config.RuntimeConfig{+ BrowserBinary: "/usr/bin/chrome",+ Targets: config.BrowserTargetsConfig{+ "cloak-1": {Provider: config.BrowserCloak, Binary: "/opt/cloak/bin"},+ },+ }++ env := doctorEnvForBrowser(cfg, "cloak")+ if env == nil || env.Binary != "/opt/cloak/bin" {+ t.Fatalf("cloak env should carry the target binary, got %+v", env)+ }++ env = doctorEnvForBrowser(cfg, "chrome")+ if env == nil || env.Binary != "/usr/bin/chrome" {+ t.Fatalf("provider without a target should fall back to the global binary, got %+v", env)+ }+}diff --git a/internal/doctor/runner.go b/internal/doctor/runner.goindex a7e7d6fb..ec7dc66f 100644--- a/internal/doctor/runner.go+++ b/internal/doctor/runner.go@@ -20,12 +20,15 @@ const ()type CheckResult struct {- Name string `json:"name"`- Status CheckStatus `json:"status"`- Detail string `json:"detail"`- Err error `json:"-"`- ErrMsg string `json:"error,omitempty"`- Duration time.Duration `json:"durationMs"`+ Name string `json:"name"`+ Status CheckStatus `json:"status"`+ Detail string `json:"detail"`+ Err error `json:"-"`+ ErrMsg string `json:"error,omitempty"`+ // Duration is for human-facing output; DurationMS is its JSON shape —+ // a raw time.Duration would marshal nanoseconds under a "Ms" key.+ Duration time.Duration `json:"-"`+ DurationMS int64 `json:"durationMs"`}type CheckFunc func(ctx context.Context, cfg *config.RuntimeConfig) CheckResult@@ -143,6 +146,7 @@ func Run(ctx context.Context, cfg *config.RuntimeConfig, checkFilter string) []Cif r.Duration == 0 {r.Duration = time.Since(start)}+ r.DurationMS = r.Duration.Milliseconds()if r.Err != nil && r.ErrMsg == "" {r.ErrMsg = r.Err.Error()}diff --git a/internal/doctor/runner_test.go b/internal/doctor/runner_test.goindex 41b2580b..9e4dd0c0 100644--- a/internal/doctor/runner_test.go+++ b/internal/doctor/runner_test.go@@ -265,3 +265,16 @@ func TestWriteJSON_Structure(t *testing.T) {t.Errorf("summary wrong: %+v", report.Summary)}}++// M7 regression: durationMs must marshal milliseconds, not raw nanoseconds.+func TestCheckResultMarshalsDurationInMilliseconds(t *testing.T) {+ r := CheckResult{Name: "x", Status: StatusPass, Duration: 1500 * time.Millisecond}+ r.DurationMS = r.Duration.Milliseconds()+ data, err := json.Marshal(r)+ if err != nil {+ t.Fatalf("marshal: %v", err)+ }+ if !strings.Contains(string(data), `"durationMs":1500`) {+ t.Fatalf("expected durationMs in milliseconds, got %s", data)+ }+}diff --git a/internal/mcp/handlers_content.go b/internal/mcp/handlers_content.goindex 348f12ab..6618daf8 100644--- a/internal/mcp/handlers_content.go+++ b/internal/mcp/handlers_content.go@@ -20,9 +20,6 @@ func handleEval(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.Callif tabID := optString(r, "tabId"); tabID != "" {payload["tabId"] = tabID}- if browser := optString(r, "browser"); browser != "" {- payload["browser"] = browser- }body, code, err := c.Post(ctx, "/evaluate", payload)if err != nil {return mcp.NewToolResultError(err.Error()), nil@@ -46,9 +43,6 @@ func handlePDF(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.CallTif pr := optString(r, "pageRanges"); pr != "" {q.Set("pageRanges", pr)}- if browser := optString(r, "browser"); browser != "" {- q.Set("browser", browser)- }body, code, err := c.Get(ctx, "/pdf", q)if err != nil {return mcp.NewToolResultError(err.Error()), nil@@ -67,9 +61,6 @@ func handleFind(c *Client) func(context.Context, mcp.CallToolRequest) (*mcp.Callif tabID := optString(r, "tabId"); tabID != "" {payload["tabId"] = tabID}- if browser := optString(r, "browser"); browser != "" {- payload["browser"] = browser- }body, code, err := c.Post(ctx, "/find", payload)if err != nil {return mcp.NewToolResultError(err.Error()), nildiff --git a/internal/mcp/handlers_wait.go b/internal/mcp/handlers_wait.goindex c714fb37..9b1d6811 100644--- a/internal/mcp/handlers_wait.go+++ b/internal/mcp/handlers_wait.go@@ -89,10 +89,6 @@ func callWaitEndpoint(ctx context.Context, c *Client, r mcp.CallToolRequest, payif tabID := optString(r, "tabId"); tabID != "" {payload["tabId"] = tabID}- if browser := optString(r, "browser"); browser != "" {- payload["browser"] = browser- }-body, code, err := c.Post(ctx, "/wait", payload)if err != nil {return mcp.NewToolResultError(err.Error()), nildiff --git a/internal/mcp/tools.go b/internal/mcp/tools.goindex 3c5b78d8..14cb6eb9 100644--- a/internal/mcp/tools.go+++ b/internal/mcp/tools.go@@ -214,8 +214,6 @@ func allTools() []mcp.Tool {mcp.WithDescription("Execute JavaScript in the browser and return the result. This is not frame-scoped; current pinchtab_frame state does not change evaluation context."),mcp.WithString("expression", mcp.Required(), mcp.Description("JavaScript expression to evaluate")),mcp.WithString("tabId", mcp.Description("Target tab ID")),- mcp.WithString("browser",- mcp.Description("Browser to use for this request (e.g. chrome, cloak, ghost-chrome).")),),mcp.NewTool("pinchtab_pdf",mcp.WithDescription("Export the current page as a PDF (returns base64-encoded PDF)"),@@ -223,15 +221,11 @@ func allTools() []mcp.Tool {mcp.WithBoolean("landscape", mcp.Description("Landscape orientation")),mcp.WithNumber("scale", mcp.Description("Print scale 0.1-2.0 (default: 1.0)")),mcp.WithString("pageRanges", mcp.Description("Pages to export (e.g., '1-3,5')")),- mcp.WithString("browser",- mcp.Description("Browser to use for this request (e.g. chrome, cloak, ghost-chrome).")),),mcp.NewTool("pinchtab_find",mcp.WithDescription("Find elements by natural-language query. Returns best_ref and selector hints that should be reused in action tools."),mcp.WithString("query", mcp.Required(), mcp.Description("Text or CSS selector to search for")),mcp.WithString("tabId", mcp.Description("Target tab ID")),- mcp.WithString("browser",- mcp.Description("Browser to use for this request (e.g. chrome, cloak, ghost-chrome).")),),// ── Tab Management ──────────────────────────────────────────@@ -265,16 +259,12 @@ func allTools() []mcp.Tool {mcp.WithNumber("timeout", mcp.Description("Timeout in milliseconds (default: 10000, max: 30000)")),mcp.WithString("state", mcp.Description("Element state to wait for: 'visible' (default) or 'hidden'")),mcp.WithString("tabId", mcp.Description("Target tab ID")),- mcp.WithString("browser",- mcp.Description("Browser to use for this request (e.g. chrome, cloak, ghost-chrome).")),),mcp.NewTool("pinchtab_wait_for_text",mcp.WithDescription("Wait for specific text to appear on the page"),mcp.WithString("text", mcp.Required(), mcp.Description("Text to wait for")),mcp.WithNumber("timeout", mcp.Description("Timeout in milliseconds (default: 10000, max: 30000)")),mcp.WithString("tabId", mcp.Description("Target tab ID")),- mcp.WithString("browser",- mcp.Description("Browser to use for this request (e.g. chrome, cloak, ghost-chrome).")),),mcp.NewTool("pinchtab_wait_for_url",mcp.WithDescription("Wait for the page URL to match a glob pattern"),