Skip to content

fix: update window reference on every task start to prevent Question API 503#683

Open
mvanhorn wants to merge 1 commit intoaccomplish-ai:mainfrom
mvanhorn:osc/679-fix-ask-user-question-503
Open

fix: update window reference on every task start to prevent Question API 503#683
mvanhorn wants to merge 1 commit intoaccomplish-ai:mainfrom
mvanhorn:osc/679-fix-ask-user-question-503

Conversation

@mvanhorn
Copy link
Contributor

@mvanhorn mvanhorn commented Mar 10, 2026

Fixes #679

Summary

The AskUserQuestion MCP tool was returning a 503: Question API not initialized error because the mainWindow reference 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 the mainWindow reference was set exactly once on the first task start and never updated.

When the BrowserWindow was recreated (e.g., macOS app reactivation after quit, or window close/reopen), the old mainWindow reference pointed to a destroyed window. The Question API server's handler checks mainWindow.isDestroyed() and returns 503 when true.

Fix

Move initPermissionApi() outside the one-time guard so the window reference is refreshed on every task:start call, while keeping the HTTP server startup (startPermissionApiServer / startQuestionApiServer) as a one-time operation.

-    if (!permissionApiInitialized) {
-      initPermissionApi(window, () => taskManager.getActiveTaskId());
+    initPermissionApi(window, () => taskManager.getActiveTaskId());
+
+    if (!permissionApiInitialized) {
       startPermissionApiServer();
       startQuestionApiServer();
       permissionApiInitialized = true;
     }

How to test

  1. Start a task, verify AskUserQuestion works
  2. Close and reopen the app window (or trigger macOS reactivation)
  3. Start a new task that calls AskUserQuestion
  4. Should get a valid response instead of 503

This contribution was developed with AI assistance (Claude Code).

Summary by CodeRabbit

  • Bug Fixes
    • Fixed permission and question API window references to remain current after window recreation.

…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>
@orcaman
Copy link
Contributor

orcaman commented Mar 10, 2026

Snyk checks have passed. No issues have been found so far.

Status Scanner Critical High Medium Low Total (0)
Open Source Security 0 0 0 0 0 issues
Licenses 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@coderabbitai
Copy link

coderabbitai bot commented Mar 10, 2026

📝 Walkthrough

Walkthrough

The 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

Cohort / File(s) Summary
Permission API Reinitialization
apps/desktop/src/main/ipc/handlers.ts
Added unconditional call to initPermissionApi(window, () => taskManager.getActiveTaskId()) before the initialization guard to update window references after potential window recreation, fixing the Question API initialization failure.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested reviewers

  • galElmalah

Poem

🐰 A window's soul was lost to time,
The Question API couldn't climb.
Now always fresh, no longer late,
We reinit to seal its fate!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: updating the window reference on every task start to fix the Question API 503 error.
Linked Issues check ✅ Passed The code change directly addresses issue #679 by moving initPermissionApi() outside the one-time guard to refresh the BrowserWindow reference on each task start, fixing the 503 error.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the Question API 503 issue by updating window references; no unrelated modifications are present.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
apps/desktop/src/main/ipc/handlers.ts (1)

306-365: Consider adding initPermissionApi to session:resume as well.

The session:resume handler also receives a fresh window reference and starts tasks that may use the permission/question APIs. If a user reopens the app window and resumes a session (without first calling task: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

📥 Commits

Reviewing files that changed from the base of the PR and between 6a5a2f7 and 2ef2f3e.

📒 Files selected for processing (1)
  • apps/desktop/src/main/ipc/handlers.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] AskUserQuestion MCP tool returns 503 - Question API not initialized

2 participants