Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [cli] Rewind messages past last user text message, ignore user tool result message #325

Merged
merged 4 commits into from
Nov 24, 2024

Conversation

jsibbison-square
Copy link
Collaborator

When a user cancelled during a tool request first a tool response was returned (the tool failed as it was interrupted).

The bug in the code was that we removed existing message upto and including the most recent user message. In this case that would be a tool response message that would be removed. This left the messages with a tool but no tool response which is an invalid request to the model.

Fixed by applying the same logic as goose original and rewinding to the most recent user 'text' message. https://github.com/block/goose/blob/main/packages/exchange/src/exchange/exchange.py#L314

@michaelneale
Copy link
Collaborator

nice - looks like not a tiny change so hopefully @baxen gets to take a gander

@jsibbison-square
Copy link
Collaborator Author

nice - looks like not a tiny change so hopefully @baxen gets to take a gander

The actual change is just:

    /// Rewind the messages to before the last user message (they have cancelled it).
    pub fn rewind_messages(&mut self) {
        if self.messages.is_empty() {
            return;
        }

        // Remove messages until we find the last user 'Text' message (not a tool response).
        while let Some(message) = self.messages.last() {
            if message.role == Role::User
                && message
                    .content
                    .iter()
                    .any(|c| matches!(c, MessageContent::Text(_)))
            {
                break;
            }
            self.messages.pop();
        }

        // Remove the last user text message we found.
        if !self.messages.is_empty() {
            self.messages.pop();
        }
    }

The rest is all tests and plumbing (agent Trait for an interface) to enable mocking the agent in the test.

Copy link
Collaborator

@michaelneale michaelneale left a comment

Choose a reason for hiding this comment

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

nice!

@jsibbison-square jsibbison-square merged commit 5c8a212 into v1.0 Nov 24, 2024
2 checks passed
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.

2 participants