Skip to content

fix(mothership): chat stream structuring + logs resource post fix - #4189

Merged
icecrasher321 merged 4 commits into
stagingfrom
fix/chat-stream-bugs
Apr 15, 2026
Merged

fix(mothership): chat stream structuring + logs resource post fix#4189
icecrasher321 merged 4 commits into
stagingfrom
fix/chat-stream-bugs

Conversation

@icecrasher321

@icecrasher321icecrasher321 commented Apr 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

Clean up chat streaming order of ops to make react query client refetch redis first when conversation active. Also logs resource when open left chat hanging.

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

vercelBot commented Apr 15, 2026

Copy link
Copy Markdown

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

1 Skipped Deployment
ProjectDeploymentActionsUpdated (UTC)
docsSkippedSkippedApr 15, 2026 11:34pm

Request Review

@cursor

cursorBot commented Apr 15, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches core chat streaming/stop behavior and client cache synchronization; regressions could surface as missing/duplicated messages or stuck queued sends. Changes are covered by new unit tests for transcript synthesis and stop-route behavior.

Overview
Chat fetch endpoints now return an effective transcript by normalizing persisted messages and synthesizing the in-flight assistant response from Redis stream snapshots (buildEffectiveChatTranscript), avoiding missing/duplicated tails while a conversation is active.

The home useChat streaming flow is refactored to write live SSE updates into the React Query chat detail cache (with a pendingMessages fallback) and introduces a sessionStorage-backed queued-send handoff so queued messages survive stop/reload/navigate scenarios without getting stuck.

Stopping a chat stream now always clears conversationId and can append a synthesized cancelled assistant message (with stable generateId IDs and stopped/complete:cancelled blocks), plus adds shared tooling for interpreting cancelled tool outcomes and expands allowed resource attachment types (incl. log/task/generic).

Reviewed by Cursor Bugbot for commit c956577. Configure here.

@greptile-apps

greptile-appsBot commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes two related chat streaming bugs: (1) when a chat is active, the API now returns an effective transcript that splices the live Redis-backed stream snapshot into the persisted message list via buildEffectiveChatTranscript, so the client always gets an up-to-date view on refetch; and (2) the stop route now unconditionally appends a cancelled assistant message (with a synthesized { type: 'stopped' } block) regardless of whether partial content was accumulated.

A significant client-side refactor moves streaming updates from a local setMessages state into the React Query cache via a new upsertTaskChatHistory helper, aligning the server and client views. handleTaskStatusEvent also drops the now-redundant detail-query invalidation on SSE completion, relying on the finalize path instead. Finally, task, log, and generic resource types are added to the chat-post schema, fixing the "log resource left chat hanging" regression.

Confidence Score: 5/5

Safe to merge — no P0/P1 issues found across any changed file.

All findings are P2 or lower. The core logic in buildEffectiveChatTranscript, the stop-route cancelled-block synthesis, and the React Query cache migration are all verifiably correct after tracing through normalizeMessage. New test files provide solid coverage for the new modules. The handleTaskStatusEvent narrowing (list-only, workspace-scoped) is safe because the workspaceId guard in useTaskEvents ensures the function is always called with a defined string.

No files require special attention.

Important Files Changed

FilenameOverview
apps/sim/lib/copilot/chat/effective-transcript.tsNew module that builds an effective chat transcript by replaying Redis stream events onto persisted messages; well-guarded (only appends live assistant if trailing message is the active user turn).
apps/sim/lib/copilot/chat/effective-transcript.test.tsNew test file with six cases covering the key branches of buildEffectiveChatTranscript: already-persisted assistant, empty stream, text stream, thinking-only, tool cancellation, and cancelled tail.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.tsMajor refactor: streaming flush now writes to the React Query cache via upsertTaskChatHistory instead of local state; messages is derived from chatHistory rather than a standalone useState; queued-send handoff mechanism added for cross-navigation recovery.
apps/sim/app/api/copilot/chat/stop/route.tsStop route now always appends a cancelled assistant message (synthesizes { type: 'stopped' } block) instead of silently no-oping when content is empty; also replaces crypto.randomUUID() with generateId().
apps/sim/app/api/copilot/chat/stop/route.test.tsNew test file covering auth, missing-chat, and empty-content stop scenarios; verifies the cancelled block shape stored in the DB.
apps/sim/app/api/copilot/chat/queries.tsGET handler now builds an effective transcript from the stream snapshot before returning, so refetches during an active stream include the live assistant tail.
apps/sim/app/api/mothership/chats/[chatId]/route.tsSame effective-transcript treatment applied to the mothership chat GET endpoint; replaces the raw messages array with the built transcript.
apps/sim/hooks/use-task-events.tshandleTaskStatusEvent now takes a workspaceId and invalidates only the workspace-specific list key; removes the previously redundant detail-query invalidation on completion.
apps/sim/lib/copilot/chat/post.tsAdds task, log, and generic to the ResourceAttachmentSchema enum; fixes the "log resource left chat hanging" validation rejection.
apps/sim/lib/copilot/chat/stream-tool-outcome.tsNew utility that factors out the tool-outcome resolution logic (user_cancelled detection, success/error fallback) used in both effective-transcript.ts and use-chat.ts.

Sequence Diagram

sequenceDiagram
participant C as Client (useChat)
participant RQ as React Query Cache
participant API as API Route
participant Redis as Redis (stream snapshot)
participant DB as Database
C->>RQ: cancelQueries(detail)
C->>RQ: upsertTaskChatHistory (optimistic user + assistant)
C->>API: POST /api/mothership/chat (send message)
API-->>C: SSE stream begins
loop During streaming
C->>RQ: upsertTaskChatHistory (flush: update assistant snapshot)
end
API->>Redis: write stream events
API->>DB: persist final messages
API-->>C: stream complete
C->>C: finalize() → invalidateChatQueries
C->>API: GET /api/mothership/chats/{chatId}
API->>DB: fetch chat + conversationId
API->>Redis: fetch streamSnapshot
API->>API: buildEffectiveChatTranscript(messages, snapshot)
API-->>C: effective transcript (persisted + live tail)
C->>RQ: update cache with server response
Note over C,RQ: SSE task_status → only list key invalidated (not detail)
Loading

Reviews (2): Last reviewed commit: "address comments" | Re-trigger Greptile

Comment threadapps/sim/app/api/copilot/chat/stop/route.ts Outdated
Comment threadapps/sim/lib/copilot/chat/effective-transcript.ts Outdated
Comment threadapps/sim/app/api/copilot/chat/stop/route.ts Outdated
Comment threadapps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
@icecrasher321

Copy link
Copy Markdown
CollaboratorAuthor

bugbot run

@icecrasher321

Copy link
Copy Markdown
CollaboratorAuthor

@greptile

@cursorcursorBot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit c956577. Configure here.

@icecrasher321
icecrasher321 merged commit 377712c into stagingApr 15, 2026
14 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/chat-stream-bugs branch April 16, 2026 20:05
Sign up for freeto 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

@icecrasher321