Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/cli/src/services/BuiltinCommandLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { extensionsCommand } from '../ui/commands/extensionsCommand.js';
import { helpCommand } from '../ui/commands/helpCommand.js';
import { shortcutsCommand } from '../ui/commands/shortcutsCommand.js';
import { rewindCommand } from '../ui/commands/rewindCommand.js';
import { undoCommand } from '../ui/commands/undoCommand.js';
import { hooksCommand } from '../ui/commands/hooksCommand.js';
import { ideCommand } from '../ui/commands/ideCommand.js';
import { initCommand } from '../ui/commands/initCommand.js';
Expand Down Expand Up @@ -122,6 +123,7 @@ export class BuiltinCommandLoader implements ICommandLoader {
shortcutsCommand,
...(this.config?.getEnableHooksUI() ? [hooksCommand] : []),
rewindCommand,
undoCommand,
await ideCommand(),
initCommand,
...(isNightlyBuild ? [oncallCommand] : []),
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/ui/commands/rewindCommand.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
* @param messageId The ID of the message to rewind to.
* @param newText The new text for the input field after rewinding.
*/
async function rewindConversation(
export async function rewindConversation(
context: CommandContext,
client: GeminiClient,
recordingService: ChatRecordingService,
Expand Down
57 changes: 57 additions & 0 deletions packages/cli/src/ui/commands/undoCommand.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { CommandKind, type SlashCommand } from './types.js';
import { rewindConversation } from './rewindCommand.js';
import { partToString } from '@google/gemini-cli-core';

export const undoCommand: SlashCommand = {
name: 'undo',
description: 'Revert the last conversation turn',
kind: CommandKind.BUILT_IN,
action: async (context) => {
const client = context.services.config?.getGeminiClient();
const recordingService = client?.getChatRecordingService();
const conversation = recordingService?.getConversation();

if (!client || !recordingService || !conversation) {
return {
type: 'message',
messageType: 'error',
content: 'Undo unavailable.',
};
}

const messages = conversation.messages;
const lastUserIndex = messages.findLastIndex((m) => m.type === 'user');

// User message and a model response to undo turn
if (messages.length < 2) {
Comment on lines +29 to +32
Copy link
Copy Markdown
Contributor

@mrpmohiburrahman mrpmohiburrahman Apr 4, 2026

Choose a reason for hiding this comment

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

When no user message exists, findLastIndex returns -1. The guard on line 32 checks messages.length < 2, but that only counts messages. It doesn't check whether any of them are actually 'user' type.

The codebase defines 5 message types ('user' | 'info' | 'error' | 'warning' | 'gemini'chatRecordingService.ts line 78), so a conversation can have 2+ non-user messages, pass the guard, and crash on line 40 when the code tries to access messages[-1].id while lastUserIndex is still -1.

Simple fix on line 32:

// before
if (messages.length < 2) {
// after
if (lastUserIndex === -1) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

fixed it, thanks!

return {
type: 'message',
messageType: 'info',
content: 'Nothing to undo.',
};
}

const targetId = messages[lastUserIndex].id;
const lastUserPrompt = messages[lastUserIndex].content;

await rewindConversation(
context,
client,
recordingService,
targetId,
partToString(lastUserPrompt),
);

return {
type: 'message',
messageType: 'info',
content: 'Undid last turn.',
};
},
};