Skip to content

Fix/mothership 3#3610

Merged
TheodoreSpeaks merged 4 commits intostagingfrom
fix/mothership-3
Mar 16, 2026
Merged

Fix/mothership 3#3610
TheodoreSpeaks merged 4 commits intostagingfrom
fix/mothership-3

Conversation

@TheodoreSpeaks
Copy link
Collaborator

Summary

Merge fixes for mothership

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation
  • Other: ___________

Testing

Tested each change individually

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)

Screenshots/Videos

Sg312 and others added 2 commits March 16, 2026 11:36
* Fix task switching causing stream to abort

* Process all task streams all the time

* Process task streams that are in the background

---------

Co-authored-by: Theodore Li <theo@sim.ai>
@cursor
Copy link

cursor bot commented Mar 16, 2026

You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace.

To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard.

@vercel
Copy link

vercel bot commented Mar 16, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped Mar 16, 2026 10:09pm

Request Review

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 16, 2026

Greptile Summary

This PR delivers a set of targeted fixes for the mothership chat/copilot flow: routing the build MCP agent through the main chat orchestrator, hardening deployment-status updates after deploy/redeploy tool calls, and patching stream-reconnection edge cases in use-chat.ts.

Key changes:

  • New handleBuildToolCall in the MCP copilot route delegates build-agent requests to orchestrateCopilotStream with commands: ['fast'], matching the Go-side ModeFast semantics rather than the subagent endpoint.
  • Deployment output fixes: executeRedeploy now accepts a params.workflowId override and emits isDeployed: true; executeDeployChat undeploy now uses isChatDeployed: false instead of isDeployed: false, fixing a prior bug where undeploy was incorrectly marking the workflow as deployed in the client registry.
  • Stricter SSE handler guard (typeof resultPayload?.isDeployed === 'boolean') and use of server-side deployedAt timestamp in client-sse/handlers.ts.
  • Cache invalidation in use-chat.ts: deployment query keys are invalidated whenever a deploy/redeploy tool call succeeds, keeping the UI consistent with the server state.
  • Stream-reconnection guard: an early-return path added for activeStreamId && !snapshot polls for the snapshot instead of proceeding with empty state; however the abort controller is no longer called on unmount, which may leave open SSE connections when navigating away.

Confidence Score: 3/5

  • Mostly safe to merge; contains a resource-leak risk (missing abort on unmount) and a misleading error message that could confuse downstream LLM consumers.
  • The deployment-output and SSE-handler fixes are correct and well-scoped. The two concerns holding back a higher score are: (1) removing abortControllerRef.current?.abort() from the cleanup effect leaves active SSE fetch connections open after unmount, which is a runtime resource leak; and (2) the misleading "workflowId is required" error in handleBuildToolCall when the real cause is an authorization failure could cause an LLM MCP client to take incorrect follow-up actions.
  • apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts (abort cleanup removal), apps/sim/app/api/mcp/copilot/route.ts (misleading auth error message)

Important Files Changed

Filename Overview
apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Three notable changes: (1) early-return polling guard when stream is active but snapshot is absent — functional but risks an unbounded invalidation loop; (2) deployment-status cache invalidation added for deploy/redeploy tool calls; (3) abort controller cleanup removed from unmount effect, potentially leaking open SSE connections.
apps/sim/app/api/mcp/copilot/route.ts New handleBuildToolCall function routes the 'build' agent through the main chat orchestrator instead of the subagent endpoint. Authorization logic contains a misleading error message when the workflow exists but the caller lacks permission.
apps/sim/lib/copilot/orchestrator/tool-executor/deployment-tools/deploy.ts executeRedeploy now accepts a params.workflowId override and adds isDeployed: true to its output. executeDeployChat undeploy uses isChatDeployed instead of isDeployed (fixing a prior bug where undeploy incorrectly set isDeployed=true in the registry), and both paths now include workflowId in output.
apps/sim/lib/copilot/client-sse/handlers.ts Deployment status hydration is now gated on typeof resultPayload?.isDeployed === 'boolean' (stricter than the old !== false default-true check) and honours the server's deployedAt timestamp. Clean and correct fix.
apps/sim/lib/copilot/orchestrator/tool-executor/index.ts Single-line change: redeploy handler now forwards params to executeRedeploy instead of ignoring them, enabling the workflowId override added in deploy.ts.
apps/sim/lib/copilot/orchestrator/sse/handlers/tool-execution.ts Trivial logging enhancement: tool execution output is now included in the success log entry for easier debugging.

Sequence Diagram

sequenceDiagram
    participant MCP as MCP Client
    participant Route as CopilotMcpRoute
    participant Auth as WorkflowAuthorizer
    participant Orch as ChatOrchestrator
    participant Tools as ToolHandlers
    participant QC as QueryCache

    MCP->>Route: tool call with agentId build
    Route->>Route: handleBuildToolCall
    alt workflowId provided
        Route->>Auth: check workspace permission
        Auth-->>Route: allowed or denied
    else no workflowId
        Route->>Route: resolve workflow for user
    end
    Route->>Orch: stream with fast mode and auto-execute
    Orch->>Tools: run each tool call in sequence
    Tools-->>Orch: tool results with deployment status
    Orch-->>Route: aggregated content and tool calls
    Route-->>MCP: JSON result

    Note over QC: After deploy tool succeeds
    QC->>QC: invalidate deployment info query
    QC->>QC: invalidate deployment versions query
    QC->>QC: invalidate workflow list query
Loading

Last reviewed commit: 7316482

Comment on lines +694 to +711
if (!resolved?.workflowId) {
return {
content: [
{
type: 'text',
text: JSON.stringify(
{
success: false,
error: 'workflowId is required for build. Call create_workflow first.',
},
null,
2
),
},
],
isError: true,
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Misleading error message when authorization fails

When a workflowId is supplied but authorizeWorkflowByWorkspacePermission returns allowed: false, resolved becomes null and the function returns the error "workflowId is required for build. Call create_workflow first." This message implies the caller forgot to provide a workflow ID, when the actual problem is a permissions failure. An LLM acting as an MCP client will likely misinterpret this and try creating a new workflow instead of flagging an access error.

Consider returning a distinct message for the authorization-failed path:

// in the IIFE block
return authorization.allowed ? { workflowId } : { error: 'not_authorized' }

…and checking resolved?.error separately from the missing-ID case.

@cursor
Copy link

cursor bot commented Mar 16, 2026

You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace.

To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard.

@cursor
Copy link

cursor bot commented Mar 16, 2026

You have used all Bugbot PR reviews included in your free trial for your GitHub account on this workspace.

To continue using Bugbot reviews, enable Bugbot for your team in the Cursor dashboard.

@TheodoreSpeaks TheodoreSpeaks merged commit b0870f4 into staging Mar 16, 2026
11 checks passed
@TheodoreSpeaks TheodoreSpeaks deleted the fix/mothership-3 branch March 16, 2026 22:13
emir-karabeg pushed a commit that referenced this pull request Mar 17, 2026
* Fix deploy subagent

* fix(stream) handle task switching   (#3609)

* Fix task switching causing stream to abort

* Process all task streams all the time

* Process task streams that are in the background

---------

Co-authored-by: Theodore Li <theo@sim.ai>

* Always return isDeployed for undeploy chat

* Fix lint

---------

Co-authored-by: Siddharth Ganesan <siddharthganesan@gmail.com>
Co-authored-by: Theodore Li <theo@sim.ai>
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.

2 participants