Files
makelore/.project-docs/30-worklog/tasks/20260927-browser-tab-focus-5c92d7a1.md

5.4 KiB

Task: Fix agent browser stealing preview tab and failing when hidden

Identity

  • Task ID: 20260927-browser-tab-focus-5c92d7a1
  • Mode: Feature
  • Branch: codex/20260927-browser-tab-focus-5c92d7a1-browser-tab-focus
  • Worktree: D:\Datas\OthersProjects.codex-worktrees\makelore\20260927-browser-tab-focus-5c92d7a1
  • Base commit: 220e89992b
  • Owner: codex
  • Status: Ready for Integration

Scope

  • Main-owned Agent Browser viewport/tool readiness and Pi open presentation requests; targeted module, tool and Electron regressions; README behavior contract.
  • Isolated from packaging task 20260927-package-170-a7c3 and completed consultation-tool changes. No Yuxi/server changes.

Intent And Constraints

  • Switching from Work to chat must preserve the browser and allow Agent debugging. Reopening an established page must not repeatedly select Work.
  • Preserve first-open real viewport initialization, explicit close, project/module/window teardown, sandbox and current-project/CDP restrictions.
  • Concurrent Task Gate and Planning Gate passed at base 220e899; relevant browser architecture/domain and peer task records read. User feedback authorizes distinguishing tab hiding from closing.
  • First reproduce hidden-page CDP failure and repeated-open presentation; apply the smallest owning-layer fix, then verify module/tool regressions, actual Electron behavior, typecheck and build.
  • No live-app restart, model traffic, deployment, push, merge or worktree cleanup in this task.

Outcome

  • Reproduced two primary causes: hidden views with valid bounds were rejected as VIEWPORT_NOT_READY; every Pi open emitted agent-browser:show and waited for presentation, even when reusing an established view.
  • Tool readiness now checks the established viewport rather than current visibility. Initial no-bounds rejection, generation/project checks, debugger state and explicit teardown remain intact.
  • Pi open only requests foreground presentation for a new viewport. Reuse/navigation publishes state and preserves the user's selected tab.
  • Actual Electron testing uncovered an additional reachable failure: hidden Page.captureScreenshot timed out despite evaluate/navigation succeeding. Disabling background throttling and using fromSurface=false did not fix it. Electron capturePage with stayHidden:true/stayAwake:false requests a frame successfully; adapter now does that before hidden CDP capture, retaining CDP parameters, tab selection and composer focus without a permanent rendering override.
  • Updated README. No Renderer workaround, new runtime dependency, server/Yuxi edit, cloud traffic, installation, merge or push.

Verification

  • Red: pnpm exec vitest run tests/unit/agent-browser-core.test.ts tests/unit/pi-product-tools.test.ts -t 'keeps a view hidden|preserves the selected tab' --maxWorkers=2. Three expected failures: hidden CDP VIEWPORT_NOT_READY and repeated presentation requests.
  • Green: the same three regressions passed after fixing module/tool ownership boundaries.
  • Related regression suite: agent-browser-core, pi-product-tools, agent-browser-panel, agent-browser-electron-adapter and agent-browser-routes: 134 tests passed.
  • Actual Electron E2E: 4 passed (native Pi tool + sandboxed WebContentsView while switching tabs; repeated Work preparation/draft retention; failed preparation recovery; native publication preflight isolation/cleanup). The native browser test verifies initial presentation, repeated hidden open including navigation, evaluate, reload, console events, screenshot, unchanged generation, restored Work view and explicit close. Project/model APIs are fixtures; native browser/tool/Renderer components are real. No paid model involved.
  • pnpm run typecheck and pnpm run build:vite passed. Build has existing Browserslist age / chunk warnings.
  • Additional Main-only tsc check reports 66 existing diagnostics. TypeScript compiler-host comparison against base 220e899 with the three changed product sources replaced in memory produced the same 66 diagnostics, zero added, none in touched files. This optional broader check is not green; standard project typecheck is green.
  • Official Electron webContents.capturePage documentation confirms stayHidden/stayAwake options: https://www.electronjs.org/docs/latest/api/web-contents#contentscapturepagerect-opts . Behavior also verified on the repository's Electron 43.4.0.
  • Diff review and whitespace check performed; no debug probes or permanent background-throttling override remain.

Follow-ups

  • Await user-requested integration and a subsequent rebuilt client; current running installation is unchanged.
  • Main compiler's pre-existing 66 diagnostics are outside this browser fix; no errors added.

Promotion Candidates

  • Targets: .project-docs/20-architecture/module-map.md, .project-docs/20-architecture/data-flow.md and .project-docs/40-domain/business-rules.md. Clarify that only first viewport initialization requires foreground presentation; hiding the Work tab retains its view/debugger and tool access, while explicit close/project/module/window teardown still releases it. Evidence: actual native E2E plus 134 focused tests. Future impact: prevent reinstating a visibility gate or unconditional show request. Semantic conflict: older text says hidden browsers cannot be debugged; the user's current request explicitly supersedes that behavior, so no additional product approval is needed for this distinction during integration.