What happened? / 问题描述
Problem Description
When executing browser tasks through the page-agent MCP server, the task immediately fails with a "Task failed. Task aborted" error.
In the browser console of the Page Agent Hub, we can see the log:
Disposing PageAgent...
background.js:29 [RemotePageController.background] Error: Could not establish connection. Receiving end does not exist.
Root Cause Analysis
The root cause lies in a race condition between the imperative execution cycle of the Agent and the declarative lifecycle of React hooks inside the Chrome Extension.
- MCP passes config on every execution: Inside
@page-agent/mcp/src/index.js, every time execute_task is triggered, the MCP client sends the LLM environment configuration (e.g. LLM_BASE_URL, LLM_API_KEY) to the Hub via WebSocket:
const config = Object.keys(llmConfig).length > 0 ? llmConfig : undefined;
const result = await hub.executeTask(task, config);
- Hub WS handler immediately triggers re-configuration: In
useHubWs.ts (or hub-DUJlnjFc.js), when the WebSocket receives the execute event, it calls configure first, followed by execute(task):
onExecute: async (task, incomingConfig) => {
const { execute, configure, config } = latestRef.current;
if (incomingConfig) await configure({ ...config, ...incomingConfig }); // 1. Updates state asynchronously
const result = await execute(task); // 2. Starts execution
- React State update unmounts the current Agent: In
useAgent.ts, the hook instantiates MultiPageAgent inside a useEffect with [config] as a dependency:
useEffect(() => {
if (!config) return;
const agent = new MultiPageAgent({ ...config });
agentRef.current = agent;
// ...
return () => {
agent.dispose(); // ⚠️ Run cleanup whenever config reference changes!
};
}, [config]);
dispose() aborts the active task: Because configure triggers setConfig, React schedules a re-render. Since config's reference has changed, the cleanup function of the previous useEffect is immediately executed. This calls agent.dispose(), which executes this.#abortController.abort(), instantly killing the task that was just started.
How to reproduce / 如何复现
- Install chrome extension and configure MCP in AI agent, e.g., Cursor
- Send a command in AI agent invoking the MCP
Version
No response
Browser
Chrome: 149.0.7827.116
Extension: 1.10.0
Before submitting
What happened? / 问题描述
Problem Description
When executing browser tasks through the
page-agentMCP server, the task immediately fails with a"Task failed. Task aborted"error.In the browser console of the Page Agent Hub, we can see the log:
Root Cause Analysis
The root cause lies in a race condition between the imperative execution cycle of the Agent and the declarative lifecycle of React hooks inside the Chrome Extension.
@page-agent/mcp/src/index.js, every timeexecute_taskis triggered, the MCP client sends the LLM environment configuration (e.g.LLM_BASE_URL,LLM_API_KEY) to the Hub via WebSocket:useHubWs.ts(orhub-DUJlnjFc.js), when the WebSocket receives theexecuteevent, it callsconfigurefirst, followed byexecute(task):useAgent.ts, the hook instantiatesMultiPageAgentinside auseEffectwith[config]as a dependency:dispose()aborts the active task: BecauseconfiguretriggerssetConfig, React schedules a re-render. Sinceconfig's reference has changed, the cleanup function of the previoususeEffectis immediately executed. This callsagent.dispose(), which executesthis.#abortController.abort(), instantly killing the task that was just started.How to reproduce / 如何复现
Version
No response
Browser
Chrome: 149.0.7827.116
Extension: 1.10.0
Before submitting