Skip to content

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

fix: keep doctor diagnostics on fatal config load (PIN-312)

Luigi Agosti9e0860f6a1b1
diff --git a/cmd/pinchtab/cmd_doctor.go b/cmd/pinchtab/cmd_doctor.go
index 0151fc6b..b550ee24 100644
--- a/cmd/pinchtab/cmd_doctor.go
+++ b/cmd/pinchtab/cmd_doctor.go
@@ -35,7 +35,7 @@ clean bill of health.
Exit codes:
0 all checks passed or were skipped
1 at least one check failed
- 2 usage or setup error (e.g. config could not be loaded)`,
+ 2 usage error (e.g. an unknown check name)`,
Example: ` pinchtab doctor
pinchtab doctor --json
pinchtab doctor browser cloak-eu
@@ -46,9 +46,18 @@ Exit codes:
}
func runDoctor(cmd *cobra.Command, _ []string) error {
- cfg, err := loadDoctorConfig()
- if err != nil {
- return newCommandExitError(2, fmt.Errorf("pinchtab doctor: %w", err))
+ cfg, diagnostics, loadErr := config.LoadConfig()
+ config.EmitLoadDiagnostics(diagnostics)
+ if cfg == nil {
+ return newCommandExitError(2, fmt.Errorf("pinchtab doctor: no configuration found"))
+ }
+ if loadErr != nil {
+ check := strings.TrimSpace(doctorCheck)
+ if check != "" && !doctor.KnownCheck(cfg, check) {
+ return newCommandExitError(2, fmt.Errorf("pinchtab doctor: unknown check %q for browser=%s", check, cfg.DefaultBrowser))
+ }
+ results := doctor.RunWithConfigError(cmd.Context(), cfg, check, loadErr)
+ return runDoctorResults(cmd, cfg, results, "pinchtab doctor", "")
}
return runDoctorChecks(cmd, cfg, doctorCheck, "pinchtab doctor", "")
}
@@ -66,6 +75,10 @@ func runDoctorChecks(cmd *cobra.Command, cfg *config.RuntimeConfig, check, errPr
}
results := doctor.Run(cmd.Context(), cfg, check)
+ return runDoctorResults(cmd, cfg, results, errPrefix, target)
+}
+
+func runDoctorResults(cmd *cobra.Command, cfg *config.RuntimeConfig, results []doctor.CheckResult, errPrefix, target string) error {
browser := config.NormalizeBrowser(cfg.DefaultBrowser)
out := cmd.OutOrStdout()
runtime := doctor.ProbeRuntime(cmd.Context(), cfg)
diff --git a/cmd/pinchtab/cmd_doctor_test.go b/cmd/pinchtab/cmd_doctor_test.go
index 7a11d70a..f65b7418 100644
--- a/cmd/pinchtab/cmd_doctor_test.go
+++ b/cmd/pinchtab/cmd_doctor_test.go
@@ -4,6 +4,7 @@ import (
"bytes"
"context"
"errors"
+ "os"
"path/filepath"
"strings"
"testing"
@@ -215,3 +216,25 @@ func doctorTestCommand(out *bytes.Buffer) *cobra.Command {
cmd.SetContext(context.Background())
return cmd
}
+
+func TestRunDoctorReportsFatalConfigInsteadOfAborting(t *testing.T) {
+ configPath := filepath.Join(t.TempDir(), "config.json")
+ t.Setenv("PINCHTAB_CONFIG", configPath)
+ if err := os.WriteFile(configPath, []byte(`{"sessions":{"agent":{"mode":"required"}}}`), 0o600); err != nil {
+ t.Fatal(err)
+ }
+ restore := setDoctorGlobals("", false)
+ t.Cleanup(restore)
+
+ var out bytes.Buffer
+ err := runDoctor(doctorTestCommand(&out), nil)
+ if got := commandExitCode(err); got != 1 {
+ t.Fatalf("exit code = %d, want 1; err=%v", got, err)
+ }
+ report := out.String()
+ for _, want := range []string{"config_file", "FAIL", "sessions.agent.mode", "required", "SKIP", "valid loaded configuration"} {
+ if !strings.Contains(report, want) {
+ t.Fatalf("doctor report missing %q:\n%s", want, report)
+ }
+ }
+}
diff --git a/internal/doctor/runner.go b/internal/doctor/runner.go
index 41c0d00b..d7f8313a 100644
--- a/internal/doctor/runner.go
+++ b/internal/doctor/runner.go
@@ -182,16 +182,35 @@ func browserCheckResult(ctx context.Context, dc browsers.DoctorCheck, env *brows
// Run executes the diagnostic pipeline; when checkFilter is non-empty only
// the named check runs.
func Run(ctx context.Context, cfg *config.RuntimeConfig, checkFilter string) []CheckResult {
+ return run(ctx, cfg, checkFilter, nil)
+}
+
+// RunWithConfigError produces a useful diagnostic report when configuration
+// validation failed. The config check carries the fatal error; checks whose
+// inputs cannot be trusted are retained as explicit skips.
+func RunWithConfigError(ctx context.Context, cfg *config.RuntimeConfig, checkFilter string, configErr error) []CheckResult {
+ return run(ctx, cfg, checkFilter, configErr)
+}
+
+func run(ctx context.Context, cfg *config.RuntimeConfig, checkFilter string, configErr error) []CheckResult {
entries := Registry(cfg)
checkFilter = strings.TrimSpace(checkFilter)
out := make([]CheckResult, 0, len(entries))
for _, e := range entries {
- if checkFilter != "" && e.Name != checkFilter {
+ if checkFilter != "" && e.Name != checkFilter && (configErr == nil || e.Name != "config_file") {
continue
}
start := time.Now()
- r := e.Fn(ctx, cfg)
+ var r CheckResult
+ switch {
+ case configErr == nil:
+ r = e.Fn(ctx, cfg)
+ case e.Name == "config_file":
+ r = CheckResult{Status: StatusFail, Detail: "configuration could not be loaded: " + configErr.Error(), Err: configErr}
+ default:
+ r = CheckResult{Status: StatusSkip, Detail: "requires a valid loaded configuration"}
+ }
r.Name = e.Name
r.LaunchesBrowser = e.LaunchesBrowser
if r.Duration == 0 {
diff --git a/internal/doctor/runner_test.go b/internal/doctor/runner_test.go
index de83de4b..3497398e 100644
--- a/internal/doctor/runner_test.go
+++ b/internal/doctor/runner_test.go
@@ -371,3 +371,23 @@ func TestBinaryStarts_DoesNotPassWithoutAVersion(t *testing.T) {
t.Errorf("detail = %q, want it to say no version was returned", r.Detail)
}
}
+
+func TestRunWithConfigErrorFailsConfigAndSkipsDependentChecks(t *testing.T) {
+ err := errors.New(`sessions.agent.mode: unsupported value "required"`)
+ results := RunWithConfigError(context.Background(), &config.RuntimeConfig{DefaultBrowser: config.BrowserChrome}, "", err)
+ if len(results) < 2 {
+ t.Fatalf("results = %v, want a report with dependent checks", results)
+ }
+ if results[0].Name != "config_file" || results[0].Status != StatusFail || !strings.Contains(results[0].Detail, "sessions.agent.mode") {
+ t.Fatalf("config result = %+v, want failing field-specific diagnosis", results[0])
+ }
+ for _, result := range results[1:] {
+ if result.Status != StatusSkip || !strings.Contains(result.Detail, "valid loaded configuration") {
+ t.Fatalf("dependent result = %+v, want explicit config-dependent skip", result)
+ }
+ }
+ filtered := RunWithConfigError(context.Background(), &config.RuntimeConfig{DefaultBrowser: config.BrowserChrome}, "binary_exists", err)
+ if len(filtered) != 2 || filtered[0].Name != "config_file" || filtered[0].Status != StatusFail || filtered[1].Name != "binary_exists" || filtered[1].Status != StatusSkip {
+ t.Fatalf("filtered results = %+v, want config failure plus requested dependent skip", filtered)
+ }
+}