Skip to content

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

fix(remedy): route the drift-warning and config-set restart hints through restartHintForMode, reuse probeHealthSnapshot, and single-source the /health dashboard mode (PIN-403)

Luigi Agostid37f51088097
diff --git a/cmd/pinchtab/cmd_config_actions.go b/cmd/pinchtab/cmd_config_actions.go
index b0bdf593..121534c9 100644
--- a/cmd/pinchtab/cmd_config_actions.go
+++ b/cmd/pinchtab/cmd_config_actions.go
@@ -1,19 +1,16 @@
package main
import (
- "encoding/json"
"fmt"
- "net/http"
"os"
"strings"
- "time"
+ "github.com/pinchtab/pinchtab/internal/api/types"
"github.com/pinchtab/pinchtab/internal/cli"
"github.com/pinchtab/pinchtab/internal/cli/output"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/config/workflow"
configschema "github.com/pinchtab/pinchtab/internal/schema"
- "github.com/pinchtab/pinchtab/internal/server"
)
func handleConfigTokenCopy(toStdout bool) {
@@ -236,34 +233,27 @@ func emitDefaultConfigHint() {
func hintRestartIfRunning() {
cfg := loadLocalConfig()
- probe := server.ProbeHealthWithToken(
- fmt.Sprintf("http://localhost:%s/health", cfg.Port), 500*time.Millisecond, cfg.Token)
- if !probe.Reachable || probe.StatusCode != http.StatusOK {
- return
+ snap, state := probeHealthSnapshot(cfg.Port, cfg.Token)
+ switch state {
+ case healthSnapshotRunning:
+ output.Hint(restartHintForMode(snap.Mode))
+ case healthSnapshotProtected:
+ // A token mismatch hides the mode, so the instance is running but we cannot
+ // confirm it is the server front door: still remind the caller to restart,
+ // in the mode-neutral form. Without this a protected instance gets no hint —
+ // the regression the earlier CheckPinchTabRunning did not have.
+ output.Hint(restartHintForMode(""))
}
- output.Hint(restartHintForMode(healthMode(probe.Body)))
}
// restartHintForMode names the restart appropriate to the running instance. Only
-// the server/daemon front door (/health mode "dashboard") is safe to restart with
-// `pinchtab server restart`; a bridge is Ctrl-C + re-run `pinchtab bridge`, and
-// naming the server command would tell a bridge user to kill their bridge. Any
-// other or unknown mode gets the mode-neutral instruction.
+// the server/daemon front door (/health mode ModeDashboard) is safe to restart
+// with `pinchtab server restart`; a bridge is Ctrl-C + re-run `pinchtab bridge`,
+// and naming the server command would tell a bridge user to kill their bridge. Any
+// other, empty, or unknown mode gets the mode-neutral instruction.
func restartHintForMode(mode string) string {
- if mode == "dashboard" {
+ if mode == types.ModeDashboard {
return "Server is running — restart it to apply changes: pinchtab server restart"
}
return "A PinchTab instance is running — restart it to apply this change."
}
-
-// healthMode reads the "mode" field from a /health body, or "" when the body is
-// absent or not the expected JSON.
-func healthMode(body []byte) string {
- var payload struct {
- Mode string `json:"mode"`
- }
- if err := json.Unmarshal(body, &payload); err != nil {
- return ""
- }
- return payload.Mode
-}
diff --git a/cmd/pinchtab/cmd_config_test.go b/cmd/pinchtab/cmd_config_test.go
index afacd84d..61592ac1 100644
--- a/cmd/pinchtab/cmd_config_test.go
+++ b/cmd/pinchtab/cmd_config_test.go
@@ -521,6 +521,47 @@ func TestConfigSetHintDoesNotNameServerRestartForBridge(t *testing.T) {
}
}
+// A running instance whose token the CLI cannot present answers /health with
+// 401/403. It is still running and still needs a restart to apply the change, so
+// the hint must print — mode-neutral, since the mode is hidden. This pins the
+// regression where switching off CheckPinchTabRunning (any 200) to a strict probe
+// left a protected instance with no hint at all.
+func TestConfigSetHintsRestartForProtectedInstance(t *testing.T) {
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusUnauthorized)
+ }))
+ defer srv.Close()
+
+ port := srv.URL[strings.LastIndex(srv.URL, ":")+1:]
+
+ configPath := filepath.Join(t.TempDir(), "pinchtab", "config.json")
+ t.Setenv("PINCHTAB_CONFIG", configPath)
+ configJSON := []byte(`{"configVersion":"0.8.0","server":{"port":"` + port + `","token":"test-token-for-restart-hint-00000"}}`)
+ if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
+ t.Fatalf("MkdirAll() error = %v", err)
+ }
+ if err := os.WriteFile(configPath, configJSON, 0o600); err != nil {
+ t.Fatalf("WriteFile() error = %v", err)
+ }
+ t.Cleanup(func() { rootCmd.SetArgs(nil) })
+
+ stderr := captureStderr(t, func() {
+ _ = captureStdout(t, func() {
+ rootCmd.SetArgs([]string{"config", "set", "security.allowScreencast", "true"})
+ if err := rootCmd.Execute(); err != nil {
+ t.Fatalf("Execute() error = %v", err)
+ }
+ })
+ })
+
+ if !strings.Contains(stderr, "restart") {
+ t.Fatalf("a protected running instance got no restart hint: %q", stderr)
+ }
+ if strings.Contains(stderr, "pinchtab server restart") {
+ t.Fatalf("a protected instance's mode is unknown, so the hint must stay neutral: %q", stderr)
+ }
+}
+
func TestIsSensitiveConfigPath(t *testing.T) {
cases := map[string]bool{
"server.token": true,
diff --git a/cmd/pinchtab/cmd_security.go b/cmd/pinchtab/cmd_security.go
index 02f822fc..be38c13c 100644
--- a/cmd/pinchtab/cmd_security.go
+++ b/cmd/pinchtab/cmd_security.go
@@ -66,7 +66,7 @@ func printEnforcedDriftWarning(cfg *config.RuntimeConfig) {
if len(snap.RestartReasons) > 0 {
fmt.Printf(" Pending (needs restart): %s\n", strings.Join(snap.RestartReasons, ", "))
}
- fmt.Println(cli.StyleStdout(cli.MutedStyle, " Apply with: pinchtab server restart"))
+ fmt.Println(cli.StyleStdout(cli.MutedStyle, " "+restartHintForMode(snap.Mode)))
fmt.Println()
}
diff --git a/cmd/pinchtab/cmd_server_background.go b/cmd/pinchtab/cmd_server_background.go
index b9c3ba75..5db5e2ff 100644
--- a/cmd/pinchtab/cmd_server_background.go
+++ b/cmd/pinchtab/cmd_server_background.go
@@ -414,7 +414,7 @@ func isPinchTabHealthReady(url, marker string) bool {
if err := json.Unmarshal(body, &health); err != nil {
return false
}
- if !types.HealthStatusServing(health.Status) || health.Mode != "dashboard" || strings.TrimSpace(health.Version) == "" {
+ if !types.HealthStatusServing(health.Status) || health.Mode != types.ModeDashboard || strings.TrimSpace(health.Version) == "" {
return false
}
return marker == "" || health.Marker == marker
diff --git a/cmd/pinchtab/root_health.go b/cmd/pinchtab/root_health.go
index 019a64c0..3b0c8766 100644
--- a/cmd/pinchtab/root_health.go
+++ b/cmd/pinchtab/root_health.go
@@ -57,10 +57,15 @@ func fetchHealthSnapshot(port string) (*healthSnapshot, healthSnapshotState) {
return fetchHealthSnapshotWithToken(port, "")
}
-// fetchHealthSnapshotWithToken is fetchHealthSnapshot with optional auth, so it
-// can read fields like restartRequired from a server that requires auth on
-// /health (the unauthenticated probe would just see a protected listener).
-func fetchHealthSnapshotWithToken(port, token string) (*healthSnapshot, healthSnapshotState) {
+// probeHealthSnapshot probes the localhost listener (with the token, so a
+// protected /health is distinguished from a dead one) and decodes the body for
+// ANY PinchTab mode. It classifies only reachability and auth; it does not judge
+// the mode or serving status. fetchHealthSnapshotWithToken layers the
+// dashboard-serving check on top for the landing banner and `pinchtab health`,
+// while callers that only need the reported mode (the config-set restart hint)
+// read snap.Mode from what this returns — a bridge, which reports no mode, comes
+// back running with an empty Mode rather than being classed invalid.
+func probeHealthSnapshot(port, token string) (*healthSnapshot, healthSnapshotState) {
var headers map[string]string
if auth := server.AuthorizationHeaderValue(token); auth != "" {
headers = map[string]string{"Authorization": auth}
@@ -80,8 +85,20 @@ func fetchHealthSnapshotWithToken(port, token string) (*healthSnapshot, healthSn
if err := json.Unmarshal(body, &snap); err != nil {
return nil, healthSnapshotInvalid
}
- if !types.HealthStatusServing(snap.Status) || snap.Mode != "dashboard" || strings.TrimSpace(snap.Version) == "" {
+ return &snap, healthSnapshotRunning
+}
+
+// fetchHealthSnapshotWithToken is fetchHealthSnapshot with optional auth, so it
+// can read fields like restartRequired from a server that requires auth on
+// /health (the unauthenticated probe would just see a protected listener). It
+// answers "running" only for a serving dashboard front door.
+func fetchHealthSnapshotWithToken(port, token string) (*healthSnapshot, healthSnapshotState) {
+ snap, state := probeHealthSnapshot(port, token)
+ if state != healthSnapshotRunning {
+ return nil, state
+ }
+ if !types.HealthStatusServing(snap.Status) || snap.Mode != types.ModeDashboard || strings.TrimSpace(snap.Version) == "" {
return nil, healthSnapshotInvalid
}
- return &snap, healthSnapshotRunning
+ return snap, healthSnapshotRunning
}
diff --git a/dashboard/src/generated/types.ts b/dashboard/src/generated/types.ts
index 4c32c090..3e57e2f4 100644
--- a/dashboard/src/generated/types.ts
+++ b/dashboard/src/generated/types.ts
@@ -36,6 +36,13 @@ export const HeaderSource = "X-PinchTab-Source";
export const HealthStatusOK = "ok";
export const HealthStatusDegraded = "degraded";
+/**
+ * ModeDashboard is the /health "mode" the front-door dashboard server reports. A
+ * bridge or instance server reports no mode, so a reader comparing against this
+ * const treats those as non-dashboard rather than by accident — and a rename
+ * stays one edit instead of a literal scattered across producers and readers.
+ */
+export const ModeDashboard = "dashboard";
//////////
// source: types.go
diff --git a/docs/endpoints.md b/docs/endpoints.md
index b10f3fa7..6dfe3020 100644
--- a/docs/endpoints.md
+++ b/docs/endpoints.md
@@ -1033,8 +1033,8 @@ different kinds of answer and neither substitutes for the other:
"code": "evaluate_disabled",
"details": {
"setting": "security.allowEvaluate",
- "hint": "Enable security.allowEvaluate to use this feature.",
- "remedy": "pinchtab config set security.allowEvaluate true && pinchtab server restart"
+ "hint": "Enable security.allowEvaluate to use this feature, then restart PinchTab to apply the change.",
+ "remedy": "pinchtab config set security.allowEvaluate true"
}
}
```
diff --git a/internal/api/types/health.go b/internal/api/types/health.go
index cb38e7e7..dc530634 100644
--- a/internal/api/types/health.go
+++ b/internal/api/types/health.go
@@ -5,6 +5,12 @@ const (
HealthStatusDegraded = "degraded"
)
+// ModeDashboard is the /health "mode" the front-door dashboard server reports. A
+// bridge or instance server reports no mode, so a reader comparing against this
+// const treats those as non-dashboard rather than by accident — and a rename
+// stays one edit instead of a literal scattered across producers and readers.
+const ModeDashboard = "dashboard"
+
func HealthStatusServing(status string) bool {
return status == HealthStatusOK || status == HealthStatusDegraded
}
diff --git a/internal/dashboard/config_health.go b/internal/dashboard/config_health.go
index d28ec0cd..346cc7d2 100644
--- a/internal/dashboard/config_health.go
+++ b/internal/dashboard/config_health.go
@@ -120,7 +120,7 @@ func (c *ConfigAPI) healthInfo(includeSecurity bool) (healthEnvelope, error) {
cfg := c.cfg()
out := healthEnvelope{
Status: status,
- Mode: "dashboard",
+ Mode: types.ModeDashboard,
Version: c.version,
Uptime: int64(time.Since(c.startedAt).Milliseconds()),
AuthRequired: cfg != nil && strings.TrimSpace(cfg.Token) != "",
diff --git a/internal/server/server.go b/internal/server/server.go
index dcc3d918..19d5ec6e 100644
--- a/internal/server/server.go
+++ b/internal/server/server.go
@@ -13,6 +13,7 @@ import (
"time"
"github.com/pinchtab/pinchtab/internal/activity"
+ "github.com/pinchtab/pinchtab/internal/api/types"
"github.com/pinchtab/pinchtab/internal/authn"
"github.com/pinchtab/pinchtab/internal/bridge"
_ "github.com/pinchtab/pinchtab/internal/browsers/all"
@@ -286,7 +287,7 @@ func RunDashboard(cfg *config.RuntimeConfig, version string) {
mux.HandleFunc("GET /health/background", func(w http.ResponseWriter, r *http.Request) {
httpx.JSON(w, http.StatusOK, map[string]string{
"status": "ok",
- "mode": "dashboard",
+ "mode": types.ModeDashboard,
"version": version,
"marker": cfg.BackgroundMarker,
})