Skip to content

feat(fork): optimistic sidebar entry + Fork | prefix for forked tasks#4353

Merged
waleedlatif1 merged 2 commits intostagingfrom
improvement/fork
Apr 30, 2026
Merged

feat(fork): optimistic sidebar entry + Fork | prefix for forked tasks#4353
waleedlatif1 merged 2 commits intostagingfrom
improvement/fork

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Forked task now appears in sidebar immediately — optimistic setQueryData in useForkTask.onSuccess writes the new entry to the cache before the background refetch arrives, so navigation lands on a sidebar that already shows the fork
  • Renamed forked task title format to Fork | Chat Name (prefix instead of suffix) for faster scannability; re-forking a fork strips the prefix first to avoid Fork | Fork | ...
  • Scoped useForkTask list invalidation to the current workspace (taskKeys.list(workspaceId)) instead of all workspaces
  • Removed mutation objects from useCallback deps in sidebar (deleteTaskMutation, deleteTasksMutation, markTaskReadMutation, markTaskUnreadMutation) — .mutate() is stable in TanStack Query v5

Type of Change

  • Bug fix
  • Improvement

Testing

Tested manually

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Adds the forked task to the sidebar immediately by writing an optimistic
entry into the task list cache in onSuccess, before the background refetch
arrives. Without this, the sidebar showed no new task until the invalidation
refetch completed after navigation.

Also moves the Fork prefix to the front of the name ('Fork | Chat Name')
so it is immediately scannable in the sidebar.
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 30, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Apr 30, 2026 3:32am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented Apr 30, 2026

PR Summary

Medium Risk
Touches both server-side fork metadata and client-side React Query cache updates, so bugs could cause incorrect task titles or sidebar list inconsistencies (especially across workspaces) until refetch.

Overview
Forked tasks are now titled with a Fork | prefix (instead of a trailing | Fork), and re-forking strips an existing prefix to avoid repeated Fork | Fork | ....

On the client, useForkTask now performs an optimistic update: after a successful fork it cancels the current workspace’s task-list query and inserts the new forked task into the cached sidebar list immediately, then triggers a scoped refetch for that workspace.

Reviewed by Cursor Bugbot for commit b4de030. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 30, 2026

Greptile Summary

This PR improves the fork UX in two ways: it changes the forked-task naming convention from a suffix (Chat Name | Fork) to a prefix (Fork | Chat Name) with re-fork deduplication, and it adds an optimistic setQueryData write in useForkTask.onSuccess so the new entry appears in the sidebar immediately — before the background invalidation refetch arrives. The previously flagged missing cancelQueries call (needed to prevent a stale in-flight refetch from overwriting the optimistic entry) has been fixed in this revision. No P0/P1 issues remain.

Confidence Score: 5/5

Safe to merge — no P0/P1 issues found; the previously flagged race condition was fixed in this revision.

Both changed files have focused, consistent changes. The optimistic write correctly populates all required TaskMetadata fields, cancelQueries is called before setQueryData, and the regex patterns in route.ts and tasks.ts are in sync. No error paths or security concerns introduced.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts Title format flipped from suffix to prefix (`Fork
apps/sim/hooks/queries/tasks.ts Adds onSuccess optimistic cache write with cancelQueries guard; scopes onSettled invalidation to the current workspace; all five required TaskMetadata fields are correctly populated.

Sequence Diagram

sequenceDiagram
    participant UI as Sidebar UI
    participant Hook as useForkTask
    participant QC as QueryClient Cache
    participant API as POST /fork
    participant DB as Database

    UI->>Hook: mutate({ chatId, upToMessageId })
    Hook->>API: POST /api/mothership/chats/{chatId}/fork
    API->>DB: INSERT copilotChats (title = "Fork | {name}")
    DB-->>API: { id: newId }
    API-->>Hook: { success: true, id: newId }

    Note over Hook,QC: onSuccess
    Hook->>QC: cancelQueries(taskKeys.list(workspaceId))
    Hook->>QC: getQueryData → existing[]
    Hook->>QC: setQueryData([optimisticTask, ...existing])
    QC-->>UI: Sidebar re-renders with fork entry

    Note over Hook,QC: onSettled
    Hook->>QC: invalidateQueries(taskKeys.list(workspaceId))
    QC->>API: GET /tasks (background refetch)
    API-->>QC: Full task list (includes real fork row)
    QC-->>UI: Sidebar re-renders with server data
Loading

Reviews (2): Last reviewed commit: "fix(fork): cancel in-flight queries befo..." | Re-trigger Greptile

Comment thread apps/sim/hooks/queries/tasks.ts Outdated
@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1
Copy link
Copy Markdown
Collaborator Author

@cursor review

Copy link
Copy Markdown

@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.

Fix All in Cursor

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

Reviewed by Cursor Bugbot for commit b4de030. Configure here.

Comment thread apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts
@waleedlatif1 waleedlatif1 merged commit cb43b59 into staging Apr 30, 2026
14 checks passed
@waleedlatif1 waleedlatif1 deleted the improvement/fork branch April 30, 2026 05:40
waleedlatif1 added a commit that referenced this pull request Apr 30, 2026
…#4353)

* feat(fork): optimistic sidebar entry + rename to 'Fork | Chat Name'

Adds the forked task to the sidebar immediately by writing an optimistic
entry into the task list cache in onSuccess, before the background refetch
arrives. Without this, the sidebar showed no new task until the invalidation
refetch completed after navigation.

Also moves the Fork prefix to the front of the name ('Fork | Chat Name')
so it is immediately scannable in the sidebar.

* fix(fork): cancel in-flight queries before optimistic sidebar write
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