High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix(cli): recognise a degraded server as a running pinchtab server (PIN-340)
Luigi Agosti7b218a978d4e
diff --git a/cmd/pinchtab/cmd_server_background.go b/cmd/pinchtab/cmd_server_background.goindex 265f5474..b9c3ba75 100644--- a/cmd/pinchtab/cmd_server_background.go+++ b/cmd/pinchtab/cmd_server_background.go@@ -16,6 +16,7 @@ import ("strings""time"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/cli""github.com/pinchtab/pinchtab/internal/config""github.com/pinchtab/pinchtab/internal/daemon"@@ -413,7 +414,7 @@ func isPinchTabHealthReady(url, marker string) bool {if err := json.Unmarshal(body, &health); err != nil {return false}- if health.Status != "ok" || health.Mode != "dashboard" || strings.TrimSpace(health.Version) == "" {+ if !types.HealthStatusServing(health.Status) || health.Mode != "dashboard" || strings.TrimSpace(health.Version) == "" {return false}return marker == "" || health.Marker == markerdiff --git a/cmd/pinchtab/cmd_server_background_test.go b/cmd/pinchtab/cmd_server_background_test.goindex 929c15ec..4aa48558 100644--- a/cmd/pinchtab/cmd_server_background_test.go+++ b/cmd/pinchtab/cmd_server_background_test.go@@ -405,18 +405,22 @@ func TestPortBusyErrorForeignListener(t *testing.T) {}func TestPortBusyErrorReadyPinchTab(t *testing.T) {- srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {- w.Header().Set("Content-Type", "application/json")- _, _ = w.Write([]byte(`{"status":"ok","mode":"dashboard","version":"dev"}`))- }))- defer srv.Close()+ for _, status := range []string{"ok", "degraded"} {+ t.Run(status, func(t *testing.T) {+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {+ w.Header().Set("Content-Type", "application/json")+ _, _ = w.Write([]byte(`{"status":"` + status + `","mode":"dashboard","version":"dev"}`))+ }))+ defer srv.Close()- err := portBusyError(srv.URL, "/tmp/config.json")- if err == nil {- t.Fatal("expected an error for a busy port")- }- if !strings.Contains(err.Error(), "server already running") || !strings.Contains(err.Error(), "pinchtab server stop") {- t.Errorf("ready-server message lacks the stop command:\n%s", err)+ err := portBusyError(srv.URL, "/tmp/config.json")+ if err == nil {+ t.Fatal("expected an error for a busy port")+ }+ if !strings.Contains(err.Error(), "server already running") || !strings.Contains(err.Error(), "pinchtab server stop") {+ t.Errorf("ready-server message lacks the stop command:\n%s", err)+ }+ })}}diff --git a/cmd/pinchtab/root_health.go b/cmd/pinchtab/root_health.goindex c3c6a9ab..019a64c0 100644--- a/cmd/pinchtab/root_health.go+++ b/cmd/pinchtab/root_health.go@@ -7,6 +7,7 @@ import ("strings""time"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/server")@@ -79,7 +80,7 @@ func fetchHealthSnapshotWithToken(port, token string) (*healthSnapshot, healthSnif err := json.Unmarshal(body, &snap); err != nil {return nil, healthSnapshotInvalid}- if snap.Status != "ok" || snap.Mode != "dashboard" || strings.TrimSpace(snap.Version) == "" {+ if !types.HealthStatusServing(snap.Status) || snap.Mode != "dashboard" || strings.TrimSpace(snap.Version) == "" {return nil, healthSnapshotInvalid}return &snap, healthSnapshotRunningdiff --git a/cmd/pinchtab/root_test.go b/cmd/pinchtab/root_test.goindex 8e441344..17c39cbb 100644--- a/cmd/pinchtab/root_test.go+++ b/cmd/pinchtab/root_test.go@@ -29,6 +29,18 @@ func TestFetchHealthSnapshotClassifiesOnlyValidDashboardHealthAsRunning(t *testibody: `{"status":"ok","mode":"dashboard","version":"dev"}`,want: healthSnapshotRunning,},+ {+ name: "degraded dashboard health",+ status: http.StatusOK,+ body: `{"status":"degraded","mode":"dashboard","version":"dev","unresponsiveInstances":["inst_1"]}`,+ want: healthSnapshotRunning,+ },+ {+ name: "unknown dashboard status",+ status: http.StatusOK,+ body: `{"status":"draining","mode":"dashboard","version":"dev"}`,+ want: healthSnapshotInvalid,+ },{name: "protected listener",status: http.StatusUnauthorized,diff --git a/dashboard/src/generated/types.ts b/dashboard/src/generated/types.tsindex 274177a3..59fe1b56 100644--- a/dashboard/src/generated/types.ts+++ b/dashboard/src/generated/types.ts@@ -1,5 +1,11 @@// Code generated by tygo. DO NOT EDIT.+//////////+// source: health.go++export const HealthStatusOK = "ok";+export const HealthStatusDegraded = "degraded";+//////////// source: types.go/*diff --git a/internal/api/types/health.go b/internal/api/types/health.gonew file mode 100644index 00000000..cb38e7e7--- /dev/null+++ b/internal/api/types/health.go@@ -0,0 +1,10 @@+package types++const (+ HealthStatusOK = "ok"+ HealthStatusDegraded = "degraded"+)++func HealthStatusServing(status string) bool {+ return status == HealthStatusOK || status == HealthStatusDegraded+}diff --git a/internal/dashboard/config_health.go b/internal/dashboard/config_health.goindex 54797df2..d28ec0cd 100644--- a/internal/dashboard/config_health.go+++ b/internal/dashboard/config_health.go@@ -5,6 +5,7 @@ import ("strings""time"+ "github.com/pinchtab/pinchtab/internal/api/types""github.com/pinchtab/pinchtab/internal/authn""github.com/pinchtab/pinchtab/internal/bridge""github.com/pinchtab/pinchtab/internal/cli/report"@@ -45,11 +46,6 @@ type healthEnvelope struct {UnresponsiveInstances []string `json:"unresponsiveInstances,omitempty"`}-const (- healthStatusOK = "ok"- healthStatusDegraded = "degraded"-)-func unresponsiveInstanceIDs(instances []bridge.Instance) []string {var ids []stringfor _, inst := range instances {@@ -113,9 +109,9 @@ func (c *ConfigAPI) healthInfo(includeSecurity bool) (healthEnvelope, error) {}}}- status := healthStatusOK+ status := types.HealthStatusOKif len(unresponsive) > 0 {- status = healthStatusDegraded+ status = types.HealthStatusDegraded}agentCount := 0if c.agents != nil {diff --git a/npm/src/index.ts b/npm/src/index.tsindex 11b430f9..63081975 100644--- a/npm/src/index.ts+++ b/npm/src/index.ts@@ -19,13 +19,13 @@ import {export * from './types';export * from './platform';-/**- * Returns true only for an actual PinchTab `/health` ready body (`{ status: "ok" }`),- * so an unrelated process listening on the port cannot satisfy startup.- */+const servingHealthStatuses: readonly unknown[] = ['ok', 'degraded'];+export function isPinchtabHealthy(body: unknown): boolean {return (- typeof body === 'object' && body !== null && (body as { status?: unknown }).status === 'ok'+ typeof body === 'object' &&+ body !== null &&+ servingHealthStatuses.includes((body as { status?: unknown }).status));}diff --git a/npm/tests/lifecycle.test.ts b/npm/tests/lifecycle.test.tsindex 029b00bd..2df4c11d 100644--- a/npm/tests/lifecycle.test.ts+++ b/npm/tests/lifecycle.test.ts@@ -18,6 +18,13 @@ describe('isPinchtabHealthy', () => {assert.strictEqual(isPinchtabHealthy({ status: 'ok', tabs: 3 }), true);});+ test('accepts a serving server that reports an unresponsive instance', () => {+ assert.strictEqual(+ isPinchtabHealthy({ status: 'degraded', unresponsiveInstances: ['inst_1'] }),+ true+ );+ });+test('rejects non-ready / non-PinchTab bodies', () => {assert.strictEqual(isPinchtabHealthy({ status: 'error' }), false);assert.strictEqual(isPinchtabHealthy({ status: 'draining' }), false);