High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix: ref stability, tab cleanup, timeouts, session restore, remove double import
bot615d279046b1
diff --git a/README.md b/README.mdindex 37b838ab..ac5b12b3 100644--- a/README.md+++ b/README.md@@ -99,7 +99,7 @@ Pinchtab patches `navigator.webdriver`, spoofs user agent, hides automation flagChrome profile saved at `~/.browser-bridge/chrome-profile/`. Cookies, localStorage, auth tokens — all persist across restarts. Tabs are saved to `~/.browser-bridge/sessions.json` on shutdown.### 📝 Text Extraction-Get readable page text without the tree overhead:+Get raw page text (body innerText) without the tree overhead:```bashcurl localhost:18800/text?tabId=X```diff --git a/TODO.md b/TODO.mdindex e96da553..db5ab50c 100644--- a/TODO.md+++ b/TODO.md@@ -1,34 +1,36 @@-# Browser Bridge — TODO+# Pinchtab — TODO-## Goal-Better browser for our agents: session persistence across restarts + direct controllability via HTTP.+## Done ✅+- [x] Session persistence — save/restore tabs on shutdown/startup+- [x] Graceful shutdown — save state on SIGTERM/SIGINT+- [x] Launch helper — self-launches Chrome, no manual CDP flags+- [x] Snapshot pruning — `?filter=interactive`, `?depth=N`+- [x] `/text` endpoint — body text extraction+- [x] Ref resolution via DOM.resolveNode + backendDOMNodeId+- [x] Stealth mode — webdriver hidden, UA spoofed, automation flags removed+- [x] Tab registry — contexts survive across requests+- [x] Ref stability — snapshot caches ref→nodeID mapping per tab, actions use cached refs+- [x] Action timeouts — 15s default, prevents hung pages blocking handlers+- [x] Tab cleanup — background goroutine removes stale entries every 30s+- [x] Tab restore on startup — loadState() called, tabs reopened-## P0: Working Foundation-- [ ] **Test with Chrome CDP** — restart Chrome with `--remote-debugging-port=9222`-- [ ] **Session persistence** — save tab state (URLs, scroll position) to `~/.browser-bridge/sessions.json` on shutdown, restore on startup-- [ ] **Auto-reconnect** — when Chrome restarts, bridge reconnects without dying-- [ ] **Improve ref resolution** — current `resolveRef()` is fragile. Use CDP `DOM.resolveNode` from a11y node backendDOMNodeId-- [ ] **`/text` endpoint** — readable text extraction (Readability/Defuddle style) for content reading tasks--## P1: Daily Driver Quality-- [ ] **Snapshot pruning** — `?filter=interactive` (buttons/links/inputs only), `?depth=N`, `?maxNodes=N`-- [ ] **Smart diff** — `?diff=true` returns only changes since last snapshot for that tab. Massive token savings on multi-step tasks-- [ ] **Launch helper** — `node server.js --launch` starts Chrome with CDP flag if not already running-- [ ] **Graceful shutdown** — save state on SIGTERM/SIGINT, clean disconnect-- [ ] **Tab restore on Chrome restart** — detect Chrome restart, reopen saved tabs-- [ ] **Error messages that help** — "Chrome not running with CDP" → "Run: /Applications/Google Chrome.app/... --remote-debugging-port=9222"+## P1: Daily Driver Quality+- [ ] **Smart diff** — `?diff=true` returns only changes since last snapshot. Massive token savings on multi-step tasks+- [ ] **Scroll actions** — scroll to element, scroll by amount+- [ ] **Wait for navigation** — after click, wait for page load before returning+- [ ] **Better /text** — Readability-style extraction instead of raw innerText## P2: Nice to Have-- [ ] **File-based output** — `?output=file` saves snapshot to disk, returns path. Agent reads only if needed (Playwright CLI approach, 4x token savings)-- [ ] **Compact format** — YAML or indented text instead of JSON for snapshots+- [ ] **File-based output** — `?output=file` saves snapshot to disk, returns path (Playwright CLI approach)+- [ ] **Compact format** — YAML or indented text instead of JSON- [ ] **Action chaining** — `POST /actions` batch multiple actions in one call-- [ ] **Docker image** — `docker run browser-bridge` with bundled Chromium-- [ ] **Config file** — `~/.browser-bridge/config.json` for port, Chrome path, auth token+- [ ] **Docker image** — `docker run pinchtab` with bundled Chromium+- [ ] **Config file** — `~/.pinchtab/config.json`- [ ] **LaunchAgent/systemd** — auto-start on boot## Not Doing- Plugin system-- Proxy rotation / anti-detection+- Proxy rotation / anti-detection- Session isolation / multi-tenant- Selenium compatibility- React UIdiff --git a/go.mod b/go.modindex c71221da..63d9a68e 100644--- a/go.mod+++ b/go.mod@@ -3,8 +3,11 @@ module github.com/luigi-agosti/pinchtabgo 1.25.0require (- github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d // indirect- github.com/chromedp/chromedp v0.14.2 // indirect+ github.com/chromedp/cdproto v0.0.0-20250803210736-d308e07a266d+ github.com/chromedp/chromedp v0.14.2+)++require (github.com/chromedp/sysutil v1.1.0 // indirectgithub.com/go-json-experiment/json v0.0.0-20250725192818-e39067aee2d2 // indirectgithub.com/gobwas/httphead v0.1.0 // indirectdiff --git a/go.sum b/go.sumindex 69af4558..0a3b6e7f 100644--- a/go.sum+++ b/go.sum@@ -12,6 +12,10 @@ github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=github.com/gobwas/pool v0.2.1/go.mod h1:q8bcK0KcYlCgd9e7WYLm9LpyS+YeLd8JVDW6WezmKEw=github.com/gobwas/ws v1.4.0 h1:CTaoG1tojrh4ucGPcoJFiAQUAsEWekEWvLy7GsVNqGs=github.com/gobwas/ws v1.4.0/go.mod h1:G3gNqMNtPppf5XUz7O4shetPpcZ1VJ7zt18dlUeakrc=+github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80 h1:6Yzfa6GP0rIo/kULo2bwGEkFvCePZ3qHDDTC3/J9Swo=+github.com/ledongthuc/pdf v0.0.0-20220302134840-0c2507a12d80/go.mod h1:imJHygn/1yfhB7XSJJKlFZKl/J+dCPAknuiaGOshXAs=+github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde h1:x0TT0RDC7UhAVbbWWBzr41ElhJx5tXPWkIHA2HWPRuw=+github.com/orisano/pixelmatch v0.0.0-20220722002657-fb0b55479cde/go.mod h1:nZgzbfBr3hhjoZnS66nKrHmduYNpc34ny7RK4z5/HM0=golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=diff --git a/main.go b/main.goindex 701a2923..d93fe7b3 100644--- a/main.go+++ b/main.go@@ -18,7 +18,6 @@ import ("github.com/chromedp/cdproto/page""github.com/chromedp/cdproto/target""github.com/chromedp/chromedp"- gojson "encoding/json")// ── Config ─────────────────────────────────────────────────@@ -30,6 +29,7 @@ var (stateDir = envOr("BRIDGE_STATE_DIR", filepath.Join(homeDir(), ".browser-bridge"))headless = os.Getenv("BRIDGE_HEADLESS") == "true"profileDir = envOr("BRIDGE_PROFILE", filepath.Join(homeDir(), ".browser-bridge", "chrome-profile"))+ actionTimeout = 15 * time.Second)// ── State ──────────────────────────────────────────────────@@ -41,8 +41,8 @@ type TabState struct {}type SessionState struct {- Tabs []TabState `json:"tabs"`- SavedAt string `json:"savedAt"`+ Tabs []TabState `json:"tabs"`+ SavedAt string `json:"savedAt"`}type TabEntry struct {@@ -50,10 +50,18 @@ type TabEntry struct {cancel context.CancelFunc}+// refCache stores the ref→backendNodeID mapping from the last snapshot per tab.+// This avoids re-fetching the a11y tree on every action — refs stay stable+// until the next snapshot call.+type refCache struct {+ refs map[string]int64 // "e0" → backendNodeID+}+type Bridge struct {allocCtx context.ContextbrowserCtx context.Context // persistent browser contexttabs map[string]*TabEntry+ snapshots map[string]*refCache // tabID → last snapshot's ref mappingmu sync.RWMutex}@@ -80,16 +88,12 @@ func main() {var allocCancel context.CancelFuncif cdpURL != "" {- // Remote mode: connect to existing Chromelog.Printf("Connecting to Chrome at %s", cdpURL)bridge.allocCtx, allocCancel = chromedp.NewRemoteAllocator(context.Background(), cdpURL)} else {- // Local mode: launch Chrome ourselvesos.MkdirAll(profileDir, 0755)log.Printf("Launching Chrome (profile: %s, headless: %v)", profileDir, headless)- // Start from minimal opts — DefaultExecAllocatorOptions sets- // automation flags that trigger bot detectionopts := []chromedp.ExecAllocatorOption{chromedp.UserDataDir(profileDir),chromedp.NoFirstRun,@@ -105,10 +109,7 @@ func main() {chromedp.Flag("disable-default-apps", false),chromedp.Flag("no-first-run", true),- // Use standard user agent (not the headless one)chromedp.UserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36"),-- // Standard window sizechromedp.WindowSize(1440, 900),}@@ -122,32 +123,25 @@ func main() {}defer allocCancel()- // Create persistent browser context (launches Chrome in local mode)browserCtx, browserCancel := chromedp.NewContext(bridge.allocCtx)defer browserCancel()// Initialize and inject stealth scriptsif err := chromedp.Run(browserCtx,chromedp.ActionFunc(func(ctx context.Context) error {- // Inject script to run on every new document (hides webdriver flag)_, err := page.AddScriptToEvaluateOnNewDocument(`- // Hide webdriverObject.defineProperty(navigator, 'webdriver', { get: () => undefined });- // Hide chrome.runtime (automation detection)if (!window.chrome) { window.chrome = {}; }if (!window.chrome.runtime) { window.chrome.runtime = {}; }- // Fix permissionsconst originalQuery = window.navigator.permissions.query;window.navigator.permissions.query = (parameters) => (parameters.name === 'notifications' ?Promise.resolve({ state: Notification.permission }) :originalQuery(parameters));- // Hide plugins lengthObject.defineProperty(navigator, 'plugins', {get: () => [1, 2, 3, 4, 5],});- // Hide languagesObject.defineProperty(navigator, 'languages', {get: () => ['en-GB', 'en-US', 'en'],});@@ -159,12 +153,19 @@ func main() {}bridge.browserCtx = browserCtxbridge.tabs = make(map[string]*TabEntry)+ bridge.snapshots = make(map[string]*refCache)// Register the initial tabinitTargetID := chromedp.FromContext(browserCtx).Target.TargetIDbridge.tabs[string(initTargetID)] = &TabEntry{ctx: browserCtx}log.Printf("Initial tab: %s", initTargetID)+ // Restore tabs from last session+ restoreState()++ // Start background tab cleanup+ go bridge.cleanStaleTabs(30 * time.Second)+mux := http.NewServeMux()mux.HandleFunc("GET /health", handleHealth)mux.HandleFunc("GET /tabs", handleTabs)@@ -176,10 +177,8 @@ func main() {mux.HandleFunc("POST /evaluate", handleEvaluate)mux.HandleFunc("POST /tab", handleTab)- // Wrap with auth + CORShandler := corsMiddleware(authMiddleware(mux))- // Graceful shutdown — save statesrv := &http.Server{Addr: ":" + port, Handler: handler}go func() {sig := make(chan os.Signal, 1)@@ -250,7 +249,7 @@ type rawAXNode struct {type rawAXValue struct {Type string `json:"type"`- Value gojson.RawMessage `json:"value"`+ Value json.RawMessage `json:"value"`}type rawAXProp struct {@@ -263,10 +262,9 @@ func (v *rawAXValue) String() string {return ""}var s string- if err := gojson.Unmarshal(v.Value, &s); err == nil {+ if err := json.Unmarshal(v.Value, &s); err == nil {return s}- // Try number/boolreturn strings.Trim(string(v.Value), `"`)}@@ -283,16 +281,18 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {}}- ctx, cancel, err := tabContext(tabID)+ ctx, resolvedTabID, err := tabContext(tabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()++ tCtx, tCancel := context.WithTimeout(ctx, actionTimeout)+ defer tCancel()// Use raw CDP call to avoid cdproto deserialization issues- var rawResult gojson.RawMessage- if err := chromedp.Run(ctx,+ var rawResult json.RawMessage+ if err := chromedp.Run(tCtx,chromedp.ActionFunc(func(ctx context.Context) error {return chromedp.FromContext(ctx).Target.Execute(ctx,"Accessibility.getFullAXTree", nil, &rawResult)@@ -302,11 +302,10 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {return}- // Parse raw responsevar treeResp struct {Nodes []rawAXNode `json:"nodes"`}- if err := gojson.Unmarshal(rawResult, &treeResp); err != nil {+ if err := json.Unmarshal(rawResult, &treeResp); err != nil {jsonErr(w, 500, fmt.Errorf("parse a11y tree: %v", err))return}@@ -331,7 +330,9 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {cur := nodeIDfor {p, ok := parentMap[cur]- if !ok { break }+ if !ok {+ break+ }d++cur = p}@@ -339,23 +340,35 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {}flat := make([]A11yNode, 0)+ refs := make(map[string]int64) // ref→backendNodeID cacherefID := 0for _, n := range treeResp.Nodes {- if n.Ignored { continue }+ if n.Ignored {+ continue+ }role := n.Role.String()name := n.Name.String()- if role == "none" || role == "generic" || role == "InlineTextBox" { continue }- if name == "" && role == "StaticText" { continue }+ if role == "none" || role == "generic" || role == "InlineTextBox" {+ continue+ }+ if name == "" && role == "StaticText" {+ continue+ }depth := depthOf(n.NodeID)- if maxDepth >= 0 && depth > maxDepth { continue }- if filter == "interactive" && !interactiveRoles[role] { continue }+ if maxDepth >= 0 && depth > maxDepth {+ continue+ }+ if filter == "interactive" && !interactiveRoles[role] {+ continue+ }+ ref := fmt.Sprintf("e%d", refID)entry := A11yNode{- Ref: fmt.Sprintf("e%d", refID),+ Ref: ref,Role: role,Name: name,Depth: depth,@@ -366,6 +379,7 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {}if n.BackendDOMNodeID != 0 {entry.NodeID = n.BackendDOMNodeID+ refs[ref] = n.BackendDOMNodeID}for _, prop := range n.Properties {@@ -381,8 +395,13 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {refID++}+ // Cache the ref→nodeID mapping for this tab (#1 ref stability fix)+ bridge.mu.Lock()+ bridge.snapshots[resolvedTabID] = &refCache{refs: refs}+ bridge.mu.Unlock()+var url, title string- chromedp.Run(ctx,+ chromedp.Run(tCtx,chromedp.Location(&url),chromedp.Title(&title),)@@ -400,12 +419,14 @@ func handleSnapshot(w http.ResponseWriter, r *http.Request) {func handleScreenshot(w http.ResponseWriter, r *http.Request) {tabID := r.URL.Query().Get("tabId")- ctx, cancel, err := tabContext(tabID)+ ctx, _, err := tabContext(tabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()++ tCtx, tCancel := context.WithTimeout(ctx, actionTimeout)+ defer tCancel()var buf []bytequality := 80@@ -415,7 +436,7 @@ func handleScreenshot(w http.ResponseWriter, r *http.Request) {}}- if err := chromedp.Run(ctx,+ if err := chromedp.Run(tCtx,chromedp.ActionFunc(func(ctx context.Context) error {var err errorbuf, err = page.CaptureScreenshot().@@ -429,7 +450,6 @@ func handleScreenshot(w http.ResponseWriter, r *http.Request) {return}- // Return raw image if requestedif r.URL.Query().Get("raw") == "true" {w.Header().Set("Content-Type", "image/jpeg")w.Write(buf)@@ -438,7 +458,7 @@ func handleScreenshot(w http.ResponseWriter, r *http.Request) {jsonResp(w, 200, map[string]any{"format": "jpeg",- "base64": buf, // encoding/json will base64-encode []byte+ "base64": buf,})}@@ -447,15 +467,17 @@ func handleScreenshot(w http.ResponseWriter, r *http.Request) {func handleText(w http.ResponseWriter, r *http.Request) {tabID := r.URL.Query().Get("tabId")- ctx, cancel, err := tabContext(tabID)+ ctx, _, err := tabContext(tabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()++ tCtx, tCancel := context.WithTimeout(ctx, actionTimeout)+ defer tCancel()var text string- if err := chromedp.Run(ctx,+ if err := chromedp.Run(tCtx,chromedp.Evaluate(`document.body.innerText`, &text),); err != nil {jsonErr(w, 500, err)@@ -463,7 +485,7 @@ func handleText(w http.ResponseWriter, r *http.Request) {}var url, title string- chromedp.Run(ctx,+ chromedp.Run(tCtx,chromedp.Location(&url),chromedp.Title(&title),)@@ -491,20 +513,27 @@ func handleNavigate(w http.ResponseWriter, r *http.Request) {return}- ctx, cancel, err := tabContext(req.TabID)+ ctx, resolvedTabID, err := tabContext(req.TabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()- if err := chromedp.Run(ctx, chromedp.Navigate(req.URL)); err != nil {+ tCtx, tCancel := context.WithTimeout(ctx, 30*time.Second)+ defer tCancel()++ if err := chromedp.Run(tCtx, chromedp.Navigate(req.URL)); err != nil {jsonErr(w, 500, err)return}+ // Invalidate snapshot cache — page changed+ bridge.mu.Lock()+ delete(bridge.snapshots, resolvedTabID)+ bridge.mu.Unlock()+var url, title string- chromedp.Run(ctx,+ chromedp.Run(tCtx,chromedp.Location(&url),chromedp.Title(&title),)@@ -529,21 +558,36 @@ func handleAction(w http.ResponseWriter, r *http.Request) {return}- ctx, cancel, err := tabContext(req.TabID)+ ctx, resolvedTabID, err := tabContext(req.TabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()- // Resolve target: prefer nodeId > selector > ref-based lookup+ tCtx, tCancel := context.WithTimeout(ctx, actionTimeout)+ defer tCancel()++ // Resolve ref to backendNodeID from cached snapshot+ if req.Ref != "" && req.NodeID == 0 && req.Selector == "" {+ bridge.mu.RLock()+ cache := bridge.snapshots[resolvedTabID]+ bridge.mu.RUnlock()+ if cache != nil {+ if nid, ok := cache.refs[req.Ref]; ok {+ req.NodeID = nid+ }+ }+ if req.NodeID == 0 {+ jsonResp(w, 400, map[string]string{+ "error": fmt.Sprintf("ref %s not found — take a /snapshot first", req.Ref),+ })+ return+ }+ }+var sel stringif req.Selector != "" {sel = req.Selector- } else if req.NodeID > 0 {- // Use JS to find element by backend node ID — chromedp resolves via DOM.resolveNode- sel = fmt.Sprintf(`[data-bridge-node="%d"]`, req.NodeID)- // For nodeId-based actions, we use chromedp's built-in node resolution}var result map[string]any@@ -551,9 +595,9 @@ func handleAction(w http.ResponseWriter, r *http.Request) {switch req.Kind {case "click":if sel != "" {- err = chromedp.Run(ctx, chromedp.Click(sel, chromedp.ByQuery))- } else if req.Ref != "" {- err = clickByRef(ctx, req.Ref)+ err = chromedp.Run(tCtx, chromedp.Click(sel, chromedp.ByQuery))+ } else if req.NodeID > 0 {+ err = clickByNodeID(tCtx, req.NodeID)} else {jsonResp(w, 400, map[string]string{"error": "need selector, ref, or nodeId"})return@@ -566,18 +610,21 @@ func handleAction(w http.ResponseWriter, r *http.Request) {return}if sel != "" {- err = chromedp.Run(ctx,+ err = chromedp.Run(tCtx,chromedp.Click(sel, chromedp.ByQuery),chromedp.SendKeys(sel, req.Text, chromedp.ByQuery),)- } else if req.Ref != "" {- err = typeByRef(ctx, req.Ref, req.Text)+ } else if req.NodeID > 0 {+ err = typeByNodeID(tCtx, req.NodeID, req.Text)+ } else {+ jsonResp(w, 400, map[string]string{"error": "need selector or ref"})+ return}result = map[string]any{"typed": req.Text}case "fill":if sel != "" {- err = chromedp.Run(ctx,+ err = chromedp.Run(tCtx,chromedp.SetValue(sel, req.Text, chromedp.ByQuery),)}@@ -588,12 +635,19 @@ func handleAction(w http.ResponseWriter, r *http.Request) {jsonResp(w, 400, map[string]string{"error": "key required for press"})return}- err = chromedp.Run(ctx, chromedp.KeyEvent(req.Key))+ err = chromedp.Run(tCtx, chromedp.KeyEvent(req.Key))result = map[string]any{"pressed": req.Key}case "focus":if sel != "" {- err = chromedp.Run(ctx, chromedp.Focus(sel, chromedp.ByQuery))+ err = chromedp.Run(tCtx, chromedp.Focus(sel, chromedp.ByQuery))+ } else if req.NodeID > 0 {+ err = chromedp.Run(tCtx,+ chromedp.ActionFunc(func(ctx context.Context) error {+ p := map[string]any{"backendNodeId": req.NodeID}+ return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.focus", p, nil)+ }),+ )}result = map[string]any{"focused": true}@@ -626,15 +680,17 @@ func handleEvaluate(w http.ResponseWriter, r *http.Request) {return}- ctx, cancel, err := tabContext(req.TabID)+ ctx, _, err := tabContext(req.TabID)if err != nil {jsonErr(w, 404, err)return}- defer cancel()++ tCtx, tCancel := context.WithTimeout(ctx, actionTimeout)+ defer tCancel()var result any- if err := chromedp.Run(ctx,+ if err := chromedp.Run(tCtx,chromedp.Evaluate(req.Expression, &result),); err != nil {jsonErr(w, 500, err)@@ -671,7 +727,6 @@ func handleTab(w http.ResponseWriter, r *http.Request) {return}- // Register in tab registrynewTargetID := string(chromedp.FromContext(ctx).Target.TargetID)bridge.mu.Lock()bridge.tabs[newTargetID] = &TabEntry{ctx: ctx, cancel: cancel}@@ -686,6 +741,18 @@ func handleTab(w http.ResponseWriter, r *http.Request) {jsonResp(w, 400, map[string]string{"error": "tabId required"})return}++ // Clean up from registry+ bridge.mu.Lock()+ if entry, ok := bridge.tabs[req.TabID]; ok {+ if entry.cancel != nil {+ entry.cancel()+ }+ delete(bridge.tabs, req.TabID)+ delete(bridge.snapshots, req.TabID)+ }+ bridge.mu.Unlock()+ctx, cancel := chromedp.NewContext(bridge.browserCtx,chromedp.WithTargetID(target.ID(req.TabID)),)@@ -701,39 +768,27 @@ func handleTab(w http.ResponseWriter, r *http.Request) {}}-// ── Ref-based actions ──────────────────────────────────────--func clickByRef(ctx context.Context, ref string) error {- nodeID, err := resolveRefToNodeID(ctx, ref)- if err != nil {- return err- }- return clickByNodeID(ctx, nodeID)-}+// ── Node-based actions (use backendNodeID directly) ────────func clickByNodeID(ctx context.Context, backendNodeID int64) error {- // Use DOM.resolveNode to get a JS object reference, then click itreturn chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {- // Resolve backend node to a remote objectp := map[string]any{"backendNodeId": backendNodeID}- var result gojson.RawMessage+ var result json.RawMessageif err := chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.resolveNode", p, &result); err != nil {return fmt.Errorf("DOM.resolveNode: %v", err)}- // Parse the object IDvar resp struct {Object struct {ObjectID string `json:"objectId"`} `json:"object"`}- if err := gojson.Unmarshal(result, &resp); err != nil {+ if err := json.Unmarshal(result, &resp); err != nil {return err}if resp.Object.ObjectID == "" {return fmt.Errorf("no objectId for node %d", backendNodeID)}- // Call click() on the resolved objectcallP := map[string]any{"objectId": resp.Object.ObjectID,"functionDeclaration": "function() { this.click(); }",@@ -744,84 +799,38 @@ func clickByNodeID(ctx context.Context, backendNodeID int64) error {)}-func typeByRef(ctx context.Context, ref string, text string) error {- nodeID, err := resolveRefToNodeID(ctx, ref)- if err != nil {- return err- }- // Focus via DOM.focus then type via keyboard+func typeByNodeID(ctx context.Context, backendNodeID int64, text string) error {return chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {- p := map[string]any{"backendNodeId": nodeID}+ p := map[string]any{"backendNodeId": backendNodeID}return chromedp.FromContext(ctx).Target.Execute(ctx, "DOM.focus", p, nil)}),chromedp.KeyEvent(text),)}-func resolveRefToNodeID(ctx context.Context, ref string) (int64, error) {- refNum := 0- fmt.Sscanf(strings.TrimPrefix(ref, "e"), "%d", &refNum)-- var rawResult gojson.RawMessage- if err := chromedp.Run(ctx,- chromedp.ActionFunc(func(ctx context.Context) error {- return chromedp.FromContext(ctx).Target.Execute(ctx,- "Accessibility.getFullAXTree", nil, &rawResult)- }),- ); err != nil {- return 0, err- }-- var treeResp struct {- Nodes []rawAXNode `json:"nodes"`- }- if err := gojson.Unmarshal(rawResult, &treeResp); err != nil {- return 0, err- }-- idx := 0- for _, n := range treeResp.Nodes {- if n.Ignored { continue }- role := n.Role.String()- if role == "none" || role == "generic" || role == "InlineTextBox" { continue }- name := n.Name.String()- if name == "" && role == "StaticText" { continue }-- if idx == refNum {- if n.BackendDOMNodeID != 0 {- return n.BackendDOMNodeID, nil- }- return 0, fmt.Errorf("node at ref %s has no backend DOM node", ref)- }- idx++- }-- return 0, fmt.Errorf("ref %s not found (max: e%d)", ref, idx-1)-}-// ── Tab context helper ─────────────────────────────────────-func tabContext(tabID string) (context.Context, context.CancelFunc, error) {- bridge.mu.RLock()- defer bridge.mu.RUnlock()+// tabContext returns the chromedp context for a tab and the resolved tabID.+// If tabID is empty, uses the first page target.+func tabContext(tabID string) (context.Context, string, error) {+ bridge.mu.Lock()+ defer bridge.mu.Unlock()if tabID == "" {- // Use first page targettargets, err := listTargets()if err != nil {- return nil, nil, err+ return nil, "", err}if len(targets) == 0 {- return nil, nil, fmt.Errorf("no tabs open")+ return nil, "", fmt.Errorf("no tabs open")}tabID = string(targets[0].TargetID)}// Check registry firstif entry, ok := bridge.tabs[tabID]; ok {- noop := func() {}- return entry.ctx, noop, nil+ return entry.ctx, tabID, nil}// Create new context for this target@@ -830,18 +839,41 @@ func tabContext(tabID string) (context.Context, context.CancelFunc, error) {)if err := chromedp.Run(ctx); err != nil {cancel()- return nil, nil, fmt.Errorf("tab %s not found: %v", tabID, err)+ return nil, "", fmt.Errorf("tab %s not found: %v", tabID, err)}- // Don't cancel — store in registry (caller gets noop cancel)- bridge.mu.RUnlock()- bridge.mu.Lock()bridge.tabs[tabID] = &TabEntry{ctx: ctx, cancel: cancel}- bridge.mu.Unlock()- bridge.mu.RLock()+ return ctx, tabID, nil+}++// cleanStaleTabs periodically removes tab entries whose targets no longer exist.+func (b *Bridge) cleanStaleTabs(interval time.Duration) {+ for {+ time.Sleep(interval)++ targets, err := listTargets()+ if err != nil {+ continue+ }++ alive := make(map[string]bool, len(targets))+ for _, t := range targets {+ alive[string(t.TargetID)] = true+ }- noop := func() {}- return ctx, noop, nil+ b.mu.Lock()+ for id, entry := range b.tabs {+ if !alive[id] {+ if entry.cancel != nil {+ entry.cancel()+ }+ delete(b.tabs, id)+ delete(b.snapshots, id)+ log.Printf("Cleaned stale tab: %s", id)+ }+ }+ b.mu.Unlock()+ }}func listTargets() ([]*target.Info, error) {@@ -856,7 +888,6 @@ func listTargets() ([]*target.Info, error) {return nil, err}- // Filter to page targets onlypages := make([]*target.Info, 0)for _, t := range targets {if t.Type == "page" {@@ -900,17 +931,32 @@ func saveState() {}}-func loadState() *SessionState {+func restoreState() {path := filepath.Join(stateDir, "sessions.json")data, err := os.ReadFile(path)if err != nil {- return nil+ return}var state SessionStateif err := json.Unmarshal(data, &state); err != nil {- return nil+ return+ }++ restored := 0+ for _, tab := range state.Tabs {+ ctx, cancel := chromedp.NewContext(bridge.browserCtx)+ if err := chromedp.Run(ctx, chromedp.Navigate(tab.URL)); err != nil {+ cancel()+ log.Printf("Failed to restore tab %s: %v", tab.URL, err)+ continue+ }+ newID := string(chromedp.FromContext(ctx).Target.TargetID)+ bridge.tabs[newID] = &TabEntry{ctx: ctx, cancel: cancel}+ restored+++ }+ if restored > 0 {+ log.Printf("Restored %d tabs from previous session", restored)}- return &state}// ── Middleware ──────────────────────────────────────────────