improvement(mothership): abort path race preventing persistence#4647
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
PR SummaryMedium Risk Overview Refactors stop/finalization to use a transactional, Adds Reviewed by Cursor Bugbot for commit 05830df. Configure here. |
Greptile SummaryThis PR fixes a race condition where stopping a copilot stream could silently drop partial assistant responses. Previously, the abort-exception catch path called
Confidence Score: 5/5Safe to merge; the core race is correctly closed by the FOR UPDATE transaction and the active-or-cleared policy handles both orderings of concurrent writes idempotently. The transaction + row-lock approach is sound for PostgreSQL, the two writer paths (stop route and cancelled lifecycle) are both serialised through the same finaliser, and the AssistantAlreadyPersisted outcome prevents double-writes. New tests cover the key race scenarios. The only gap is a defence-in-depth omission (no userId on the cancelled path in post.ts), which is not immediately exploitable given session-scoped chatIds. apps/sim/lib/copilot/chat/post.ts — the buildOnComplete cancelled path omits userId when calling finalizeAssistantTurn, while the stop route passes it; worth aligning for consistency. Important Files Changed
Sequence DiagramsequenceDiagram
participant Client
participant StopRoute as StopRoute
participant Lifecycle as Lifecycle
participant Finalizer as Finalizer
participant DB as PostgreSQL
Client->>StopRoute: POST /chat/stop
Client->>Lifecycle: AbortSignal fires
Lifecycle->>Lifecycle: runStreamLoop throws
Lifecycle->>Finalizer: finalizeAssistantTurn active-or-cleared
Finalizer->>DB: BEGIN SELECT FOR UPDATE
DB-->>Finalizer: row locked
Finalizer->>DB: UPDATE append assistant clear marker
Finalizer-->>Lifecycle: "updated=true AppendedAssistant COMMIT"
StopRoute->>Finalizer: finalizeAssistantTurn active-or-cleared with userId
Finalizer->>DB: BEGIN SELECT FOR UPDATE
DB-->>Finalizer: row assistant already present marker null
Finalizer-->>StopRoute: "updated=false AssistantAlreadyPersisted"
StopRoute->>StopRoute: publishStatusChanged completed idempotent
Reviews (3): Last reviewed commit: "address bugbot comment" | Re-trigger Greptile |
|
@greptile |
|
bugbot run |
|
bugbot run |
|
@greptile |
There was a problem hiding this comment.
✅ 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 05830df. Configure here.
Summary
Fixes stopped copilot streams so partial assistant responses are persisted reliably even if the abort path clears the active stream marker first.
Type of Change
Testing
Tested manually
Checklist