fix: update window reference on every task start to prevent Question API 503#683
fix: update window reference on every task start to prevent Question API 503#683mvanhorn wants to merge 1 commit intoaccomplish-ai:mainfrom
Conversation
…API 503 The initPermissionApi call was inside a one-time initialization guard, so the mainWindow reference was never updated after the first task. If the BrowserWindow was recreated (e.g. macOS app reactivation), the stale reference caused mainWindow.isDestroyed() to return true, resulting in a 503 "Question API not initialized" error. Move initPermissionApi outside the guard so the window reference is refreshed on every task start, while keeping server startup one-time. Fixes accomplish-ai#679 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
📝 WalkthroughWalkthroughThe change unconditionally reinitializes the permission API with the current window reference before the existing initialization guard, ensuring the API remains bound to the correct window even after window recreation. This addresses the 503 "Question API not initialized" error. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
apps/desktop/src/main/ipc/handlers.ts (1)
306-365: Consider addinginitPermissionApitosession:resumeas well.The
session:resumehandler also receives a freshwindowreference and starts tasks that may use the permission/question APIs. If a user reopens the app window and resumes a session (without first callingtask:start), the permission API would still hold a stale window reference, potentially causing the same 503 error.Proposed fix
const validatedExistingTaskId = existingTaskId ? sanitizeString(existingTaskId, 'taskId', 128) : undefined; if (!isMockTaskEventsEnabled() && !storage.hasReadyProvider()) { throw new Error( 'No provider is ready. Please connect a provider and select a model in Settings.', ); } + // Update window reference for permission/question APIs (handles window recreation) + initPermissionApi(window, () => taskManager.getActiveTaskId()); + const taskId = validatedExistingTaskId || createTaskId();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/desktop/src/main/ipc/handlers.ts` around lines 306 - 365, The session:resume IPC handler currently obtains a fresh window (via BrowserWindow.fromWebContents(event.sender)) but never reinitializes the permission/question API, leaving it pointing at a stale window; call initPermissionApi with the new window (the same BrowserWindow instance you assign to window) before creating callbacks or calling taskManager.startTask so the permission API is bound to the current window; update the handler around where you createTaskCallbacks and before taskManager.startTask (references: 'session:resume' handler, BrowserWindow.fromWebContents(event.sender), initPermissionApi, createTaskCallbacks, taskManager.startTask).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/desktop/src/main/ipc/handlers.ts`:
- Around line 306-365: The session:resume IPC handler currently obtains a fresh
window (via BrowserWindow.fromWebContents(event.sender)) but never reinitializes
the permission/question API, leaving it pointing at a stale window; call
initPermissionApi with the new window (the same BrowserWindow instance you
assign to window) before creating callbacks or calling taskManager.startTask so
the permission API is bound to the current window; update the handler around
where you createTaskCallbacks and before taskManager.startTask (references:
'session:resume' handler, BrowserWindow.fromWebContents(event.sender),
initPermissionApi, createTaskCallbacks, taskManager.startTask).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 8497ca47-0ae4-4b1c-b6cd-a0b6c37c90e9
📒 Files selected for processing (1)
apps/desktop/src/main/ipc/handlers.ts
Fixes #679
Summary
The
AskUserQuestionMCP tool was returning a503: Question API not initializederror because themainWindowreference became stale after window recreation.Root cause
In
apps/desktop/src/main/ipc/handlers.ts,initPermissionApi(window, ...)was called inside a one-time initialization guard (if (!permissionApiInitialized)). This meant themainWindowreference was set exactly once on the first task start and never updated.When the
BrowserWindowwas recreated (e.g., macOS app reactivation after quit, or window close/reopen), the oldmainWindowreference pointed to a destroyed window. The Question API server's handler checksmainWindow.isDestroyed()and returns 503 when true.Fix
Move
initPermissionApi()outside the one-time guard so the window reference is refreshed on everytask:startcall, while keeping the HTTP server startup (startPermissionApiServer/startQuestionApiServer) as a one-time operation.How to test
This contribution was developed with AI assistance (Claude Code).
Summary by CodeRabbit