Skip to content

Feat/mship block stream#4626

Merged
Sg312 merged 6 commits into
stagingfrom
feat/mship-block-stream
May 16, 2026
Merged

Feat/mship block stream#4626
Sg312 merged 6 commits into
stagingfrom
feat/mship-block-stream

Conversation

@Sg312
Copy link
Copy Markdown
Collaborator

@Sg312 Sg312 commented May 16, 2026

Summary

Make mship block stream

Type of Change

  • Bug fix

Testing

Manual

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 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 May 16, 2026 1:48am

Request Review

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 16, 2026

Greptile Summary

This PR adds end-to-end NDJSON streaming support to the Mothership block execution path so long-running headless requests send heartbeats to prevent HTTP proxy timeouts, and assistant content chunks reach the client incrementally.

  • route.ts: A new wantsStreamedExecuteResponse guard branches the endpoint into a streaming path that emits an initial heartbeat, forwards text-channel events as chunk events, and closes with a final event. A shared buildExecuteResponsePayload helper deduplicates the response shape.
  • mothership-handler.ts: Always requests application/x-ndjson. readMothershipExecuteResponse consumes the NDJSON stream for the non-streaming path; createMothershipStreamingExecution pipes chunk events into a ReadableStream and mutates execution.output on the final event for the streaming path.
  • trace-spans.ts: hasUnhandledError now short-circuits for successful mothership spans, preventing contained tool-call failures from bubbling up to mark the workflow as errored.

Confidence Score: 5/5

Safe to merge — the streaming and non-streaming paths are cleanly separated, abort/cleanup handling is symmetric, and the previously flagged reader-lock gap is fixed.

Both the streaming and non-streaming branches have consistent error handling and resource cleanup. The reader lock is properly released via try/finally in both readMothershipExecuteResponse and createMothershipStreamingExecution. Abort signals thread correctly through the new lifecycleAbortController and stream cancel callbacks. Tests cover the heartbeat, chunk, error, and cancel scenarios.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/app/api/mothership/execute/route.ts Adds NDJSON streaming path with heartbeats, chunk forwarding, abort plumbing, and a refactored buildExecuteResponsePayload helper
apps/sim/executor/handlers/mothership/mothership-handler.ts Adds NDJSON stream consumption helpers, always-on ndjson request headers, and proper reader lock release in all paths
apps/sim/executor/handlers/mothership/mothership-handler.test.ts Adds tests for heartbeat stream consumption, error surfacing, chunk streaming with metadata, and stream cancellation aborting the fetch
apps/sim/lib/logs/execution/trace-spans/trace-spans.ts Short-circuits hasUnhandledError for successful mothership spans so contained tool errors don't bubble to the workflow root
apps/sim/lib/logs/execution/trace-spans/trace-spans.test.ts Adds a test verifying that a successful mothership span with a failed child tool span does not mark the workflow as errored

Sequence Diagram

sequenceDiagram
    participant Handler as MothershipBlockHandler
    participant Route as POST /api/mothership/execute
    participant Lifecycle as runHeadlessCopilotLifecycle
    participant Client as Stream Consumer

    Handler->>Route: POST (Accept: application/x-ndjson)
    Route->>Route: wantsStreamedExecuteResponse true
    Route-->>Handler: 200 application/x-ndjson
    Route->>Client: type heartbeat
    Route->>Lifecycle: runLifecycle(onEvent)
    loop Every 15s idle
        Route->>Client: type heartbeat
    end
    loop On text event
        Lifecycle-->>Route: text event
        Route->>Client: type chunk content
    end
    Lifecycle-->>Route: result
    alt success
        Route->>Client: type final data
    else failure
        Route->>Client: type error
    end
    Route->>Client: stream close
    alt isContentSelectedForStreaming
        Handler->>Handler: createMothershipStreamingExecution
        Client->>Handler: stream.cancel abortController.abort
    else non-streaming
        Handler->>Handler: readMothershipExecuteResponse
    end
Loading

Reviews (3): Last reviewed commit: "Fix" | Re-trigger Greptile

Comment thread apps/sim/executor/handlers/mothership/mothership-handler.ts Outdated
Comment thread apps/sim/executor/handlers/mothership/mothership-handler.ts
Comment thread apps/sim/app/api/mothership/execute/route.ts
@Sg312
Copy link
Copy Markdown
Collaborator Author

Sg312 commented May 16, 2026

@greptile review

@Sg312
Copy link
Copy Markdown
Collaborator Author

Sg312 commented May 16, 2026

@cursor review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 16, 2026

PR Summary

Medium Risk
Adds a new NDJSON streaming mode with heartbeats/chunking and new cancellation paths, which increases complexity in request lifecycle and stream parsing/abort handling (potential for leaks or partial results if mis-handled).

Overview
Adds an optional NDJSON streaming response to POST /api/mothership/execute: callers can request application/x-ndjson (or x-mothership-execute-stream: ndjson) to receive periodic heartbeat events, streamed assistant chunk events, and a terminating final payload instead of waiting for a single JSON response.

Updates the executor’s MothershipBlockHandler to always request NDJSON, parse both JSON and NDJSON responses, and—when ctx.stream selects content—return a StreamingExecution that forwards chunk text while still capturing the final metadata (tokens/tool calls/cost). Abort/cancel handling is tightened so request/stream cancellation propagates through to the underlying Mothership lifecycle and fetch, and tests are expanded to cover heartbeats, chunk streaming, error events, and cancellation behavior.

Reviewed by Cursor Bugbot for commit eee2da0. Bugbot is set up for automated code reviews on this repo. Configure here.

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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit eee2da0. Configure here.

Comment thread apps/sim/executor/handlers/mothership/mothership-handler.ts
@Sg312
Copy link
Copy Markdown
Collaborator Author

Sg312 commented May 16, 2026

@greptile review

@Sg312 Sg312 merged commit 3712d1e into staging May 16, 2026
13 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