High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
feat: implement hash-based IDs for profiles, instances, and tabs (BEFORE Phase 6)
Mariofb566e7fb463
diff --git a/internal/bridge/api.go b/internal/bridge/api.goindex c4ca99c8..707c6358 100644--- a/internal/bridge/api.go+++ b/internal/bridge/api.go@@ -102,18 +102,19 @@ type AnalyticsReport struct {}type Instance struct {- ID string `json:"id"`- Name string `json:"name"`- Profile string `json:"profile"`- Port string `json:"port"`- Headless bool `json:"headless"`- Status string `json:"status"`- StartTime time.Time `json:"startTime"`+ ID string `json:"id"` // Hash-based ID: inst_XXXXXXXX+ ProfileID string `json:"profileId"` // Hash-based profile ID: prof_XXXXXXXX+ ProfileName string `json:"profileName"` // Human-readable profile name (for display only)+ Port string `json:"port"` // Internal: instance port+ Headless bool `json:"headless"` // Mode: headless vs headed+ Status string `json:"status"` // Status: starting/running/stopping/stopped/error+ StartTime time.Time `json:"startTime"` // When instance was created+ Error string `json:"error,omitempty"` // Error message if status=error}type InstanceTab struct {- InstanceID string `json:"instanceId"`- TabID string `json:"tabId"`+ ID string `json:"id"` // Hash-based tab ID: tab_XXXXXXXX+ InstanceID string `json:"instanceId"` // Hash-based instance ID: inst_XXXXXXXXURL string `json:"url"`Title string `json:"title"`}diff --git a/internal/dashboard/dashboard.go b/internal/dashboard/dashboard.goindex ff73a183..82a49cde 100644--- a/internal/dashboard/dashboard.go+++ b/internal/dashboard/dashboard.go@@ -94,7 +94,7 @@ func (d *Dashboard) relayChildEvents() {if _, ok := tracked[inst.Port]; !ok {ctx, cancel := context.WithCancel(context.Background())tracked[inst.Port] = cancel- go d.subscribeChildSSE(ctx, inst.Port, inst.Name)+ go d.subscribeChildSSE(ctx, inst.Port, inst.ProfileName)}}diff --git a/internal/idutil/idutil.go b/internal/idutil/idutil.gonew file mode 100644index 00000000..63b8502b--- /dev/null+++ b/internal/idutil/idutil.go@@ -0,0 +1,65 @@+package idutil++import (+ "crypto/sha256"+ "encoding/hex"+ "fmt"+ "time"+)++// Manager generates stable hash-based IDs for profiles, instances, and tabs+type Manager struct{}++// NewManager creates a new ID manager+func NewManager() *Manager {+ return &Manager{}+}++// ProfileID generates a stable hash-based ID for a profile from its name+// Format: prof_XXXXXXXX (12 chars total)+func (m *Manager) ProfileID(name string) string {+ return hashID("prof", name)+}++// InstanceID generates a stable hash-based ID for an instance+// Uses profile ID, instance name, and creation timestamp for uniqueness+// Format: inst_XXXXXXXX (12 chars total)+func (m *Manager) InstanceID(profileID, instanceName string) string {+ data := fmt.Sprintf("%s:%s:%d", profileID, instanceName, time.Now().UnixNano())+ return hashID("inst", data)+}++// TabID generates a stable hash-based ID for a tab within an instance+// Uses instance ID and tab number for uniqueness+// Format: tab_XXXXXXXX (12 chars total)+func (m *Manager) TabID(instanceID string, tabIndex int) string {+ data := fmt.Sprintf("%s:%d", instanceID, tabIndex)+ return hashID("tab", data)+}++// hashID creates a short hash-based ID with the given prefix+// Format: {prefix}_{first 8 hex chars of SHA256}+func hashID(prefix, data string) string {+ hash := sha256.Sum256([]byte(data))+ hexHash := hex.EncodeToString(hash[:])+ // Take first 8 characters of hash for readability (still extremely collision-resistant)+ return fmt.Sprintf("%s_%s", prefix, hexHash[:8])+}++// IsValidID checks if an ID matches the expected prefix format+func IsValidID(id, prefix string) bool {+ if len(id) < len(prefix)+1 {+ return false+ }+ return id[:len(prefix)] == prefix && id[len(prefix)] == '_'+}++// ExtractPrefix extracts the prefix from an ID+func ExtractPrefix(id string) string {+ for i, c := range id {+ if c == '_' {+ return id[:i]+ }+ }+ return ""+}diff --git a/internal/orchestrator/handlers.go b/internal/orchestrator/handlers.goindex e6c14d10..bac580b0 100644--- a/internal/orchestrator/handlers.go+++ b/internal/orchestrator/handlers.go@@ -177,7 +177,7 @@ func (o *Orchestrator) handleProfileInstance(w http.ResponseWriter, r *http.Requinstances := o.List()for _, inst := range instances {- if inst.Name == name && (inst.Status == "running" || inst.Status == "starting") {+ if inst.ProfileName == name && (inst.Status == "running" || inst.Status == "starting") {web.JSON(w, 200, map[string]any{"name": name,"running": inst.Status == "running",diff --git a/internal/orchestrator/orchestrator.go b/internal/orchestrator/orchestrator.goindex dccddb7c..cbea6d7c 100644--- a/internal/orchestrator/orchestrator.go+++ b/internal/orchestrator/orchestrator.go@@ -14,11 +14,12 @@ import ("time""github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/idutil""github.com/pinchtab/pinchtab/internal/profiles")type Orchestrator struct {- instances map[string]*InstanceInternal+ instances map[string]*InstanceInternal // keyed by instance IDbaseDir stringbinary stringprofiles *profiles.ProfileManager@@ -27,6 +28,7 @@ type Orchestrator struct {client *http.ClientchildAuthToken stringportAllocator *PortAllocator+ idMgr *idutil.Manager}type InstanceInternal struct {@@ -77,6 +79,7 @@ func NewOrchestratorWithRunner(baseDir string, runner HostRunner) *Orchestratorclient: &http.Client{Timeout: 3 * time.Second},childAuthToken: os.Getenv("BRIDGE_TOKEN"),portAllocator: NewPortAllocator(9868, 9968),+ idMgr: idutil.NewManager(),}return orch}@@ -124,9 +127,9 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instancfor _, inst := range o.instances {if inst.Port == port && instanceIsActive(inst) {o.mu.Unlock()- return nil, fmt.Errorf("port %s already in use by instance %q", port, inst.Name)+ return nil, fmt.Errorf("port %s already in use by instance %q", port, inst.ProfileName)}- if inst.Name == name && instanceIsActive(inst) {+ if inst.ProfileName == name && instanceIsActive(inst) {o.mu.Unlock()return nil, fmt.Errorf("profile %q already has an active instance (%s)", name, inst.Status)}@@ -136,10 +139,13 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instancreturn nil, fmt.Errorf("port %s is already in use on this machine", port)}- id := fmt.Sprintf("%s-%s", name, port)- if inst, ok := o.instances[id]; ok && inst.Status == "running" {+ // Generate hash-based IDs+ profileID := o.idMgr.ProfileID(name)+ instanceID := o.idMgr.InstanceID(profileID, name)++ if inst, ok := o.instances[instanceID]; ok && inst.Status == "running" {o.mu.Unlock()- return nil, fmt.Errorf("instance %q already running", id)+ return nil, fmt.Errorf("instance already running for profile %q", name)}o.mu.Unlock()@@ -167,7 +173,7 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instanc})logBuf := newRingBuffer(64 * 1024)- slog.Info("starting instance process", "id", id, "binary", o.binary, "port", port, "profile", profilePath)+ slog.Info("starting instance process", "id", instanceID, "profile", name, "port", port)cmd, err := o.runner.Run(context.Background(), o.binary, env, logBuf, logBuf)if err != nil {@@ -176,13 +182,13 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instancinst := &InstanceInternal{Instance: bridge.Instance{- ID: id,- Name: name,- Profile: profilePath,- Port: port,- Headless: headless,- Status: "starting",- StartTime: time.Now(),+ ID: instanceID,+ ProfileID: profileID,+ ProfileName: name,+ Port: port,+ Headless: headless,+ Status: "starting",+ StartTime: time.Now(),},URL: fmt.Sprintf("http://localhost:%s", port),cmd: cmd,@@ -190,7 +196,7 @@ func (o *Orchestrator) Launch(name, port string, headless bool) (*bridge.Instanc}o.mu.Lock()- o.instances[id] = inst+ o.instances[instanceID] = insto.mu.Unlock()go o.monitor(inst)@@ -268,7 +274,7 @@ func (o *Orchestrator) StopProfile(name string) error {o.mu.RLock()ids := make([]string, 0, 1)for id, inst := range o.instances {- if inst.Name == name && instanceIsActive(inst) {+ if inst.ProfileName == name && instanceIsActive(inst) {ids = append(ids, id)}}@@ -452,8 +458,8 @@ func (o *Orchestrator) AllTabs() []bridge.InstanceTab {}for _, tab := range tabs {all = append(all, bridge.InstanceTab{+ ID: tab.ID,InstanceID: inst.ID,- TabID: tab.ID,URL: tab.URL,})}diff --git a/internal/orchestrator/orchestrator_test.go b/internal/orchestrator/orchestrator_test.goindex adae8b3f..eb03165c 100644--- a/internal/orchestrator/orchestrator_test.go+++ b/internal/orchestrator/orchestrator_test.go@@ -48,18 +48,18 @@ func TestOrchestrator_ListAndStop(t *testing.T) {runner := &mockRunner{portAvail: true}o := NewOrchestratorWithRunner(t.TempDir(), runner)- _, _ = o.Launch("p1", "9001", true)+ inst, _ := o.Launch("p1", "9001", true)// Simulate stopalive = false- err := o.Stop("p1-9001")+ err := o.Stop(inst.ID)if err != nil {t.Fatalf("Stop failed: %v", err)}- inst := o.List()[0]- if inst.Status != "stopped" {- t.Errorf("expected status stopped, got %s", inst.Status)+ instStatus := o.List()[0]+ if instStatus.Status != "stopped" {+ t.Errorf("expected status stopped, got %s", instStatus.Status)}}@@ -72,11 +72,14 @@ func TestOrchestrator_StopProfile(t *testing.T) {o := NewOrchestratorWithRunner(t.TempDir(), runner)o.mu.Lock()- o.instances["p1-9001"] = &InstanceInternal{+ instID := o.idMgr.InstanceID(o.idMgr.ProfileID("p1"), "p1")+ o.instances[instID] = &InstanceInternal{Instance: bridge.Instance{- ID: "p1-9001",- Name: "p1",- Status: "running",+ ID: instID,+ ProfileID: o.idMgr.ProfileID("p1"),+ ProfileName: "p1",+ Port: "9001",+ Status: "running",},URL: "http://localhost:9001",}diff --git a/internal/orchestrator/process_test.go b/internal/orchestrator/process_test.goindex 9a4b2db0..2dd5acea 100644--- a/internal/orchestrator/process_test.go+++ b/internal/orchestrator/process_test.go@@ -6,6 +6,7 @@ import ("testing""github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/idutil")type mockRunner struct {@@ -43,8 +44,9 @@ func TestLaunch_Mocked(t *testing.T) {if !runner.runCalled {t.Error("expected runner.Run to be called")}- if inst.ID != "test-prof-9999" {- t.Errorf("expected ID test-prof-9999, got %s", inst.ID)+ // Check that ID has the correct hash-based format (inst_XXXXXXXX)+ if !idutil.IsValidID(inst.ID, "inst") {+ t.Errorf("expected ID format inst_XXXXXXXX, got %s", inst.ID)}}diff --git a/internal/profiles/profiles.go b/internal/profiles/profiles.goindex 5fa3acdb..51dad765 100644--- a/internal/profiles/profiles.go+++ b/internal/profiles/profiles.go@@ -1,8 +1,6 @@package profilesimport (- "crypto/sha256"- "encoding/hex""fmt""io/fs""log/slog"@@ -13,11 +11,15 @@ import ("time""github.com/pinchtab/pinchtab/internal/bridge"+ "github.com/pinchtab/pinchtab/internal/idutil")+var idMgr = idutil.NewManager()++// profileID generates a stable hash-based ID from profile name+// Returns format: prof_XXXXXXXXfunc profileID(name string) string {- h := sha256.Sum256([]byte(name))- return hex.EncodeToString(h[:6])+ return idMgr.ProfileID(name)}type ProfileManager struct {