GeezerVison is showing github.com — fix(mothership): chat stream structuring + logs resource post fix by icecrasher321 · Pull Request #4189 · simstudioai/sim
Conversation
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
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)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
greptile-apps[bot] reacted with thumbs up emoji
vercel
Bot
commented
Apr 15, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
1 Skipped Deployment
| Project |
Deployment |
Actions |
Updated (UTC) |
| docs |
Skipped |
|
Apr 15, 2026 11:34pm |

|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
cursor
Bot
commented
Apr 15, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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
| Filename |
Overview |
| apps/sim/lib/copilot/chat/effective-transcript.ts |
New 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.ts |
New 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.ts |
Major 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.ts |
Stop 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.ts |
New 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.ts |
GET 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.ts |
Same 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.ts |
handleTaskStatusEvent 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.ts |
Adds 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.ts |
New 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
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
cursor[bot] reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
greptile-apps[bot] reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This branch was previously deployed
1 inactive
deployment
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|