From edb769b8267c7270223065107b3fbd9df24872ba Mon Sep 17 00:00:00 2001 From: Simon <10131203+gaomeng1900@users.noreply.github.com> Date: Thu, 11 Jun 2026 19:53:30 +0800 Subject: [PATCH] fix(ext): handle stopped lifecycle state --- packages/extension/docs/extension_api.md | 2 +- .../extension/src/agent/MultiPageAgent.ts | 30 +++++++++++-------- .../src/entrypoints/sidepanel/App.tsx | 2 +- packages/extension/src/lib/db.ts | 2 +- 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/packages/extension/docs/extension_api.md b/packages/extension/docs/extension_api.md index ab661d2..bb48461 100644 --- a/packages/extension/docs/extension_api.md +++ b/packages/extension/docs/extension_api.md @@ -131,7 +131,7 @@ export type Execute = (task: string, config: ExecuteConfig) => Promise { + if (heartBeatInterval) { + clearInterval(heartBeatInterval) + heartBeatInterval = null + } + chrome.storage.local.set({ isAgentRunning: false }).catch(console.error) + tabsController.dispose() }, }) - /** - * Project agent status into chrome.storage. The content script polls - * `isAgentRunning` + `agentHeartbeat` (eventually consistent by design). - * - * When the agent is in side-panel and user closed the side-panel. - * There is no chance for isAgentRunning to be set false. - * (unload event doesn't work well in side panel.) - * (I'm trying not to use long-lived connection because the lifecycle of a sw is hard to predict.) - * This heartbeat mechanism acts as a backup. - */ - let heartBeatInterval: number | null = null - this.addEventListener('statuschange', () => { const running = this.status === 'running' diff --git a/packages/extension/src/entrypoints/sidepanel/App.tsx b/packages/extension/src/entrypoints/sidepanel/App.tsx index 8fe5c17..e54948f 100644 --- a/packages/extension/src/entrypoints/sidepanel/App.tsx +++ b/packages/extension/src/entrypoints/sidepanel/App.tsx @@ -39,7 +39,7 @@ export default function App() { if ( prev === 'running' && - (status === 'completed' || status === 'error') && + (status === 'completed' || status === 'error' || status === 'stopped') && history.length > 0 && currentTask ) { diff --git a/packages/extension/src/lib/db.ts b/packages/extension/src/lib/db.ts index 9e520a8..e0db44c 100644 --- a/packages/extension/src/lib/db.ts +++ b/packages/extension/src/lib/db.ts @@ -8,7 +8,7 @@ export interface SessionRecord { id: string task: string history: HistoricalEvent[] - status: 'completed' | 'error' + status: 'completed' | 'error' | 'stopped' createdAt: number }