Skip to content

fix(mothership): explain a Chat turn the worker ends without a reason - #8478

Merged
waleedlatif1 merged 4 commits into
stagingfrom
fix/chat-ended-run-terminal-message
Oct 1, 2026
Merged

waleedlatif1 merged 4 commits into
stagingfrom
fix/chat-ended-run-terminal-message

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Problem. When a resume or reattach reaches a Chat run that has already ended (for example at its deadline), the turn failed with the generic "An unexpected error occurred while processing the response." and gave no hint of what happened.

Root cause. When the worker replays an ended run from its log, it sends an error terminal with no error event, because that replay does not carry the run's stored reason. runCopilotLifecycle saw completionStatus === error with an empty context.errors, so no specific message was set and callers fell back to the generic one.

Fix. runCopilotLifecycle now detects that exact shape (error terminal, no reported error, no replay refusal, not aborted) and sets a surface-neutral message: "This run had already ended before it could continue." A reason the worker reports, a replay refusal, and a user Stop all still take precedence. The refusal wins by an explicit guard, not by spread order.

Also:

  • The Go stream's error text is now logged under errorMessage / detail, so it no longer overwrites the log line's own message field (go/stream.ts, session/buffer.ts).
  • STREAM_TIMEOUT_MS is renamed CHAT_RUN_DEADLINE_MS and documented as the worker's default run deadline. Sim does not enforce it; it is only the base of USAGE_SETTLE_MS.
  • The byte-budget doc now says a reader behind the replay-ring trim is re-synced from the worker log, and gets replay_gap only when that log cannot serve it.

Behaviour changes

  • A turn that reattaches to an already-ended run now shows "This run had already ended before it could continue." instead of the generic unexpected-error message. This applies to Chat, workflow execute, and inbox.
  • Runs that fail with a worker-reported reason, runs refused on replay, and runs the user stopped behave as before.
  • Log field names change for two Go-stream error log lines (message → errorMessage / detail).
  • No change to billing values: USAGE_SETTLE_MS keeps the same number.

Test plan

  • lib/mothership/request/lifecycle/run.test.ts: new cases for an error terminal with no reason (gets the ended-run message), a replay refusal combined with a reasonless error terminal (refusal wins), and a Stop with a reasonless error terminal (stays a cancellation). Removing the fallback turns the ended-run case red; restoring it turns it green.
  • lib/mothership/request/lifecycle and lib/mothership/request/go suites pass (185 tests).
  • bun run type-check in apps/sim
  • Biome check on changed files
  • Staging: reattach to a Chat run past its deadline and confirm the new message

When the worker rebuilds an ended run from its log (a resume or reattach
that reaches a run that already ended, for example at its deadline), it
sends an error terminal with no error event. Sim then fell back to the
generic "An unexpected error occurred while processing the response."
The turn now says the run had already ended and can be continued by
sending a message. A reason the worker reports, a replay refusal, and a
Stop all still take precedence.

Also:
- Log the Go stream's error text under errorMessage/detail so it no longer
  overwrites the log line's own message (stream.ts, buffer.ts).
- Rename STREAM_TIMEOUT_MS to CHAT_RUN_DEADLINE_MS and document it as the
  worker's default run deadline, now only the base of USAGE_SETTLE_MS.
- Update the byte-budget doc: a reader behind the ring trim is re-synced
  from the worker log; replay_gap is only the fallback.
… a replay refusal

The fallback is shared by Chat, workflow execute and inbox, so it no longer
tells the reader to send a message. A replay refusal now wins by guard rather
than by spread order, with a test that covers the combination.
@vercel

vercel Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
docs Ready Ready Preview Sep 30, 2026 11:19pm UTC

Request Review

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 7 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@greptile-apps

greptile-apps Bot commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adjusts how worker run deadlines and error messages are handled.

The PR appears safe to merge, with a non-blocking Slack callback recovery-message regression.

Summary

The PR gives reasonless, already-ended runs a specific lifecycle error and clarifies the worker deadline, replay-ring behavior, and Go-stream log fields. Changes since the previous review also add a desktop system-browser source-connection flow. The Slack callback’s new failure redirect loses actionable recovery guidance when the browser session has expired.

Diagram
sequenceDiagram
  participant Desktop
  participant Sim
  participant Browser
  Desktop->>Sim: Store one-use source request
  Desktop->>Browser: Open source connection
  Browser->>Sim: Consume request and start authorization
  Sim-->>Browser: Provider completion
  Browser->>Desktop: Return result through loopback
Loading

Reviews (3) · Last reviewed commit: "Merge remote-tracking branch 'origin/sta..."

Comment thread apps/sim/lib/mothership/request/lifecycle/run.test.ts Outdated
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 7 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 7 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 2178d6e into staging Oct 1, 2026
32 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/chat-ended-run-terminal-message branch October 1, 2026 01:24

This branch was successfully deployed

1 active deployment
Preview — 183d3219 Deployed Sep 30, 2026 by vercel[bot]
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