Skip to content

fix(mothership): send a late queue write to the chat a new-chat queue moved to - #8741

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/mothership-late-queue-write-follows-migration
Oct 7, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/mothership-late-queue-write-follows-migration

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • A busy refusal no longer vanishes into a dead new-chat key.
    • The scenario: on the new-chat surface, the first message's POST is pending and a follow-up is queued, then the user presses Send-now.
    • The Stop waits for the first message's admission and adopts its chat, which moves the queue to the chat's key and switches the surface.
    • When the follow-up's POST was then refused as busy, the restore re-queued it under the new-chat key captured at dispatch. The message disappeared from the chat's queue, was never retried there, and could later be adopted by another new-chat surface and sent into a new chat.
  • Fixed for every late write, not just this one.
    • migrate now records where a new-chat key moved (migratedTo), including when its queue was empty.
    • Every queue write that captured a key before an await resolves it at write time: the dispatch's removal and restore, the direct send's re-queue after its POST, and the history check's defer and drop. Removals and defers use liveQueueKey. Inserts use liveQueuePosition, which anchors on identity: a restored message goes right after the last message still queued that was ahead of it (at dispatch, or in the chat's queue before the move), else at the head, however the queue changed meanwhile.
  • Every re-queue on a chatless surface carries the surface. A Stop-failed or failed restore used to drop heldSurface. If it landed after the surface unmounted, when nothing else marks the dead key's queue, no later mount adopted it. requeuedFields now tags the surface for every reason.
  • Nits:
    • a stale comment over STOP_REQUEST_TIMEOUT_MS now describes it;
    • sendMothershipMessage normalizes through sendPayload().

Type of Change

  • Bug fix

Testing

  • re-queues a Send-now refused as busy in the chat the new-chat surface moved to (DOM) reproduces the scenario. It fails on staging, where the follow-up is left under the dead key and the visible queue is empty, and passes here.
  • send-queue-policy.test.ts: Stop-failed and failed re-queues keep their chatless surface. Fails on staging.
  • Store tests: liveQueueKey follows a migration, including one whose queue was empty, a late insert stays behind the messages the chat's queue already held, and stays in order when messages ahead of it are removed meanwhile.
  • The full gate and integration tests ran on the CI runner.

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)

… moved to

On the new-chat surface, a Send-now whose Stop saw the first message admitted
moved the queue to that chat, but the follow-up's busy refusal was re-queued
under the dead new-chat key it was dispatched from: gone from the chat, never
retried there, and liable to be adopted into a new chat later. migrate now
records where a key moved, and every write that captured a key before an
await (the dispatch's removal and restore, the direct send's re-queue, the
history check's defer and drop) resolves it at write time (liveQueueKey).
Every re-queue on a chatless surface also carries the surface, so one that
lands after the surface unmounted can still be adopted.
@vercel

vercel Bot commented Oct 7, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Oct 7, 2026 10:52am 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 Oct 7, 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 8 files

Confidence score: 5/5

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

Turn on auto-fix | Re-trigger cubic

@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.

All reported issues were addressed across 8 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts Outdated
@greptile-apps

greptile-apps Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Fixes message queue routing when a new chat surface moves.

The PR appears safe to merge; no blocking issue remains.

What we checked:

  • Old migration records cannot load: migratedTo stays in memory. Storage saves and restores only queues, so old migration records cannot reach the new code.

Summary

This PR sends late queue writes to the chat that a new-chat queue moved into. It also keeps failed sends available when their chatless surface mounts again.

  • The latest change replaces fixed queue positions with message IDs, so removing older messages does not move a restored send behind newer ones.
  • The earlier ordering finding is addressed by the current code and a regression test.
  • No new actionable issues were found. Tests were inspected, not run.
Diagram
%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Send captures messages ahead] --> B[Send waits]
  B --> C[New-chat queue moves to a chat]
  C --> D[Send needs to return to queue]
  D --> E[Follow recorded queue move]
  E --> F[Find last remaining message ahead]
  F --> G[Restore after it or at the head]
Loading

Reviews (3) · Last reviewed commit: "fix(mothership): anchor a late re-queue ..." · Reviewed by Greptile

…dy held

When the new-chat queue moved into a chat queue that already had messages,
migrate put those first, but a late write still used its index in the
new-chat queue and could land ahead of them. The move now records how many
messages it went behind, and late writes resolve their position, not just
their key (liveQueuePosition).
@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 Oct 7, 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 8 files

Confidence score: 5/5

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

Turn on auto-fix | Re-trigger cubic

Comment thread apps/sim/stores/mothership-queue/store.ts Outdated
…not an index

A restored message went back at an index captured at dispatch, offset by
how many messages a chat's queue held when the new-chat queue moved into it.
Removing any of those while the POST was out shifted it behind a newer
message. It now goes right after the last message still queued that was
ahead of it (at dispatch, or in the chat's queue before the move), else at
the head.
@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 Oct 7, 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 8 files

Confidence score: 5/5

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

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit f0e5700 into staging Oct 7, 2026
36 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/mothership-late-queue-write-follows-migration branch October 7, 2026 16:32

This branch was previously deployed

1 inactive deployment
Preview — 8611209d Deployed Oct 7, 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