Skip to content

fix(refetch): task sse terminal event removed and auto-refetch behaviour disabled#4167

Open
icecrasher321 wants to merge 2 commits intostagingfrom
fix/refetch-ordering
Open

fix(refetch): task sse terminal event removed and auto-refetch behaviour disabled#4167
icecrasher321 wants to merge 2 commits intostagingfrom
fix/refetch-ordering

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

Far less likely case of db write failing is considered acceptable to not auto-refetch stale state pre persistence.

Type of Change

  • Bug fix

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)

@vercel
Copy link
Copy Markdown

vercel bot commented Apr 14, 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 14, 2026 10:13pm

Request Review

@cursor
Copy link
Copy Markdown

cursor bot commented Apr 14, 2026

PR Summary

Medium Risk
Changes task update signaling from per-chat status payloads to coarse workspace-level list invalidations, which may affect UI freshness/performance and could miss detail refreshes in edge cases.

Overview
Unifies task SSE notifications around “workspace task list changed” events. Server-side publishers across chat create/rename/delete/stop and stream lifecycle now call taskPubSub.publishTaskListChanged({ workspaceId }) and stop sending chatId/type payloads.

The mothership SSE endpoint subscribes via onTaskListChanged and emits empty task_status events, and the client useTaskEvents handler is simplified to always invalidate taskKeys.lists() (no payload parsing or chat-detail invalidation). Separately, use-chat refactors invalidation into invalidateTaskHistory vs invalidateTaskList, and avoids auto-refetching active chat history on finalize (refetchType: 'none'), updating tests/mocks accordingly.

Reviewed by Cursor Bugbot for commit b28b2a3. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps bot commented Apr 14, 2026

Greptile Summary

This PR simplifies the task event pub/sub system by replacing granular TaskStatusEvent payloads (with chatId, type) with a single workspace-scoped TaskListChangedEvent. The core behavioral fix is in finalize inside use-chat.ts: the detail query is now marked stale with refetchType: 'none' rather than triggering an immediate refetch, which prevents the client from racing the server-side DB write to completion.

Confidence Score: 5/5

Safe to merge — the simplification is consistent across all publishers and the single subscriber, and the refetchType:'none' fix correctly addresses the DB-write race condition.

All callers of the old publishStatusChanged API are updated, tests cover the new behaviour, and the intentional asymmetry between finalize (refetchType:'none') and stop (refetchType:'active') is correctly implemented. No P0/P1 findings.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/tasks.ts Renamed pub/sub interface from TaskStatusEvent to TaskListChangedEvent, dropping chatId and type fields; renamed publish/subscribe methods accordingly. Redis channel identifier unchanged.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Split invalidation into invalidateTaskHistory (supports refetchType) and invalidateTaskList; finalize now marks detail cache stale with refetchType:'none', while the stop path retains active refetch via invalidateChatQueries.
apps/sim/hooks/use-task-events.ts Simplified handleTaskStatusEvent to only invalidate taskKeys.lists(); removed payload parsing and completed-event detail invalidation. logger is still used for SSE onerror.
apps/sim/app/api/mothership/events/route.ts SSE endpoint now emits an empty {} payload per task_status event; workspace filtering remains on the server side before the send.
apps/sim/lib/copilot/chat/post.ts All publishStatusChanged calls replaced with publishTaskListChanged with simplified payload across persistUserMessage, buildOnComplete, and buildOnError.
apps/sim/lib/mothership/inbox/executor.ts All publishStatusChanged calls migrated to publishTaskListChanged consistently.
apps/sim/hooks/use-task-events.test.ts Tests updated to match the new no-arg handleTaskStatusEvent signature; prior payload-parsing and completed-detail tests removed as expected.

Sequence Diagram

sequenceDiagram
    participant Client as Browser (use-chat.ts)
    participant API as API Route
    participant PubSub as taskPubSub (Redis)
    participant SSE as SSE Endpoint
    participant RQ as React Query Cache

    Note over Client,RQ: Normal stream completion (finalize)
    Client->>RQ: invalidateTaskHistory('none') stale, no refetch
    Client->>RQ: invalidateTaskList()
    API->>PubSub: publishTaskListChanged({workspaceId})
    PubSub-->>SSE: task:status_changed event
    SSE-->>Client: task_status {}
    Client->>RQ: invalidateQueries(taskKeys.lists())

    Note over Client,RQ: Manual stop (stopChat)
    Client->>API: POST /api/copilot/chat/stop
    API->>PubSub: publishTaskListChanged({workspaceId})
    API-->>Client: 200 OK
    Client->>RQ: invalidateChatQueries() - invalidateTaskHistory('active') + invalidateTaskList()
    PubSub-->>SSE: task:status_changed event
    SSE-->>Client: task_status {}
    Client->>RQ: invalidateQueries(taskKeys.lists())
Loading

Reviews (1): Last reviewed commit: "simplify event" | Re-trigger Greptile

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