fix(chat): cancel button not stopping agent and session messages lost on page switch#1489
fix(chat): cancel button not stopping agent and session messages lost on page switch#1489leoleils wants to merge 2 commits intoagentscope-ai:mainfrom
Conversation
- Fix cancel endpoint to preserve conversation context - Fix session message preservation on interruption - Add frontend cancel state handling improvements - Add unit tests for cancel endpoint and message preservation - Add Kiro specs for both bugfixes
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses two critical bugs in the chat interface: the inability to properly stop an agent during tool execution and the loss of chat messages upon page navigation. The changes involve both frontend and backend modifications to synchronize cancellation signals, expose necessary application state, and ensure session data integrity, significantly improving the robustness and user experience of the chat feature. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces fixes for bugs in the chat interface, primarily focusing on the issue where the cancel button didn't stop the backend agent. The changes correctly implement a backend cancellation mechanism by adding a /api/agent/cancel endpoint and wiring it up to the frontend. The asyncio task cancellation is also properly handled now by re-raising CancelledError instead of a generic RuntimeError. The changes are well-tested with new unit and property-based tests.
My review has one suggestion for improvement in the new cancel endpoint to make the task lookup more robust and prevent potential cancellation of incorrect tasks. Note that my review is based on the provided code changes, which seem to address the agent cancellation bug.
Summary
Fixes two related bugs in the web chat interface:
execute_shell_command) leaves the session in an unrecoverable state — the UI shows "Answers have stopped" and the user cannot send new messages.Root Cause
api.cancelcallback inconsole/src/pages/Chat/index.tsxonly calledconsole.log(data)without sending a cancel request to the backend. The agent process continued running, leaving frontend and backend state out of sync.runner.py, theCancelledErrorhandler re-raised asRuntimeError("Task has been cancelled!"), which broke asyncio cancellation semantics and prevented proper cleanup.app.statedid not exposeagent_app, so there was no way to look up and cancel running agent tasks from the router layer.Changes
console/src/pages/Chat/index.tsx:api.cancelnow sends aPOST /api/agent/cancelrequest with thesession_idto notify the backend.src/copaw/app/routers/agent.py: AddedPOST /api/agent/cancelendpoint that looks up the running asyncio task bysession_idinagent_app._local_tasksand cancels it.src/copaw/app/runner/runner.py: Changedraise RuntimeError(...)to bareraisein theCancelledErrorhandler to correctly propagate cancellation.src/copaw/app/_app.py: Exposedagent_apponapp.stateso the cancel endpoint can access it..gitignore: Added.hypothesis/to ignore test cache.Tests
tests/unit/agent/test_cancel_endpoint.py— cancel endpoint unit teststests/unit/agent/test_preservation.py— message preservation logic testsconsole/src/pages/Chat/cancel-bug-condition.test.ts— frontend cancel condition testsconsole/src/pages/Chat/preservation.test.ts— frontend message preservation tests