Summary
handleDialogTurnComplete gates settlement on eventOwnsLatestSessionTurn, which requires the completed turn to be the last dialog turn; when an optimistic follow-up turn exists (or the machine context was reset by the failure path), the completion event is dropped and the session stays PROCESSING forever.
Ownership matching now prefers the machine's currentDialogTurnId, the failure path settles with a single FINISHING_SETTLED transition instead of ERROR_OCCURRED→RESET (which clears currentDialogTurnId and desynchronizes later ownership checks), and completion events settle unconditionally (BACKEND_STREAM_COMPLETED while PROCESSING plus an unconditional beginTurnCompletion). The ownsSessionSettlement mechanism stays in place at all other consumption points.
Area
src/web-ui/src/flow_chat/services/flow-chat-manager/EventHandlerModule.ts (+ its test file).
Reproduction or evidence
At 32f2427, eventOwnsLatestSessionTurn (:183-192) still prioritizes dialogTurns.at(-1); handleDialogTurnComplete :2648/:2658 still gate both transitions on ownsSessionSettlement; the failed path (:2748-2749) still performs the ERROR_OCCURRED→RESET double transition. A turn completed while a newer optimistic turn exists leaves the state machine in PROCESSING indefinitely. Regression test: settles the state machine when completion arrives for a non-latest turn.
Environment
All platforms (frontend state machine); baseline 32f2427.
AI-assisted change. Testing: verified locally (pnpm vitest run EventHandlerModule.test.ts 48 passed, 0 failed, including the new regression case; pnpm run type-check (tsc --noEmit) exit 0 with no errors).
Summary
handleDialogTurnComplete gates settlement on eventOwnsLatestSessionTurn, which requires the completed turn to be the last dialog turn; when an optimistic follow-up turn exists (or the machine context was reset by the failure path), the completion event is dropped and the session stays PROCESSING forever.
Ownership matching now prefers the machine's currentDialogTurnId, the failure path settles with a single FINISHING_SETTLED transition instead of ERROR_OCCURRED→RESET (which clears currentDialogTurnId and desynchronizes later ownership checks), and completion events settle unconditionally (BACKEND_STREAM_COMPLETED while PROCESSING plus an unconditional beginTurnCompletion). The ownsSessionSettlement mechanism stays in place at all other consumption points.
Area
src/web-ui/src/flow_chat/services/flow-chat-manager/EventHandlerModule.ts (+ its test file).
Reproduction or evidence
At 32f2427, eventOwnsLatestSessionTurn (:183-192) still prioritizes dialogTurns.at(-1); handleDialogTurnComplete :2648/:2658 still gate both transitions on ownsSessionSettlement; the failed path (:2748-2749) still performs the ERROR_OCCURRED→RESET double transition. A turn completed while a newer optimistic turn exists leaves the state machine in PROCESSING indefinitely. Regression test: settles the state machine when completion arrives for a non-latest turn.
Environment
All platforms (frontend state machine); baseline 32f2427.
AI-assisted change. Testing: verified locally (
pnpm vitest run EventHandlerModule.test.ts48 passed, 0 failed, including the new regression case;pnpm run type-check(tsc --noEmit) exit 0 with no errors).