Skip to content

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

12.5 KB
Raw Blame
package orchestrator

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"sync"
	"time"

	"github.com/pinchtab/pinchtab/internal/api/types"
	"github.com/pinchtab/pinchtab/internal/bridge"
	"github.com/pinchtab/pinchtab/internal/config"
)

// effectiveInstanceStatus reconciles a stored instance Status with whether the
// instance is actually active, so a stale "stopped" on a live instance reads as
// "running" and a non-live "starting/running/stopping" reads as "stopped".
func effectiveInstanceStatus(status string, active bool) string {
	if active && status == "stopped" {
		return "running"
	}
	if !active &&
		(status == "starting" || status == "running" || status == "stopping") {
		return "stopped"
	}
	return status
}

func (o *Orchestrator) List() []bridge.Instance {
	o.mu.RLock()
	defer o.mu.RUnlock()

	result := make([]bridge.Instance, 0, len(o.instances))
	for _, inst := range o.instances {
		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
}

func (o *Orchestrator) probeInstance(inst *InstanceInternal) instanceProbe {
	ctx, cancel := context.WithTimeout(context.Background(), responsivenessProbeBudget)
	defer cancel()
	var tabsErr error
	done := make(chan struct{})
	go func() {
		tabsErr = o.probeTabs(ctx, inst)
		close(done)
	}()
	crashes, healthErr := o.fetchCrashes(ctx, inst)
	<-done
	if crashes != nil {
		for i := range crashes.Recent {
			crashes.Recent[i].InstanceID = inst.ID
		}
	}
	return instanceProbe{crashes: crashes, responsiveness: classifyResponsiveness(healthErr, tabsErr)}
}

var refreshJoinWait = 250 * time.Millisecond

// RefreshCrashes asks every live instance for its crash record and probes its
// tabs route, so List can carry both and CrashSummary can merge the crashes.
// Browser crashes are recorded by the process that owns the browser, which in
// server mode is never this one.
func (o *Orchestrator) RefreshCrashes() {
	timer := time.NewTimer(refreshJoinWait)
	defer timer.Stop()
	select {
	case <-o.startRefresh():
	case <-timer.C:
	}
}

func (o *Orchestrator) startRefresh() <-chan struct{} {
	o.refreshMu.Lock()
	defer o.refreshMu.Unlock()
	if o.refreshDone != nil {
		return o.refreshDone
	}
	done := make(chan struct{})
	o.refreshDone = done
	go func() {
		o.refreshInstances()
		o.refreshMu.Lock()
		o.refreshDone = nil
		o.refreshMu.Unlock()
		close(done)
	}()
	return done
}

func (o *Orchestrator) refreshInstances() {
	o.mu.RLock()
	instances := make([]*InstanceInternal, 0, len(o.instances))
	for _, inst := range o.instances {
		if inst.Status == "running" && instanceIsActive(inst) {
			instances = append(instances, inst)
		}
	}
	o.mu.RUnlock()

	probes := make([]instanceProbe, len(instances))
	var wg sync.WaitGroup
	for i, inst := range instances {
		wg.Add(1)
		go func(i int, inst *InstanceInternal) {
			defer wg.Done()
			probes[i] = o.probeInstance(inst)
		}(i, inst)
	}
	wg.Wait()

	o.mu.Lock()
	if o.crashes == nil {
		o.crashes = map[string]bridge.CrashSummary{}
	}
	for _, inst := range o.instances {
		inst.Responsiveness = bridge.ResponsivenessUnknown
	}
	for i, inst := range instances {
		inst.Responsiveness = probes[i].responsiveness
		if probes[i].crashes == nil {
			continue
		}
		o.crashes[inst.ID] = *probes[i].crashes
	}
	o.mu.Unlock()
}

// CrashSummary merges the instances' crash records into the shape bridge /health
// carries, each event naming its instance.
func (o *Orchestrator) CrashSummary() bridge.CrashSummary {
	o.RefreshCrashes()
	o.mu.RLock()
	defer o.mu.RUnlock()
	var merged bridge.CrashSummary
	for _, crashes := range o.crashes {
		merged.Total += crashes.Total
		merged.Recent = append(merged.Recent, crashes.Recent...)
	}
	sort.SliceStable(merged.Recent, func(i, j int) bool { return merged.Recent[i].Time.Before(merged.Recent[j].Time) })
	if len(merged.Recent) > maxMergedCrashEvents {
		merged.Recent = merged.Recent[len(merged.Recent)-maxMergedCrashEvents:]
	}
	return merged
}

const maxMergedCrashEvents = 20

func (o *Orchestrator) Logs(id string) (string, error) {
	o.mu.RLock()
	defer o.mu.RUnlock()

	inst, ok := o.instances[id]
	if !ok {
		return "", fmt.Errorf("instance %q not found", id)
	}
	if inst.logBuf == nil {
		return "", nil
	}
	return inst.logBuf.String(), nil
}

func (o *Orchestrator) LogsSince(id string, offset uint64) (chunk string, newOffset uint64, reset bool, err error) {
	o.mu.RLock()
	defer o.mu.RUnlock()

	inst, ok := o.instances[id]
	if !ok {
		return "", 0, false, fmt.Errorf("instance %q not found", id)
	}
	if inst.logBuf == nil {
		return "", 0, false, nil
	}
	c, n, rs := inst.logBuf.since(offset)
	return c, n, rs, nil
}

func (o *Orchestrator) FirstRunningURL() string {
	return o.firstRunningURL(nil)
}

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 nil
	}
	normalized := config.NormalizeBrowser(browser)
	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()
	if inst := o.firstRunningLocked(match); inst != nil {
		return inst.URL
	}
	return ""
}

func (o *Orchestrator) firstRunningLocked(match func(*InstanceInternal) bool) *InstanceInternal {
	var first *InstanceInternal
	for _, inst := range o.instances {
		if inst.Status != "running" || !instanceIsActive(inst) || inst.URL == "" {
			continue
		}
		if match != nil && !match(inst) {
			continue
		}
		if first == nil || startsBefore(inst, first) {
			first = inst
		}
	}
	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
	}
	if resolved != nil && !resolved.Legacy {
		if resolved.Provider == "" {
			return nil, http.StatusBadRequest, fmt.Errorf("no default browser target configured and none requested")
		}
		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 == "" {
		match, status, err := o.defaultRouteMatch()
		if err != nil {
			return "", status, err
		}
		return o.firstRunningURL(match), 0, nil
	}

	if _, err := config.ParseBrowser(requested, nil); err != nil {
		return "", http.StatusBadRequest, fmt.Errorf("unknown browser %q", requested)
	}

	normalized := config.NormalizeBrowser(requested)
	if cfg := o.cfg(); cfg != nil && len(cfg.Targets) > 0 {
		matches := config.TargetsForBrowser(cfg, requested)
		if len(matches) == 0 {
			return "", http.StatusBadRequest, fmt.Errorf("no browser target configured for browser %q", requested)
		}
		u := o.FirstRunningURLForBrowser(normalized)
		if u == "" {
			return "", http.StatusConflict, fmt.Errorf("no running instance for browser %q", requested)
		}
		return u, 0, nil
	}

	if u := o.FirstRunningURLForBrowser(normalized); u != "" {
		return u, 0, nil
	}
	// The requested browser has no running instance. If a single instance with a
	// different browser is running, this browser-pinned server can't serve the
	// request: return a clear conflict instead of an empty result the caller
	// would poll on until a misleading "instance not ready" timeout. On-demand
	// cross-browser launch is the multi-instance (simple) strategy's job; a
	// single always-on server serves one browser.
	if only := o.singleRunningInstance(); only != nil && only.Browser != "" &&
		config.NormalizeBrowser(only.Browser) != normalized {
		return "", http.StatusConflict, fmt.Errorf(
			"browser %q requested but %q is already running; restart the server with --browser %s or launch an instance with that browser",
			requested, only.Browser, requested)
	}
	return "", 0, nil
}

func (o *Orchestrator) instanceTabsCached(inst *InstanceInternal, fresh bool) ([]bridge.InstanceTab, error) {
	if inst == nil {
		return nil, fmt.Errorf("nil instance")
	}
	if !fresh {
		if cached, ok := o.tabsCache.Get(inst.ID); ok {
			return cached, nil
		}
	}
	tabs, err := o.fetchTabs(inst)
	if err != nil {
		return nil, err
	}
	out := make([]bridge.InstanceTab, 0, len(tabs))
	for _, tab := range tabs {
		out = append(out, bridge.InstanceTab{
			ID:         tab.ID,
			InstanceID: inst.ID,
			URL:        tab.URL,
			Title:      tab.Title,
		})
	}
	o.tabsCache.Set(inst.ID, out)
	return out, nil
}

func (o *Orchestrator) AllTabs() []bridge.InstanceTab {
	return o.allTabs(false)
}

func (o *Orchestrator) allTabs(fresh bool) []bridge.InstanceTab {
	o.mu.RLock()
	instances := make([]*InstanceInternal, 0)
	for _, inst := range o.instances {
		if inst.Status == "running" && instanceIsActive(inst) {
			instances = append(instances, inst)
		}
	}
	o.mu.RUnlock()

	all := make([]bridge.InstanceTab, 0)
	for _, inst := range instances {
		tabs, err := o.instanceTabsCached(inst, fresh)
		if err != nil {
			continue
		}
		all = append(all, tabs...)
	}
	return all
}

func (o *Orchestrator) FindInstanceByTab(tabID string) (*bridge.Instance, bool) {
	if tabID == "" {
		return nil, false
	}

	o.mu.RLock()
	instances := make([]*InstanceInternal, 0, len(o.instances))
	for _, inst := range o.instances {
		if inst.Status == "running" && instanceIsActive(inst) {
			instances = append(instances, inst)
		}
	}
	o.mu.RUnlock()

	for _, inst := range instances {
		tabs, err := o.instanceTabsCached(inst, false)
		if err != nil {
			continue
		}
		for _, tab := range tabs {
			if tab.ID == tabID {
				copyInst := inst.Instance
				return &copyInst, true
			}
		}
	}
	return nil, false
}

func (o *Orchestrator) AllMetrics() []types.InstanceMetrics {
	o.mu.RLock()
	instances := make([]*InstanceInternal, 0)
	for _, inst := range o.instances {
		if inst.Status == "running" && instanceIsActive(inst) {
			instances = append(instances, inst)
		}
	}
	o.mu.RUnlock()

	all := make([]types.InstanceMetrics, 0)
	for _, inst := range instances {
		mem, err := o.fetchMetrics(inst)
		if err != nil || mem == nil {
			continue
		}
		all = append(all, types.InstanceMetrics{
			InstanceID:        inst.ID,
			ProfileName:       inst.ProfileName,
			MemoryMB:          mem.MemoryMB,
			Renderers:         mem.Renderers,
			Page:              pageMetricsDTO(mem.Page),
			UnreadableTargets: mem.UnreadableTargets,
		})
	}
	return all
}

func pageMetricsDTO(page *bridge.PageMetrics) *types.PageMetrics {
	if page == nil {
		return nil
	}
	return &types.PageMetrics{
		Targets:          page.Targets,
		JSHeapUsedMB:     page.JSHeapUsedMB,
		JSHeapTotalMB:    page.JSHeapTotalMB,
		Documents:        page.Documents,
		Frames:           page.Frames,
		Nodes:            page.Nodes,
		JSEventListeners: page.JSEventListeners,
	}
}

func (o *Orchestrator) ScreencastURL(instanceID, tabID string) string {
	o.mu.RLock()
	defer o.mu.RUnlock()
	inst, ok := o.instances[instanceID]
	if !ok {
		return ""
	}
	target, err := o.instancePathURL(inst, "/screencast", "tabId="+url.QueryEscape(tabID))
	if err != nil {
		return ""
	}
	switch target.Scheme {
	case "https":
		target.Scheme = "wss"
	default:
		target.Scheme = "ws"
	}
	return target.String()
}