Skip to content

fix(sidebar): preserve task project during create flow#1319

Merged
rabanspiegel merged 4 commits intomainfrom
emdash/feat-12ccan-dev-version-9j0
Mar 6, 2026
Merged

fix(sidebar): preserve task project during create flow#1319
rabanspiegel merged 4 commits intomainfrom
emdash/feat-12ccan-dev-version-9j0

Conversation

@rabanspiegel
Copy link
Contributor

@rabanspiegel rabanspiegel commented Mar 6, 2026

Summary

  • preserve the clicked sidebar project in a ref before opening the create-task modal
  • use that pending project during submission so task creation does not depend on selectedProject updating first
  • prevent the dev-mode sidebar '+' flow from closing the modal without creating a task

Validation

  • pnpm run format
  • pnpm run lint
  • pnpm run type-check
  • pnpm exec vitest run

Note

Medium Risk
Touches the task creation flow by decoupling it from selectedProject timing; mistakes here could create tasks under the wrong project or leave the UI in an inconsistent state, but the change is small and localized to the modal/create path.

Overview
Fixes a race in the sidebar “create task” flow by capturing the clicked project in a pendingTaskProjectRef before navigating/opening the modal, and then using that project when handleCreateTask submits.

Also ensures the pending project is cleared after submission and when the taskModal closes (via showModal(..., { onClose })), preventing stale project state from leaking into later task creations.

Written by Cursor Bugbot for commit 4475cd4. This will update automatically on new commits. Configure here.

Ensure sidebar task creation uses the intended project even before selectedProject state catches up.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
@vercel
Copy link

vercel bot commented Mar 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
docs Ready Ready Preview, Comment Mar 6, 2026 2:26am

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 6, 2026

Greptile Summary

This PR fixes a race condition in the sidebar task-creation flow where selectedProject state had not yet updated by the time handleCreateTask was called, causing tasks to be assigned to the wrong project. The fix stores the clicked sidebar project synchronously in a pendingTaskProjectRef before triggering any state updates, then consumes and clears it on submission.

Key changes:

  • Added pendingTaskProjectRef = useRef<Project | null>(null) to capture the intended project before async state propagation
  • handleStartCreateTaskFromSidebar now sets the ref before calling activateProjectView and openTaskModal
  • handleCreateTask reads the ref first (pendingTaskProjectRef.current || selectedProject) and immediately clears it, so all other task-creation paths continue to use selectedProject as before

Issue found:

  • The ref is only cleared inside handleCreateTask (the success path). If the modal is dismissed without submitting, pendingTaskProjectRef.current remains set. Any subsequent modal open via handleNewTask, autoOpenTaskModalTrigger, or a keyboard shortcut will silently create the task under the stale sidebar project rather than the currently selected one. An onClose callback should be added to openTaskModalImplRef.current to clear the ref on dismissal.

Confidence Score: 3/5

  • The core race-condition fix is sound, but a missing cleanup path on modal dismissal can cause tasks to be created under the wrong project in follow-up flows.
  • The change is small and well-targeted, but the uncleared ref on modal dismiss is a real logic bug that silently misbehaves rather than erroring — making it easy to miss in manual testing. The bug only surfaces after a specific sequence of user actions (sidebar open → cancel → switch project → open modal again), which likely wasn't covered by existing tests.
  • src/renderer/hooks/useTaskManagement.ts — specifically the openTaskModalImplRef.current assignment block (lines 951–967) needs an onClose handler to clear pendingTaskProjectRef.

Important Files Changed

Filename Overview
src/renderer/hooks/useTaskManagement.ts Introduces pendingTaskProjectRef to fix a race condition between sidebar project selection and task creation; however, the ref is not cleared on modal dismissal, which can cause tasks to be created under the wrong project in subsequent non-sidebar modal flows.

Sequence Diagram

sequenceDiagram
    participant User
    participant Sidebar
    participant useTaskManagement
    participant pendingTaskProjectRef
    participant ModalProvider
    participant handleCreateTask

    User->>Sidebar: Click '+' on Project A
    Sidebar->>useTaskManagement: handleStartCreateTaskFromSidebar(ProjectA)
    useTaskManagement->>pendingTaskProjectRef: set(ProjectA)
    useTaskManagement->>useTaskManagement: activateProjectView(ProjectA)
    useTaskManagement->>ModalProvider: openTaskModal()

    alt User submits the modal
        ModalProvider-->>handleCreateTask: onSuccess(result)
        handleCreateTask->>pendingTaskProjectRef: read → ProjectA
        handleCreateTask->>pendingTaskProjectRef: clear (set null) ✅
        handleCreateTask->>useTaskManagement: createTaskMutation(ProjectA)
    else User dismisses the modal (bug path)
        ModalProvider-->>useTaskManagement: onClose() — no handler wired
        Note over pendingTaskProjectRef: ref still holds ProjectA ⚠️
        User->>useTaskManagement: opens modal via handleNewTask (ProjectB selected)
        ModalProvider-->>handleCreateTask: onSuccess(result)
        handleCreateTask->>pendingTaskProjectRef: read → ProjectA (stale!)
        handleCreateTask->>useTaskManagement: createTaskMutation(ProjectA) ❌ wrong project
    end
Loading

Comments Outside Diff (1)

  1. src/renderer/hooks/useTaskManagement.ts, line 951-967 (link)

    Stale pendingTaskProjectRef when modal is dismissed

    pendingTaskProjectRef.current is set in handleStartCreateTaskFromSidebar and only cleared inside handleCreateTask (on line 915). However, if the user opens the modal via the sidebar and then dismisses it (without submitting), the ref is never cleared. ModalProvider supports an optional onClose callback, but none is provided here.

    This creates the following bug scenario:

    1. User clicks sidebar '+' for Project ApendingTaskProjectRef.current = ProjectA
    2. User dismisses the modal (presses Escape or clicks ✕) → onClose fires, modal closes, but ref is still ProjectA
    3. User switches to Project B (selectedProject is now ProjectB)
    4. User opens the modal through any other path (e.g. handleNewTask, keyboard shortcut, or autoOpenTaskModalTrigger)
    5. User submits → handleCreateTask reads pendingTaskProjectRef.current → task is silently created under Project A instead of Project B

    The fix is to pass an onClose callback that clears the ref:

    openTaskModalImplRef.current = () => {
      showModal('taskModal', {
        onSuccess: (result) =>
          handleCreateTask(
            result.name,
            result.initialPrompt,
            result.agentRuns,
            result.linkedLinearIssue ?? null,
            result.linkedGithubIssue ?? null,
            result.linkedJiraIssue ?? null,
            result.autoApprove,
            result.useWorktree,
            result.baseRef,
            result.nameGenerated
          ),
        onClose: () => {
          pendingTaskProjectRef.current = null;
        },
      });
    };
    

Last reviewed commit: 007d68b

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Clear the pending sidebar project when the create-task modal closes so later task creation flows use the current selection.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
…v-version-9j0

# Conflicts:
#	src/renderer/hooks/useTaskManagement.ts
@rabanspiegel rabanspiegel requested review from arnestrickmann and removed request for arnestrickmann March 6, 2026 02:32
@rabanspiegel rabanspiegel merged commit 87c324d into main Mar 6, 2026
5 checks passed
@rabanspiegel rabanspiegel deleted the emdash/feat-12ccan-dev-version-9j0 branch March 6, 2026 21:28
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.

1 participant