fix(sessions): handle turn_complete event ordering for local sessions#2197
Merged
Conversation
Generated-By: PostHog Code Task-Id: abebbeb8-aae5-4d41-94be-f2e7e878e3a2
Contributor
Prompt To Fix All With AIFix the following 1 code review issue. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 1
apps/code/src/renderer/features/sessions/service/service.test.ts:3099
The test only asserts `currentPromptId` is preserved after `turn_complete` for local sessions, but the fix also skips clearing `isPromptPending` and `promptStartedAt`. If a future change incorrectly clears those two fields on `turn_complete` for local sessions, this test would not catch it. Adding assertions for all three fields would make the regression spec fully self-describing.
```suggestion
expect(session?.currentPromptId).toBe(42);
expect(session?.isPromptPending).toBe(true);
expect(session?.promptStartedAt).not.toBeNull();
```
Reviews (1): Last reviewed commit: "fix(sessions): handle turn_complete even..." | Re-trigger Greptile |
| }, | ||
| }); | ||
|
|
||
| expect(session?.currentPromptId).toBe(42); |
Contributor
There was a problem hiding this comment.
The test only asserts
currentPromptId is preserved after turn_complete for local sessions, but the fix also skips clearing isPromptPending and promptStartedAt. If a future change incorrectly clears those two fields on turn_complete for local sessions, this test would not catch it. Adding assertions for all three fields would make the regression spec fully self-describing.
Suggested change
| expect(session?.currentPromptId).toBe(42); | |
| expect(session?.currentPromptId).toBe(42); | |
| expect(session?.isPromptPending).toBe(true); | |
| expect(session?.promptStartedAt).not.toBeNull(); |
Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/code/src/renderer/features/sessions/service/service.test.ts
Line: 3099
Comment:
The test only asserts `currentPromptId` is preserved after `turn_complete` for local sessions, but the fix also skips clearing `isPromptPending` and `promptStartedAt`. If a future change incorrectly clears those two fields on `turn_complete` for local sessions, this test would not catch it. Adding assertions for all three fields would make the regression spec fully self-describing.
```suggestion
expect(session?.currentPromptId).toBe(42);
expect(session?.isPromptPending).toBe(true);
expect(session?.promptStartedAt).not.toBeNull();
```
How can I resolve this? If you propose a fix, please make it concise.
charlesvien
approved these changes
May 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR
This PR reapplies a critical fix for local session event ordering where
turn_completeevents arrive before JSON-RPC responses, causing race conditions. The fix ensures cloud and local sessions handle turn completion differently based on their canonical signal sources.closes #2182 to fix codex message queuing
Problem
Local sessions with Codex agents experience a race condition where the
turn_completeevent arrives before the corresponding JSON-RPC response. The previous implementation clearedcurrentPromptIdimmediately onturn_complete, which interfered with the ID-match guard that relies on this value being present when the JSON-RPC response arrives. This caused prompts to hang or fail to complete properly.Cloud sessions use
turn_completeas their canonical turn-done signal, while local sessions use the JSON-RPC response. These require different handling to prevent race conditions.Changes
dequeueMessagesAsTextmock return type annotation from implicit to explicit(): string | null => nullfor clarityisCloudTurnCompleteEvent()toisTurnCompleteEvent()to better reflect its universal applicabilityturn_completeevent handling to only clear session state (isPromptPending,promptStartedAt,currentPromptId) for cloud sessions, preventing the race condition in local sessionsHow did you test this?
The changes include a new integration test that reproduces the race condition scenario:
turn_completeevent before the JSON-RPC responsecurrentPromptIdis preserved until the JSON-RPC response arrivesCreated with PostHog Code