Skip to content

fix(chat): cancel button not stopping agent and session messages lost on page switch#1489

Open
leoleils wants to merge 2 commits intoagentscope-ai:mainfrom
leoleils:bugfix/chat-interrupted-cannot-continue
Open

fix(chat): cancel button not stopping agent and session messages lost on page switch#1489
leoleils wants to merge 2 commits intoagentscope-ai:mainfrom
leoleils:bugfix/chat-interrupted-cannot-continue

Conversation

@leoleils
Copy link

Summary

Fixes two related bugs in the web chat interface:

  1. Clicking the "Stop" button while the agent is executing a tool call (e.g. execute_shell_command) leaves the session in an unrecoverable state — the UI shows "Answers have stopped" and the user cannot send new messages.
  2. Switching to another page (e.g. MCP, Models) during a streaming response and returning causes chat messages to be lost permanently.

Root Cause

  • api.cancel callback in console/src/pages/Chat/index.tsx only called console.log(data) without sending a cancel request to the backend. The agent process continued running, leaving frontend and backend state out of sync.
  • In runner.py, the CancelledError handler re-raised as RuntimeError("Task has been cancelled!"), which broke asyncio cancellation semantics and prevented proper cleanup.
  • app.state did not expose agent_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.cancel now sends a POST /api/agent/cancel request with the session_id to notify the backend.
  • src/copaw/app/routers/agent.py: Added POST /api/agent/cancel endpoint that looks up the running asyncio task by session_id in agent_app._local_tasks and cancels it.
  • src/copaw/app/runner/runner.py: Changed raise RuntimeError(...) to bare raise in the CancelledError handler to correctly propagate cancellation.
  • src/copaw/app/_app.py: Exposed agent_app on app.state so the cancel endpoint can access it.
  • .gitignore: Added .hypothesis/ to ignore test cache.

Tests

  • tests/unit/agent/test_cancel_endpoint.py — cancel endpoint unit tests
  • tests/unit/agent/test_preservation.py — message preservation logic tests
  • console/src/pages/Chat/cancel-bug-condition.test.ts — frontend cancel condition tests
  • console/src/pages/Chat/preservation.test.ts — frontend message preservation tests

- 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
@github-actions github-actions bot added the first-time-contributor PR created by a first time contributor label Mar 14, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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

  • Agent Cancellation: Implemented a proper mechanism to cancel agent tasks when the "Stop" button is clicked, preventing the UI from entering an unrecoverable state.
  • Message Preservation: Ensured chat messages are not lost when switching pages during a streaming response, by correcting how session updates handle message data.
  • Backend API: Introduced a new backend endpoint (/api/agent/cancel) to handle agent task cancellations.
  • Error Handling: Refined the backend's asyncio.CancelledError handling to correctly propagate cancellation without raising a RuntimeError.

🧠 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
  • .gitignore
    • Added .hypothesis/ to ignore test cache.
  • .kiro/specs/chat-interrupted-cannot-continue/.config.kiro
    • Added Kiro spec configuration for the chat interruption bugfix.
  • .kiro/specs/chat-interrupted-cannot-continue/bugfix.md
    • Documented the bugfix requirements for the chat interruption issue in Chinese.
  • .kiro/specs/chat-interrupted-cannot-continue/design.md
    • Documented the design for the chat interruption bugfix in Chinese.
  • .kiro/specs/chat-interrupted-cannot-continue/tasks.md
    • Documented the implementation tasks for the chat interruption bugfix in Chinese.
  • .kiro/specs/chat-session-messages-lost/.config.kiro
    • Added Kiro spec configuration for the chat session messages lost bugfix.
  • .kiro/specs/chat-session-messages-lost/bugfix.md
    • Documented the bugfix requirements for the chat session messages lost issue in Chinese.
  • .kiro/specs/chat-session-messages-lost/design.md
    • Documented the design for the chat session messages lost bugfix in Chinese.
  • .kiro/specs/chat-session-messages-lost/tasks.md
    • Documented the implementation tasks for the chat session messages lost bugfix in Chinese.
  • console/src/pages/Chat/cancel-bug-condition.test.ts
    • Added a new frontend test to explore the cancel bug condition.
  • console/src/pages/Chat/index.tsx
    • Modified the api.cancel callback to send a POST request to the backend's new cancel endpoint.
  • console/src/pages/Chat/preservation.test.ts
    • Added a new frontend test to verify message preservation logic.
  • console/vitest.config.ts
    • Added Vitest configuration for frontend tests.
  • src/copaw/app/_app.py
    • Exposed the agent_app instance on app.state to allow access from other parts of the application.
  • src/copaw/app/routers/agent.py
    • Imported Request from fastapi.
    • Defined a CancelRequest Pydantic model for the new cancel endpoint.
    • Added a new POST /api/agent/cancel endpoint to find and cancel active agent tasks by session_id.
  • src/copaw/app/runner/runner.py
    • Changed the asyncio.CancelledError handling in query_handler to re-raise the bare exception, ensuring proper cancellation semantics.
  • tests/unit/agent/init.py
    • Added an __init__.py file for agent unit tests.
  • tests/unit/agent/test_cancel_endpoint.py
    • Added a new backend unit test to verify the existence and functionality of the /api/agent/cancel endpoint.
  • tests/unit/agent/test_preservation.py
    • Added a new backend unit test to ensure existing functionalities like session state saving and router endpoints are preserved.
Activity
  • Frontend api.cancel callback was updated to send a POST /api/agent/cancel request.
  • A new backend POST /api/agent/cancel endpoint was implemented to handle agent task cancellations.
  • The agent_app instance was exposed on app.state for backend access.
  • asyncio.CancelledError handling in runner.py was adjusted for correct propagation.
  • New frontend tests (cancel-bug-condition.test.ts, preservation.test.ts) and backend tests (test_cancel_endpoint.py, test_preservation.py) were added to validate the fixes and ensure no regressions.
  • Vitest configuration was added for frontend testing.
  • Kiro specification documents (bugfix, design, tasks) were added for both identified issues.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Generative AI Prohibited Use Policy, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@leoleils leoleils requested a deployment to maintainer-approved March 14, 2026 15:06 — with GitHub Actions Waiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

first-time-contributor PR created by a first time contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant