fix(spa): don't attribute a page-hide to a turn that already finished - #1370
Merged
Merged
Conversation
Page-hide attribution picks turns from streamingSessionIds(), which still lists a session until the transport closes, and close can trail `done`. A departure in that window POSTed `navigated_away` for a finished turn. The backend lease gate doesn't fully cover it, because the lease is released in the stream generator's `finally`, which can itself come after `done`. Skip sessions where hasReceivedDone() is true, as the Stop path does since #1366. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Follow-up to #1366, closing the gap its "Not in this PR" section named.
Problem
The
pagehidehandler inchat-http.service.tsattributes a page departure to every session instreamingSessionIds()and POSTsnavigated_awayfor each. A session stays in that list until the transport'sonclose, which can trail the server'sdoneframe (widest on a first turn, wheresession_titlecan arrive afterdone). A refresh or tab close in that window stampsnavigated_awayon a turn that already finished.The backend gate in
POST /sessions/{id}/interruptrecordsnavigated_awayonly while the single-flight lease is held. That doesn't fully close the gap: the lease is released in the stream generator'sfinally, which can itself run afterdonehas been sent.Fix
Skip any session where
StreamParserService.hasReceivedDone(sessionId)is true, the same check #1366 added tocancelChatRequest. Mid-stream departures are still attributed exactly as before.Tests
Two specs added to the existing race block in
chat-http.service.spec.ts(renamed "Stop or page-hide racing the end of the turn"). They use the same open-stream harness as the Stop specs:done, before close: sends no interrupt POST and doesn't abort the stream.navigated_awayPOST for the session.With the fix reverted locally, the first spec fails and the second passes, as expected.
npx ng test --watch=false: 324 files, 4082 tests passed.🤖 Generated with Claude Code