High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.
fix all bugs & in progress items
botd34193aaed1c
diff --git a/TODO.md b/TODO.mdindex 8b347b87..77475e44 100644--- a/TODO.md+++ b/TODO.md@@ -11,9 +11,9 @@ BridgeAPI interface, handler tests, nil guard, deprecated flag removal.---## Bugs & In Progress-- [ ] **Navigate timeout on some SPAs** — `navigatePage` 500ms sleep isn't enough for heavy JS pages. Consider polling `document.readyState` instead.-- [ ] **Restore navigates all tabs at once** — can overwhelm CPU/memory on startup with many tabs. Should queue or limit concurrency.-- [ ] **Screenshot base64 returns raw bytes** — `"base64": <bytes>` in JSON, should be actual base64 string encoding.+- [x] **Navigate timeout on some SPAs** — Fixed: `navigatePage` now polls `document.readyState` until interactive/complete (10s timeout)+- [x] **Restore navigates all tabs at once** — Fixed: Added concurrency limiting (max 3 concurrent tab creations, max 2 concurrent navigations)+- [x] **Screenshot base64 returns raw bytes** — Fixed: Already working correctly, returns proper base64 string encoding in JSON## P6: Next Up- [ ] **Action chaining** — `POST /actions` batch multiple actions in one call (big token saver for agents)diff --git a/cdp.go b/cdp.goindex 89c3e742..6c187047 100644--- a/cdp.go+++ b/cdp.go@@ -9,9 +9,9 @@ import ("github.com/chromedp/chromedp")-// navigatePage uses raw CDP Page.navigate + polls for a non-blank URL.+// navigatePage uses raw CDP Page.navigate + polls document.readyState for completion.// Unlike chromedp.Navigate which waits for the full load event (hangs on SPAs),-// this fires navigation and waits up to 5s for the page to start loading.+// this fires navigation and waits for interactive/complete state or timeout.func navigatePage(ctx context.Context, url string) error {return chromedp.Run(ctx,chromedp.ActionFunc(func(ctx context.Context) error {@@ -29,9 +29,34 @@ func navigatePage(ctx context.Context, url string) error {}return nil}),- // Brief sleep to let the page start rendering — not a full load wait.- // Agents should use /snapshot to confirm readiness.- chromedp.Sleep(500*time.Millisecond),+ // Poll document.readyState until interactive/complete or timeout+ chromedp.ActionFunc(func(ctx context.Context) error {+ deadline := time.Now().Add(10 * time.Second)+ for time.Now().Before(deadline) {+ var readyState string+ evalP := map[string]any{+ "expression": "document.readyState",+ }+ var evalResult json.RawMessage+ if err := chromedp.FromContext(ctx).Target.Execute(ctx, "Runtime.evaluate", evalP, &evalResult); err == nil {+ var evalResp struct {+ Result struct {+ Value string `json:"value"`+ } `json:"result"`+ }+ if json.Unmarshal(evalResult, &evalResp) == nil {+ readyState = evalResp.Result.Value+ if readyState == "interactive" || readyState == "complete" {+ return nil // Page is ready for interaction+ }+ }+ }+ // Check every 200ms to avoid excessive polling+ time.Sleep(200 * time.Millisecond)+ }+ // Timeout reached, but don't fail - page might still be usable+ return nil+ }),)}diff --git a/state.go b/state.goindex fe03adf1..563fe9c6 100644--- a/state.go+++ b/state.go@@ -82,7 +82,7 @@ func (b *Bridge) SaveState() {}}-// RestoreState reopens tabs from the last saved session.+// RestoreState reopens tabs from the last saved session with concurrency limiting.func (b *Bridge) RestoreState() {path := filepath.Join(stateDir, "sessions.json")data, err := os.ReadFile(path)@@ -94,28 +94,55 @@ func (b *Bridge) RestoreState() {return}+ if len(state.Tabs) == 0 {+ return+ }++ // Limit concurrent tab creation to avoid overwhelming Chrome+ const maxConcurrentTabs = 3+ const maxConcurrentNavs = 2++ tabSem := make(chan struct{}, maxConcurrentTabs)+ navSem := make(chan struct{}, maxConcurrentNavs)+restored := 0for _, tab := range state.Tabs {if strings.Contains(tab.URL, "/sorry/") || strings.Contains(tab.URL, "about:blank") {continue}++ // Acquire semaphore for tab creation+ tabSem <- struct{}{}++ // Small delay between tab creations to spread load+ if restored > 0 {+ time.Sleep(200 * time.Millisecond)+ }+ctx, cancel := chromedp.NewContext(b.browserCtx)// Just initialize the tab context (attaches to Chrome) — don't navigate yet.- // The agent will navigate when it needs the tab.if err := chromedp.Run(ctx); err != nil {cancel()+ <-tabSem // Release semaphoreslog.Warn("restore tab failed", "url", tab.URL, "err", err)continue}+newID := string(chromedp.FromContext(ctx).Target.TargetID)b.mu.Lock()b.tabs[newID] = &TabEntry{ctx: ctx, cancel: cancel}b.mu.Unlock()restored++- // Fire-and-forget navigate — don't block on page load+ // Fire-and-forget navigate with concurrency limitinggo func(tabCtx context.Context, url string) {- tCtx, tCancel := context.WithTimeout(tabCtx, 10*time.Second)+ defer func() { <-tabSem }() // Release tab semaphore when done++ // Acquire navigation semaphore+ navSem <- struct{}{}+ defer func() { <-navSem }()++ tCtx, tCancel := context.WithTimeout(tabCtx, 15*time.Second)defer tCancel()_ = chromedp.Run(tCtx, chromedp.ActionFunc(func(ctx context.Context) error {p := map[string]any{"url": url}@@ -124,6 +151,6 @@ func (b *Bridge) RestoreState() {}(ctx, tab.URL)}if restored > 0 {- slog.Info("restored tabs", "count", restored)+ slog.Info("restored tabs", "count", restored, "concurrent_limit", maxConcurrentTabs)}}