GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — improvement(mothership): streaming state transitions by icecrasher321 · Pull Request #4439 · simstudioai/sim

Conversation

@icecrasher321

@icecrasher321 icecrasher321 commented May 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Improve streaming state transitions and fix issues like switching tabs leading to odd stream UIs.

Type of Change

  • Bug fix
  • Other: Code quality

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
All reactions
  • 👍 1 reaction
Vikhyath Mondreti added 2 commits May 4, 2026 16:18
Vikhyath Mondreti
improvement(mothership): improve streaming state transitions
79b3dd6
Vikhyath Mondreti
Merge branch 'staging' into dev
f6ab790
@vercel

vercel Bot commented May 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 4, 2026 11:37pm

Request Review

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@cursor

cursor Bot commented May 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

PR Summary

Medium Risk
Touches chat streaming stop/reconnect, queued-message handoff, and cache invalidation logic; regressions could cause stuck UI, dropped queued messages, or missed task refreshes under race conditions. Changes are bounded to mothership/copilot streaming paths but are behaviorally complex and timing-sensitive.

Overview
Propagates streamId through task status pubsub/SSE (taskPubSub, mothership events, copilot stop/post) so clients can correlate started/completed events to a specific stream.

Hardens use-chat streaming state transitions: adds explicit timeouts/abort-signal utilities, prevents duplicate cursor processing, supports resolving chatId from stream batches, recovers active streams on pageshow/visibility/online, and makes queued-message handoff robust with TTL/claims/backoff and a dedicated queued-handoff stop mode.

Updates task-event handling to invalidate/remove the right React Query caches (including detail on relevant events) while avoiding detail invalidation when a status event is stale relative to an optimistic/newer active stream; expands tests to cover these race cases.

Reviewed by Cursor Bugbot for commit b8d76f9. Configure here.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@icecrasher321 icecrasher321 changed the title improvement(mothership): improve streaming state transitions improvement(mothership): streaming state transitions May 4, 2026
@vercel
vercel Bot deployed to Preview May 4, 2026 23:26
View deployment
cursor[bot]
cursor Bot reviewed May 4, 2026
View reviewed changes
Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

@greptile-apps

greptile-apps Bot commented May 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR improves streaming state transitions in the mothership chat UI by: (1) adding streamId correlation to all task status events so clients can make smarter cache-invalidation decisions without spurious refetches, and (2) introducing a visibility/online recovery mechanism that resumes an active SSE stream when the user returns to a tab, fixing odd UI state caused by the stream stalling while the tab was hidden.

Supporting infrastructure includes combineAbortSignals, sleepWithAbort, resolveChatIdForStream, owner-tagged handoff claims, a queued-handoff stop mode, and targetChatId propagation through all finalize/reconnect code paths.

Confidence Score: 5/5

Safe to merge — no P0/P1 issues found; two P2 observations on abort-signal hygiene.

All findings are P2. The resolveChatIdForStream inside stopBarrier is bounded by its own internal 10 s timeout so it cannot hang indefinitely; the combineAbortSignals cleanup-listener ordering issue is a minor GC concern with no observable data-loss path. Core logic for stream recovery, cache invalidation, and cross-tab handoff is correct and well-tested.

use-chat.ts — the stopBarrier in stopGeneration (queued-handoff branch) and combineAbortSignals helper are worth a second read for abort-hygiene completeness.

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Major refactor: adds stream recovery on tab-return (visibility/online events), owner-tagged handoff claims, queued-handoff stop mode, combineAbortSignals/sleepWithAbort primitives, resolveChatIdForStream, adoptResolvedChatId, and propagates targetChatId through all finalize paths. Core logic is sound; one noteworthy gap in the queued-handoff stop barrier.
apps/sim/hooks/use-task-events.ts Adds smart per-detail cache invalidation for started/completed events keyed by streamId; removes detail cache for deleted events; uses getLiveAssistantMessageId to detect locally-optimistic active streams and avoid spurious refetches.
apps/sim/hooks/use-task-events.test.ts Comprehensive test coverage for all new invalidation decision branches; covers deleted, started, completed, renamed, and unknown event types with various cached-state permutations.
apps/sim/lib/mothership/inbox/executor.ts Moves generateId() for userMessageId earlier so streamId can be included in both started and completed status events; no semantic change to execution logic.
apps/sim/lib/copilot/chat/post.ts Adds streamId: userMessageId to started, completed, and error-path completed events so clients can correlate status events to specific streams.
apps/sim/app/api/mothership/events/route.ts Conditionally forwards streamId from the task status event to the SSE payload so browser clients receive it.
apps/sim/app/api/copilot/chat/stop/route.ts Passes streamId through to the completed status event on successful stop; straightforward one-line addition.
apps/sim/app/api/copilot/chat/stream/route.ts Includes chatId in the resume response body when the run has an associated chatId, so clients can resolve the chatId from a batch poll without an extra request.
apps/sim/lib/copilot/tasks.ts Adds optional streamId field to TaskStatusEvent interface to carry per-stream correlation data in pubsub events.

Sequence Diagram

sequenceDiagram
    participant UI as Browser Tab
    participant useChat as useChat hook
    participant SSE as SSE Stream
    participant Server as Server (API)
    participant Redis as Redis/Pub-Sub
    participant OtherTab as Other Tab (sessionStorage)

    Note over UI,OtherTab: Normal streaming flow
    UI->>useChat: sendMessage()
    useChat->>Server: POST /stream
    Server-->>SSE: stream events
    SSE-->>useChat: processSSEStream (live)
    Server->>Redis: publishStatus(started, streamId)
    Redis-->>useChat: task_status SSE (streamId)
    useChat->>useChat: shouldSkipDetailInvalidation?

    Note over UI,OtherTab: Tab switch / visibility recovery
    UI->>useChat: visibilitychange / pageshow / online
    useChat->>useChat: recoverActiveStreamFromRedis()
    useChat->>useChat: cancelActiveStreamReader()
    useChat->>Server: fetchStreamBatch(afterCursor)
    Server-->>useChat: batch events + chatId
    useChat->>SSE: attachToExistingStream (resume)
    SSE-->>useChat: processSSEStream (resume)

    Note over UI,OtherTab: Queued message during active stream
    UI->>useChat: sendMessage() [while streaming]
    useChat->>useChat: createQueuedMessage(+handoff)
    useChat->>useChat: stopGeneration(queued-handoff)
    useChat->>Server: POST /abort (streamId)
    alt chatId unknown
        useChat->>Server: resolveChatIdForStream (poll)
        Server-->>useChat: chatId resolved
    end
    Server->>Redis: publishStatus(completed, streamId)
    Redis-->>useChat: task_status SSE
    useChat->>useChat: shouldSkipDetailInvalidation?
    useChat->>Server: POST /stream (new message)

    Note over UI,OtherTab: Cross-tab handoff recovery
    UI->>OtherTab: writeQueuedSendHandoffState (sessionStorage)
    OtherTab->>OtherTab: writeQueuedSendHandoffClaim (ownerId)
    OtherTab->>useChat: resolveChatIdForStream
    OtherTab->>Server: POST /stream (recovered message)
Loading

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

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

greptile-apps[bot]
greptile-apps Bot reviewed May 4, 2026
View reviewed changes
Comment thread apps/sim/hooks/use-task-events.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Vikhyath Mondreti
address comments
b8d76f9
@vercel
vercel Bot temporarily deployed to Preview May 4, 2026 23:37 Inactive
@icecrasher321

icecrasher321 commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

bugbot run

cursor[bot] reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@icecrasher321

icecrasher321 commented May 4, 2026

Copy link
Copy Markdown
Contributor Author

@greptile

greptile-apps[bot] reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

cursor[bot]
cursor Bot reviewed May 4, 2026
View reviewed changes

@cursor cursor Bot left a comment

Copy link
Copy Markdown

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 b8d76f9. Configure here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@icecrasher321
icecrasher321 merged commit 9eeb1b2 into staging May 4, 2026
26 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@waleedlatif1 waleedlatif1 mentioned this pull request May 5, 2026
v0.6.66: child trace spans, reranker controls, attachment previews, logs block, parallel-in-loop wall clock, gpt-image-2 #4444
Merged

This branch was previously deployed

1 inactive deployment
Preview — b8d76f91 Deployed May 4, 2026 by vercel[bot]
View deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@greptile-apps greptile-apps[bot] greptile-apps[bot] left review comments
@cursor cursor[bot] cursor[bot] left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

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.

1 participant

@icecrasher321
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.