Skip to content

[BUG] history.undo() calls truncate(idx) which removes the last user message itself — undo discards the user's question instead of only the assistant's response #48502

@Rieznichenko

Description

@Rieznichenko

Project

cortex

Description

In history.rs (lines 56-75), the undo() function finds the last user message via rposition() and calls self.messages.truncate(idx). Vec::truncate(n) keeps elements at indices [0..n), so it removes the element AT idx — meaning the user's own message is deleted, not just the assistant's reply. The intent (per the comment "Remove everything from the last user message onwards") is to undo the turn, but the user loses their question and cannot see what they asked.

Error Message

Debug Logs

System Information

Operating System: Windows 10
Version: 0.0.7

Screenshots

Image

Steps to Reproduce

PS E:\Workspace\Work History\SN100\cortex> cargo run --bin cortex

Cortex v0.0.7 — Interactive Session

What is Rust?

Rust is a popular programming language used to build everything from web servers to game engines. Rust is known for being very fast, and is similar to C and C++ in language.

/undo

Expected Behavior

Undone. Your last question "What is Rust?" is preserved.
You can edit and resubmit it.

What is Rust? ← still visible, cursor ready to edit
The last assistant response is removed, but the user's question remains so they can modify and resubmit.

Actual Behavior

Undone.

← empty prompt, user's question is gone

Both the assistant response AND the user's question at index idx are removed by truncate(idx). The conversation reverts to BEFORE the user typed anything, not to after they typed but before the assistant replied. The user's input is lost.

Code trace:

messages = [system, user("What is Rust?"), assistant("Rust is...")]
last_user_idx = 1
truncate(1) → keeps [0..1) → messages = [system]
// user("What is Rust?") at idx=1 is gone!

Additional Context

File: history.rs (Lines 67-69)
Root Cause: self.messages.truncate(idx) removes the element at idx (the user message). Should be self.messages.truncate(idx + 1) to keep the user message and only remove what follows it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingideIssues related to IDEvalidValid issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions