fix: correct message ordering after task cancellation — v0.50.163 (#883)
fix: correct message ordering after task cancellation — v0.50.163 (#883) Fixes the message-ordering glitch from #882: clicking Cancel while the agent is responding could cause a subsequent response to render above the "*Task cancelled.*" marker. Root cause: the cancel handler pushed the marker only to local S.messages without persisting to the server. When the done event fired shortly after and replaced S.messages from server state, the marker disappeared from client state while the next response anchored to the server-authoritative position. Fix has three parts: - Server (cancel_stream): append *Task cancelled.* to session.messages with _error:True + timestamp, then save. _error ensures _sanitize_messages_for_api() strips it from conversation_history on the next agent turn, so the LLM never sees it as a prior assistant turn. Precedent: same flag used for the apperror marker at line 1343. - Client (SSE cancel handler): fetch /api/session instead of pushing locally (same pattern as the done handler). Falls back to local push if the fetch fails. - Tests: fix test window width for cancel handler (1200→dynamic); add two regression tests pinning _error flag and _sanitize invariant. 1941 tests passing. Co-authored-by: piliang <piliang1@jd.com>
This commit is contained in:
@@ -1666,6 +1666,18 @@ def cancel_stream(stream_id: str) -> bool:
|
||||
_cs.pending_user_message = None
|
||||
_cs.pending_attachments = []
|
||||
_cs.pending_started_at = None
|
||||
# Add cancel message to session messages so client sees consistent state.
|
||||
# _error=True flags this as a synthetic UI marker so
|
||||
# _sanitize_messages_for_api() (line 591-593) strips it from the
|
||||
# conversation_history passed to the agent on the NEXT user message —
|
||||
# otherwise the model would see "Task cancelled." in its history as a
|
||||
# prior assistant turn and could respond accordingly.
|
||||
_cs.messages.append({
|
||||
'role': 'assistant',
|
||||
'content': '*Task cancelled.*',
|
||||
'_error': True,
|
||||
'timestamp': int(time.time()),
|
||||
})
|
||||
_cs.save()
|
||||
except Exception:
|
||||
logger.debug("Failed to clear session state on cancel for %s", _cancel_session_id)
|
||||
|
||||
Reference in New Issue
Block a user