Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(md): file streaming patch preview#4465

Merged
icecrasher321 merged 2 commits intostagingsimstudioai/sim:stagingfrom
fix/md-streamingsimstudioai/sim:fix/md-streamingCopy head branch name to clipboard
May 6, 2026
Merged

fix(md): file streaming patch preview#4465
icecrasher321 merged 2 commits intostagingsimstudioai/sim:stagingfrom
fix/md-streamingsimstudioai/sim:fix/md-streamingCopy head branch name to clipboard

Conversation

@icecrasher321
Copy link
Copy Markdown
Collaborator

Summary

Fixes markdown file preview flicker during streamed workspace file edits by guarding preview activation, replay, reconnect, and cache handoff state.

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 May 6, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 6, 2026 3:38am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 6, 2026

PR Summary

Medium Risk
Touches core chat streaming/preview session state and resource activation behavior, which can cause regressions like incorrect tab switching or stale preview content if edge cases were missed. Changes are localized to preview-session handling and add tests to reduce risk.

Overview
Reduces file-preview flicker during streamed file edits by only rendering/activating streaming previews once a session has renderable content (via new hasRenderableFilePreviewContent).

Hardens preview session reconciliation by preventing replayed/stale streaming events from overwriting completed sessions (shouldReplaceSession), tracking which tab “owned” a preview to decide when auto-activation is allowed, and suppressing resource auto-selection during handoff from preview completion to persisted file resources.

On preview completion, seeds the workspace file-content query cache with the final preview text and tightens completion success checks before adding/activating the resulting file resource. Updates unit tests to cover renderability and session replacement rules.

Reviewed by Cursor Bugbot for commit 6f4bbe6. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 6, 2026

Greptile Summary

This PR guards markdown file preview activation, replay, reconnect, and cache handoff state to eliminate flicker during streamed workspace file edits. The core fix is adding hasRenderableFilePreviewContent as a gate so the preview panel and active-resource switching only fire once actual streamed content has arrived, rather than on the first lifecycle event.

  • hasRenderableFilePreviewContent is introduced as a shared predicate (previewText.length > 0 || previewVersion > 0) and threaded through ResourceContent, mothership-view, and use-chat to gate all activation, chrome-sync, and UI render paths.
  • shouldReplaceSession gains a guard that prevents replayed streaming events from overwriting a fully-completed session, fixing reconnect state regression.
  • Resource event handoff (completedPreviewResourceHandoffRef) coordinates between the file_preview_complete handler and the subsequent resource event so that activation is suppressed when the preview panel was already managing focus.

Confidence Score: 5/5

Safe to merge; the change is a targeted guard around preview activation that does not alter the underlying streaming or file-write logic.

All activation, cache-seed, and resource-handoff paths gate correctly on hasRenderableFilePreviewContent. The reconnect-replay guard in shouldReplaceSession is tested and closes a real regression. No data-loss or activation race was introduced in the changed paths.

The resource event handler in use-chat.ts grew significantly; reviewers should follow the completedPreviewResourceHandoffRef cleanup flow (both the stale-handoff branch at ~3011 and the current-handoff branch at ~3034) to verify nothing is leaked if a resource event arrives without a corresponding preview session.

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/home/hooks/use-file-preview-sessions.ts Adds hasRenderableFilePreviewContent predicate and a replay guard in shouldReplaceSession; both are well-tested and correct.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Large changeset introducing activation-owner tracking, handoff refs, cache seeding, and suppression logic; logic is sound but inline latestPreviewForResource and complex handoff cleanup increase maintenance surface.
apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx Correctly removes the stale previewSession && guard from the streaming-file branch and adds hasRenderableFilePreviewContent checks to both derived text values.
apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx Tightens shouldShowStreamingFilePanel to gate on hasRenderableFilePreviewContent; minimal and correct change.
apps/sim/app/workspace/[workspaceId]/home/hooks/use-file-preview-sessions.test.tsx New tests cover the pending-empty, version-gated renderable, and completed-session replay cases for the two new exported functions.

Sequence Diagram

sequenceDiagram
    participant Stream as SSE Stream
    participant Chat as use-chat handler
    participant Sessions as useFilePreviewSessions
    participant Resources as Resources state
    participant UI as ResourceContent

    Stream->>Chat: file_preview_start
    Chat->>Sessions: upsert(pending, v=0, text='')
    Note over UI: guard: hasRenderable=false - no activation

    Stream->>Chat: file_preview_target
    Chat->>Chat: rememberPreviewActivationOwner(session)
    Chat->>Sessions: upsert(baseSession)
    Chat->>Resources: syncPreviewResourceChrome (activate if autoActivate)

    Stream->>Chat: file_preview_content (first)
    Note over Chat: prevSession has no renderable content
    Chat->>Sessions: upsert(streaming, v=1, text='...')
    Chat->>Resources: syncPreviewResourceChrome - activate preview
    UI-->>UI: streamingPreviewText defined - render FileViewer

    Stream->>Chat: file_preview_content (subsequent)
    Note over Chat: prevSession already renderable - skip chrome sync
    Chat->>Sessions: upsert(streaming, v=N, text='...')

    Stream->>Chat: file_preview_complete (success=true, id matches)
    Chat->>Sessions: complete(session)
    Chat->>Resources: setResources (add real file, remove streaming-file)
    Chat->>Chat: completedPreviewResourceHandoffRef.set(fileId)
    Chat->>Chat: seedCompletedPreviewContentCache(fileId, previewText)

    Stream->>Chat: resource event (file added)
    Chat->>Chat: read completedPreviewHandoff - suppressActivation?
    alt suppress activation
        Chat->>Resources: setResources (add without activating)
    else normal
        Chat->>Resources: addResource - activate
    end
    Chat->>Chat: delete handoff ref (cleanup)
Loading

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

Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Outdated
Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
@icecrasher321
Copy link
Copy Markdown
Collaborator Author

bugbot run

@icecrasher321
Copy link
Copy Markdown
Collaborator Author

@greptile

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.

✅ 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 6f4bbe6. Configure here.

@icecrasher321 icecrasher321 merged commit 93c0202 into staging May 6, 2026
14 checks passed
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

Morty Proxy This is a proxified and sanitized view of the page, visit original site.