Skip to content

Conversation

@trycatchkamal
Copy link
Contributor

@trycatchkamal trycatchkamal commented Aug 13, 2025

TLDR

Fixed a bug in the /chat save command where conversations containing only system context messages (initial environment info and model acknowledgment) were being saved as checkpoints. The fix ensures that only conversations with actual user interaction beyond the initial system context are saved, preventing storage waste and clutter from meaningless checkpoints.

Dive Deeper

The issue was that the /chat save command was saving conversation checkpoints even when there were no actual user messages, only the initial system context messages. This happened because:

  1. System Context Always Present: Every new chat session automatically includes 2 initial messages:

    • A user message with environment context (directory structure, etc.)
    • A model acknowledgment message
  2. Insufficient Filtering: The original logic checked history.length > 2, but this wasn't sufficient to distinguish between:

    • Just system context (2 messages) → should NOT save
    • System context + actual user conversation (3+ messages) → should save
  3. Storage Waste: Users could accidentally save "conversations" that contained no meaningful content, wasting storage and cluttering the checkpoint list.

The fix implements proper filtering logic that:

  • Only saves conversations that contain actual user messages beyond the initial system context
  • Returns "No conversation found to save" when only system context exists

This aligns with the existing logic in the /chat resume command, which already filters out system messages for display purposes.

Reviewer Test Plan

  1. Pull and run the code with the fix applied
  2. Test the save command with minimal conversation:
    • Start a new chat session (this adds system context automatically)
    • Run /chat save test-empty immediately without any user input
    • Expected: Should return "No conversation found to save" instead of creating a checkpoint
  3. Test the save command with actual conversation:
    • Start a new chat session
    • Send a user message like "Hello, how are you?"
    • Get a response from the model
    • Run /chat save test-real
    • Expected: Should successfully save the conversation
  4. Verify checkpoint content:
    • Run /chat list to see saved checkpoints
    • Run /chat resume test-real to verify the conversation loads with actual content
    • Expected: Should show the user's message and model's response, not just system context
  5. Test edge cases:
    • Try saving with various conversation lengths
    • Verify that conversations with only system context are rejected
    • Verify that conversations with actual user interaction are accepted
  6. Integration testing:
    • Save a meaningful conversation
    • Delete it with /chat delete
    • Verify it's removed from the list
    • Try to resume the deleted conversation (should fail appropriately)

Testing Matrix

🍏 🪟 🐧
npm run
npx
Docker
Podman - -
Seatbelt - -

Linked issues / bugs

Addition information

~\gemini-cli\packages\cli>npm test -- chatCommand.test.ts

> @google/gemini-cli@0.1.19 test
> vitest run chatCommand.test.ts


 RUN  v3.2.4 ~/gemini-cli/packages/cli
      Coverage enabled with v8

  src/ui/commands/chatCommand.test.ts (17 tests) 17ms
    chatCommand > should have the correct main command definition 3ms
    chatCommand > list subcommand > should inform when no checkpoints are found 1ms
    chatCommand > list subcommand > should list found checkpoints 1ms
    chatCommand > list subcommand > should handle invalid date formats gracefully 1ms
    chatCommand > save subcommand > should return an error if tag is missing 1ms
    chatCommand > save subcommand > should inform if conversation history is empty or only contains system context 1ms
    chatCommand > save subcommand > should return confirm_action if checkpoint already exists 2ms
    chatCommand > save subcommand > should save the conversation if overwrite is confirmed 1ms
    chatCommand > resume subcommand > should return an error if tag is missing 1ms
    chatCommand > resume subcommand > should inform if checkpoint is not found 0ms
    chatCommand > resume subcommand > should resume a conversation 1ms
    chatCommand > resume subcommand > completion > should provide completion suggestions 1ms
    chatCommand > resume subcommand > completion > should suggest filenames sorted by modified time (newest first) 1ms
    chatCommand > delete subcommand > should return an error if tag is missing 1ms
    chatCommand > delete subcommand > should return an error if checkpoint is not found 1ms
    chatCommand > delete subcommand > should delete the conversation 1ms
    chatCommand > delete subcommand > completion > should provide completion suggestions 1ms

 Test Files  1 passed (1)
      Tests  17 passed (17)
   Start at  11:15:02
   Duration  12.21s (transform 190ms, setup 61ms, collect 1.01s, tests 17ms, environment 8.41s, prepare 678ms)

JUNIT report written to ~/gemini-cli/packages/cli/junit.xml
 % Coverage report from v8

@trycatchkamal trycatchkamal requested a review from a team as a code owner August 13, 2025 05:55
@google-cla
Copy link

google-cla bot commented Aug 13, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

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.

Summary of Changes

Hello @trycatchkamal, 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 a bug in the /chat save command where conversations containing only initial system context messages were being saved as checkpoints. The fix refines the saving logic to ensure that only conversations with actual user interaction are saved, preventing storage waste and clutter from meaningless checkpoints. This change aligns the save behavior with the intended purpose of preserving meaningful chat history.

Highlights

  • Refined Conversation Saving Logic: The condition for saving a chat conversation has been updated from history.length > 0 to history.length > 2. This change correctly accounts for the two initial system context messages present in every new chat session, ensuring that only conversations with actual user input are considered for saving.
  • Prevention of Empty Checkpoints: This fix prevents the /chat save command from creating checkpoints for conversations that consist solely of system context, thus avoiding storage waste and improving the clarity of saved conversation lists.
  • Enhanced Test Coverage: The unit tests for chatCommand have been expanded to explicitly cover scenarios where the conversation history is empty or contains only system context messages, verifying that the save command correctly informs the user that no conversation is found to save in these cases.
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 in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

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 issue 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 Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, 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 aims to prevent saving chat sessions that only contain the initial system context. The implementation correctly identifies this scenario but introduces a critical bug where valid, short conversations (e.g., a single question and answer) would also be prevented from being saved. I've provided a suggestion to refine the logic to correctly distinguish between system-only context and actual short conversations, preventing potential data loss for users.

@trycatchkamal trycatchkamal force-pushed the bug/6119-chat-save-nonempty branch from 58a7bb3 to 44d2125 Compare August 13, 2025 07:22
@trycatchkamal trycatchkamal force-pushed the bug/6119-chat-save-nonempty branch from 44d2125 to b11ccc7 Compare August 13, 2025 07:26
@trycatchkamal trycatchkamal changed the title Check if the history has more than 2 conversations and then save /chat save command saves empty conversations with only system context Aug 13, 2025
it('should inform if conversation history is empty or only contains system context', async () => {
mockGetHistory.mockReturnValue([]);
const result = await saveCommand?.action?.(mockContext, tag);
let result = await saveCommand?.action?.(mockContext, tag);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: rather than reusing the same variable can you create new ones. result1, result2, etc

Copy link
Collaborator

@jacob314 jacob314 left a comment

Choose a reason for hiding this comment

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

lgtm


const history = chat.getHistory();
if (history.length > 0) {
if (history.length > 2) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

as a follow up, it would it be more robust if chat had a method that determined whether the history had any messages that were actually from the user? It is slightly paranoid but I do worry this constant might change from 2 to some other value in the future if we tweaked how the initial conversation is setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jacob314 Thanks for the suggestion.

I agree that relying on a hardcoded constant for the minimum history length is fragile, especially if the initial conversation setup changes in the future. I think moving this logic into some function that checks for actual user messages would make the code more robust and maintainable.

It also makes sense to consider refactoring the resume command to use similar logic for consistency. I will take this up and propose a separate refactor PR, making future changes easier to manage.

@jacob314 jacob314 enabled auto-merge August 14, 2025 16:24
@jacob314 jacob314 added this pull request to the merge queue Aug 14, 2025
Merged via the queue into google-gemini:main with commit 4973e7e Aug 14, 2025
18 checks passed
thacio added a commit to thacio/auditaria that referenced this pull request Aug 14, 2025
agarwalravikant pushed a commit to agarwalravikant/gemini-cli that referenced this pull request Aug 18, 2025
acoliver referenced this pull request in vybestack/llxprt-code Aug 18, 2025
JeongJaeSoon pushed a commit to JeongJaeSoon/gemini-cli that referenced this pull request Aug 21, 2025
involvex pushed a commit to involvex/gemini-cli that referenced this pull request Sep 11, 2025
reconsumeralization pushed a commit to reconsumeralization/gemini-cli that referenced this pull request Sep 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/chat save command saves empty conversations with only system context

3 participants