Skip to content

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

fix(health): /health defaultInstance is instances[0] of a map-ordered Orchestrator.List, so with two or more instances it names a different instance between calls (PIN-432)

Luigi Agosti4ada2c6b0fbf
diff --git a/docs/reference/health.md b/docs/reference/health.md
index 8751cda7..886ccf7b 100644
--- a/docs/reference/health.md
+++ b/docs/reference/health.md
@@ -63,7 +63,7 @@ curl http://localhost:9867/health
| `authRequired` | `true` when a server token is configured |
| `profiles` | Number of configured profiles |
| `instances` | Number of managed instances |
-| `defaultInstance` | First managed instance info, when present: `id`, `status` and `responsiveness` |
+| `defaultInstance` | The instance shorthand (non-`/instances`) routes use, when present: `id`, `status` and `responsiveness`. With no running routable instance it is the earliest-started one |
| `agents` | Connected agent count |
| `restartRequired` | `true` when file-based config changes need restart |
| `restartReasons` | Restart reason list when required |
diff --git a/internal/dashboard/config_health.go b/internal/dashboard/config_health.go
index f071cd4a..4f010708 100644
--- a/internal/dashboard/config_health.go
+++ b/internal/dashboard/config_health.go
@@ -60,6 +60,28 @@ type crashReporter interface {
CrashSummary() bridge.CrashSummary
}
+type defaultInstancer interface {
+ DefaultInstance() (bridge.Instance, bool)
+}
+
+func defaultInstanceInfo(lister InstanceLister, instances []bridge.Instance) *healthInstanceInfo {
+ def, ok := bridge.Instance{}, false
+ if d, isDefaulter := lister.(defaultInstancer); isDefaulter {
+ def, ok = d.DefaultInstance()
+ }
+ if !ok {
+ if len(instances) == 0 {
+ return nil
+ }
+ def = instances[0]
+ }
+ return &healthInstanceInfo{
+ ID: def.ID,
+ Status: def.Status,
+ Responsiveness: def.Responsiveness,
+ }
+}
+
func (c *ConfigAPI) healthInfo(includeSecurity bool) (healthEnvelope, error) {
_, _, restartReasons, err := c.currentConfig()
if err != nil {
@@ -101,13 +123,7 @@ func (c *ConfigAPI) healthInfo(includeSecurity bool) (healthEnvelope, error) {
instances := c.instances.List()
instanceCount = len(instances)
unresponsive = unresponsiveInstanceIDs(instances)
- if len(instances) > 0 {
- defaultInst = &healthInstanceInfo{
- ID: instances[0].ID,
- Status: instances[0].Status,
- Responsiveness: instances[0].Responsiveness,
- }
- }
+ defaultInst = defaultInstanceInfo(c.instances, instances)
}
status := types.HealthStatusOK
if len(unresponsive) > 0 {
diff --git a/internal/dashboard/config_health_test.go b/internal/dashboard/config_health_test.go
index 17745af7..4274f97f 100644
--- a/internal/dashboard/config_health_test.go
+++ b/internal/dashboard/config_health_test.go
@@ -9,6 +9,7 @@ import (
"testing"
"time"
+ "github.com/pinchtab/pinchtab/internal/bridge"
"github.com/pinchtab/pinchtab/internal/config"
"github.com/pinchtab/pinchtab/internal/profiles"
)
@@ -87,3 +88,43 @@ func TestHealthCountsEachProfileInExactlyOneBucketThatReconcilesWithTheListing(t
}
}
}
+
+type routedInstances struct {
+ instances []bridge.Instance
+ def bridge.Instance
+ routable bool
+}
+
+func (s routedInstances) List() []bridge.Instance { return s.instances }
+
+func (s routedInstances) DefaultInstance() (bridge.Instance, bool) { return s.def, s.routable }
+
+func TestHealthDefaultInstanceNamesTheRoutedInstanceRatherThanTheFirstListed(t *testing.T) {
+ listed := []bridge.Instance{
+ {ID: "inst_first", Status: "stopped"},
+ {ID: "inst_routed", Status: "running", Responsiveness: bridge.ResponsivenessResponsive},
+ }
+ lister := routedInstances{instances: listed, def: listed[1], routable: true}
+ for i := 0; i < 10; i++ {
+ def, _ := healthBody(t, lister)["defaultInstance"].(map[string]any)
+ if def["id"] != "inst_routed" || def["status"] != "running" || def["responsiveness"] != bridge.ResponsivenessResponsive {
+ t.Fatalf("call %d: defaultInstance = %v, want the routed inst_routed", i, def)
+ }
+ }
+}
+
+func TestHealthDefaultInstanceFallsBackToTheFirstListedWithoutARoutedInstance(t *testing.T) {
+ listed := []bridge.Instance{{ID: "inst_starting", Status: "starting"}, {ID: "inst_other", Status: "starting"}}
+ for name, lister := range map[string]InstanceLister{
+ "no routable instance": routedInstances{instances: listed},
+ "lister without routing": plainInstances{instances: listed},
+ } {
+ def, _ := healthBody(t, lister)["defaultInstance"].(map[string]any)
+ if def["id"] != "inst_starting" || def["status"] != "starting" {
+ t.Errorf("%s: defaultInstance = %v, want the first listed inst_starting", name, def)
+ }
+ }
+ if _, present := healthBody(t, routedInstances{})["defaultInstance"]; present {
+ t.Error("defaultInstance present with no instances")
+ }
+}
diff --git a/internal/orchestrator/instance_query.go b/internal/orchestrator/instance_query.go
index 9074666a..9417c974 100644
--- a/internal/orchestrator/instance_query.go
+++ b/internal/orchestrator/instance_query.go
@@ -35,18 +35,42 @@ func (o *Orchestrator) List() []bridge.Instance {
result := make([]bridge.Instance, 0, len(o.instances))
for _, inst := range o.instances {
- copyInst := inst.Instance
- copyInst.Status = effectiveInstanceStatus(copyInst.Status, instanceIsActive(inst))
- copyInst.Responsiveness = bridge.NormalizeResponsiveness(copyInst.Responsiveness)
- if crashes, ok := o.crashes[inst.ID]; ok && crashes.Total > 0 {
- summary := crashes
- copyInst.Crashes = &summary
- }
- result = append(result, copyInst)
+ result = append(result, o.instanceViewLocked(inst))
}
+ sort.Slice(result, func(i, j int) bool {
+ if !result[i].StartTime.Equal(result[j].StartTime) {
+ return result[i].StartTime.Before(result[j].StartTime)
+ }
+ return result[i].ID < result[j].ID
+ })
return result
}
+func (o *Orchestrator) instanceViewLocked(inst *InstanceInternal) bridge.Instance {
+ view := inst.Instance
+ view.Status = effectiveInstanceStatus(view.Status, instanceIsActive(inst))
+ view.Responsiveness = bridge.NormalizeResponsiveness(view.Responsiveness)
+ if crashes, ok := o.crashes[inst.ID]; ok && crashes.Total > 0 {
+ summary := crashes
+ view.Crashes = &summary
+ }
+ return view
+}
+
+func (o *Orchestrator) DefaultInstance() (bridge.Instance, bool) {
+ match, _, err := o.defaultRouteMatch()
+ if err != nil {
+ return bridge.Instance{}, false
+ }
+ o.mu.RLock()
+ defer o.mu.RUnlock()
+ inst := o.firstRunningLocked(match)
+ if inst == nil {
+ return bridge.Instance{}, false
+ }
+ return o.instanceViewLocked(inst), true
+}
+
type instanceProbe struct {
crashes *bridge.CrashSummary
responsiveness string
@@ -196,65 +220,80 @@ func (o *Orchestrator) FirstRunningURL() string {
}
func (o *Orchestrator) FirstRunningURLForBrowser(browser string) string {
+ return o.firstRunningURL(browserMatcher(browser))
+}
+
+// browserMatcher has no legacy empty-Browser fallback: a request for a
+// SPECIFIC browser must not be routed to an instance of unknown provenance.
+// Browserless legacy instances stay reachable via FirstRunningURL.
+func browserMatcher(browser string) func(*InstanceInternal) bool {
browser = strings.TrimSpace(browser)
if browser == "" {
- return o.FirstRunningURL()
+ return nil
}
normalized := config.NormalizeBrowser(browser)
- // No legacy empty-Browser fallback here: a request for a SPECIFIC
- // browser must not be routed to an instance of unknown provenance.
- // Browserless legacy instances stay reachable via FirstRunningURL.
- return o.firstRunningURL(func(inst *InstanceInternal) bool {
+ return func(inst *InstanceInternal) bool {
return inst != nil && inst.Browser == normalized
- })
+ }
}
func (o *Orchestrator) firstRunningURL(match func(*InstanceInternal) bool) string {
o.mu.RLock()
defer o.mu.RUnlock()
- // Collect running instances and sort by start time for determinism.
- type candidate struct {
- start time.Time
- url string
+ if inst := o.firstRunningLocked(match); inst != nil {
+ return inst.URL
}
- var candidates []candidate
+ return ""
+}
+
+func (o *Orchestrator) firstRunningLocked(match func(*InstanceInternal) bool) *InstanceInternal {
+ var first *InstanceInternal
for _, inst := range o.instances {
- if inst.Status == "running" && instanceIsActive(inst) {
- if inst.URL == "" {
- continue
- }
- if match != nil && !match(inst) {
- continue
- }
- candidates = append(candidates, candidate{start: inst.StartTime, url: inst.URL})
+ if inst.Status != "running" || !instanceIsActive(inst) || inst.URL == "" {
+ continue
+ }
+ if match != nil && !match(inst) {
+ continue
+ }
+ if first == nil || startsBefore(inst, first) {
+ first = inst
}
}
- if len(candidates) == 0 {
- return ""
+ return first
+}
+
+func startsBefore(a, b *InstanceInternal) bool {
+ if !a.StartTime.Equal(b.StartTime) {
+ return a.StartTime.Before(b.StartTime)
+ }
+ if a.URL != b.URL {
+ return a.URL < b.URL
+ }
+ return a.ID < b.ID
+}
+
+func (o *Orchestrator) defaultRouteMatch() (func(*InstanceInternal) bool, int, error) {
+ resolved, err := config.ResolveDefaultBrowserTarget(o.cfg())
+ if err != nil {
+ return nil, http.StatusBadRequest, err
}
- sort.Slice(candidates, func(i, j int) bool {
- if candidates[i].start.Equal(candidates[j].start) {
- return candidates[i].url < candidates[j].url
+ if resolved != nil && !resolved.Legacy {
+ if resolved.Provider == "" {
+ return nil, http.StatusBadRequest, fmt.Errorf("no default browser target configured and none requested")
}
- return candidates[i].start.Before(candidates[j].start)
- })
- return candidates[0].url
+ return browserMatcher(resolved.Provider), 0, nil
+ }
+ return nil, 0, nil
}
func (o *Orchestrator) FirstRunningURLForRequest(r *http.Request) (string, int, error) {
requested := ExtractRequestedBrowser(r)
if requested == "" {
- resolved, err := config.ResolveDefaultBrowserTarget(o.cfg())
+ match, status, err := o.defaultRouteMatch()
if err != nil {
- return "", http.StatusBadRequest, err
- }
- if resolved != nil && !resolved.Legacy {
- if resolved.Provider == "" {
- return "", http.StatusBadRequest, fmt.Errorf("no default browser target configured and none requested")
- }
- return o.FirstRunningURLForBrowser(resolved.Provider), 0, nil
+ return "", status, err
}
- return o.FirstRunningURL(), 0, nil
+ return o.firstRunningURL(match), 0, nil
}
if _, err := config.ParseBrowser(requested, nil); err != nil {
diff --git a/internal/orchestrator/instance_query_test.go b/internal/orchestrator/instance_query_test.go
index 37ec25d0..1aa7e855 100644
--- a/internal/orchestrator/instance_query_test.go
+++ b/internal/orchestrator/instance_query_test.go
@@ -1,6 +1,19 @@
package orchestrator
-import "testing"
+import (
+ "encoding/json"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "slices"
+ "strings"
+ "testing"
+ "time"
+
+ "github.com/pinchtab/pinchtab/internal/bridge"
+ "github.com/pinchtab/pinchtab/internal/config"
+ "github.com/pinchtab/pinchtab/internal/dashboard"
+)
func TestEffectiveInstanceStatus(t *testing.T) {
cases := []struct {
@@ -24,3 +37,147 @@ func TestEffectiveInstanceStatus(t *testing.T) {
}
}
}
+
+func registerInstance(o *Orchestrator, id, status, browser string, start time.Time) {
+ url := "http://" + id + ".local"
+ inst := &InstanceInternal{
+ Instance: bridge.Instance{ID: id, Status: status, URL: url, Browser: browser, StartTime: start},
+ URL: url,
+ }
+ if status != "stopped" {
+ inst.cmd = &mockCmd{pid: 1, isAlive: true}
+ }
+ o.instances[id] = inst
+}
+
+func listIDs(o *Orchestrator) []string {
+ var ids []string
+ for _, inst := range o.List() {
+ ids = append(ids, inst.ID)
+ }
+ return ids
+}
+
+func okClient() *http.Client {
+ return &http.Client{Transport: roundTripFunc(func(req *http.Request) (*http.Response, error) {
+ return &http.Response{
+ StatusCode: http.StatusOK,
+ Body: io.NopCloser(strings.NewReader(`{"status":"ok"}`)),
+ Header: make(http.Header),
+ Request: req,
+ }, nil
+ })}
+}
+
+func healthDefaultInstanceID(t *testing.T, o *Orchestrator) string {
+ t.Helper()
+ api := dashboard.NewConfigAPI(o.LiveConfig(), o, nil, nil, nil, "test", time.Now())
+ w := httptest.NewRecorder()
+ api.HandleHealth(w, httptest.NewRequest(http.MethodGet, "/health", nil))
+ if w.Code != http.StatusOK {
+ t.Fatalf("GET /health = %d: %s", w.Code, w.Body.String())
+ }
+ var body struct {
+ DefaultInstance *struct {
+ ID string `json:"id"`
+ } `json:"defaultInstance"`
+ }
+ if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
+ t.Fatalf("decode /health: %v", err)
+ }
+ if body.DefaultInstance == nil {
+ t.Fatalf("/health omitted defaultInstance: %s", w.Body.String())
+ }
+ return body.DefaultInstance.ID
+}
+
+func shorthandRouteInstanceID(t *testing.T, o *Orchestrator) string {
+ t.Helper()
+ target, status, err := o.RouteForRequest(httptest.NewRequest(http.MethodGet, "/text", nil))
+ if err != nil {
+ t.Fatalf("RouteForRequest status=%d err=%v", status, err)
+ }
+ for _, inst := range o.List() {
+ if inst.URL == target {
+ return inst.ID
+ }
+ }
+ t.Fatalf("shorthand route target %q matches no instance", target)
+ return ""
+}
+
+func TestListOrdersInstancesByStartTimeThenIDWhateverTheMapOrder(t *testing.T) {
+ alwaysAlive(t)
+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})
+ base := time.Now()
+ registerInstance(o, "inst_z", "running", "", base)
+ registerInstance(o, "inst_m", "running", "", base.Add(2*time.Second))
+ registerInstance(o, "inst_b", "stopped", "", base)
+ registerInstance(o, "inst_y", "running", "", base.Add(time.Second))
+ registerInstance(o, "inst_a", "running", "", base.Add(2*time.Second))
+ registerInstance(o, "inst_c", "running", "", base.Add(-time.Second))
+
+ want := []string{"inst_c", "inst_b", "inst_z", "inst_y", "inst_a", "inst_m"}
+ for i := 0; i < 50; i++ {
+ if got := listIDs(o); !slices.Equal(got, want) {
+ t.Fatalf("call %d: List() order = %v, want %v", i, got, want)
+ }
+ }
+}
+
+func TestHealthDefaultInstanceIsTheInstanceShorthandRoutesUse(t *testing.T) {
+ alwaysAlive(t)
+ cases := []struct {
+ name string
+ cfg *config.RuntimeConfig
+ want string
+ }{
+ {name: "earliest running instance without a default target", cfg: &config.RuntimeConfig{}, want: "inst_z"},
+ {name: "default target browser even when another started first", cfg: &config.RuntimeConfig{
+ DefaultTarget: "cloak",
+ Targets: config.BrowserTargetsConfig{
+ "chrome": {Provider: config.BrowserChrome},
+ "cloak": {Provider: config.BrowserCloak},
+ },
+ }, want: "inst_a"},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})
+ o.ApplyRuntimeConfig(tc.cfg)
+ o.client = okClient()
+ base := time.Now()
+ registerInstance(o, "inst_0", "stopped", config.BrowserChrome, base.Add(-time.Minute))
+ registerInstance(o, "inst_z", "running", config.BrowserChrome, base)
+ registerInstance(o, "inst_a", "running", config.BrowserCloak, base.Add(time.Second))
+
+ routed := shorthandRouteInstanceID(t, o)
+ if routed != tc.want {
+ t.Fatalf("shorthand route instance = %q, want %q", routed, tc.want)
+ }
+ for i := 0; i < 20; i++ {
+ if got := healthDefaultInstanceID(t, o); got != routed {
+ t.Fatalf("call %d: /health defaultInstance = %q, want the shorthand route instance %q", i, got, routed)
+ }
+ }
+ })
+ }
+}
+
+func TestHealthDefaultInstanceFallsBackToTheFirstListedWhenNoneIsRoutable(t *testing.T) {
+ alwaysAlive(t)
+ o := NewOrchestratorWithRunner(t.TempDir(), &mockRunner{portAvail: true})
+ o.client = okClient()
+ base := time.Now()
+ registerInstance(o, "inst_z", "starting", "", base)
+ registerInstance(o, "inst_a", "starting", "", base.Add(time.Second))
+
+ if _, ok := o.DefaultInstance(); ok {
+ t.Fatal("DefaultInstance reported a routable instance while every instance is still starting")
+ }
+ for i := 0; i < 20; i++ {
+ if got := healthDefaultInstanceID(t, o); got != "inst_z" {
+ t.Fatalf("call %d: /health defaultInstance = %q, want the earliest-started inst_z", i, got)
+ }
+ }
+}
diff --git a/tests/e2e/scenarios/infra/orchestrator-extended.sh b/tests/e2e/scenarios/infra/orchestrator-extended.sh
index cfd8ee7b..23ee9e33 100644
--- a/tests/e2e/scenarios/infra/orchestrator-extended.sh
+++ b/tests/e2e/scenarios/infra/orchestrator-extended.sh
@@ -53,6 +53,34 @@ else
((ASSERTIONS_FAILED++)) || true
fi
+DEFAULT_IDS=""
+for _ in 1 2 3 4 5 6 7 8; do
+ pt_get /health
+ DEFAULT_IDS="${DEFAULT_IDS}$(echo "$RESULT" | jq -r '.defaultInstance.id // empty')"$'\n'
+done
+DEFAULT_ID=$(printf '%s' "$DEFAULT_IDS" | head -1)
+if [ -n "$DEFAULT_ID" ] && [ "$(printf '%s' "$DEFAULT_IDS" | sort -u | wc -l | tr -d ' ')" = "1" ]; then
+ pass_assert "health defaultInstance stays ${DEFAULT_ID} across calls with two instances"
+else
+ fail_assert "health defaultInstance changed between calls: $(printf '%s' "$DEFAULT_IDS" | tr '\n' ' ')"
+fi
+if [ "$DEFAULT_ID" != "$ATTACHED_INST_ID" ]; then
+ pass_assert "health defaultInstance is the earlier instance, not the attached bridge"
+else
+ fail_assert "health defaultInstance named the later-attached bridge ${ATTACHED_INST_ID}"
+fi
+
+pt_post /navigate "{\"url\":\"${FIXTURES_URL}/index.html\"}"
+assert_ok "shorthand navigate"
+assert_tab_id "shorthand navigate returned tabId"
+pt_get /instances/tabs
+SHORTHAND_INST_ID=$(echo "$RESULT" | jq -r --arg tab "$TAB_ID" '[.[] | select(.id == $tab or .tabId == $tab)][0].instanceId // empty')
+if [ -n "$SHORTHAND_INST_ID" ] && [ "$SHORTHAND_INST_ID" = "$DEFAULT_ID" ]; then
+ pass_assert "health defaultInstance is the instance shorthand routes use"
+else
+ fail_assert "shorthand route used '${SHORTHAND_INST_ID}' but health defaultInstance is '${DEFAULT_ID}'"
+fi
+
pt_post "/instances/${ATTACHED_INST_ID}/stop" '{}'
assert_ok "stop attached bridge instance"