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

[BUGFIX] History overflow bug fix #920

Closed
wants to merge 18 commits into from
Closed

Conversation

mr-lee
Copy link
Collaborator

@mr-lee mr-lee commented Mar 24, 2025

  1. Added History Overflow Error Handling:
    • Created a new HistoryOverflowError struct to handle conversation history overflow
    • Modified fix_history() to return a boolean indicating if overflow handling is needed
    • Added a new force_valid_history() method to handle history without clearing it

  2. Modified Return Types:
    • Changed as_sendable_conversation_state() to return Result<FigConversationState, HistoryOverflowError>
    • Updated all call sites to handle the new Result type with ? operator

  3. Improved History Management:
    • Added logic to find valid starting messages in conversation history
    • Implemented smarter history truncation to preserve context
    • Replaced automatic history clearing with controlled overflow handling

  4. Test Improvements:
    • Limited test iterations to avoid overflow errors
    • Updated test assertions to handle Result return types

Files Modified

• crates/q_cli/src/cli/chat/conversation_state.rs
• crates/q_cli/src/cli/chat/mod.rs

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Implement user choice for handling conversation history overflow:
- Compact: Summarize the history and continue
- Reset: Clear the history and start fresh
- Continue: Keep the history as is with a warning

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
- Add #[allow(dead_code)] to ctx field in HistoryOverflowHandler
- Add #[allow(dead_code)] to unused methods in HistoryOverflowHandler
- Fix test_conversation_state_history_handling_with_tool_results to avoid overflow errors
- Mock shell execution in test_prompts to avoid dependency on shell binaries
- Mark test_prompts as ignored to prevent failures in environments without shells

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Fix compiler warnings by adding #[allow(dead_code)] annotations to unused
fields and methods, and properly handling Result types in tests.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Add proper error handling for HistoryOverflowError by adding it to the
ChatError enum and using the ? operator to propagate the error.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Simplify error handling for history overflow by removing the unused
history_overflow_handler.rs file and its associated test file. The code
now uses Rust's built-in error propagation mechanism with the ? operator
instead of a complex handler class.

Also removed the unused extract_history method from ConversationState.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Remove the ignore annotation from test_prompts in init.rs to ensure
tests are properly executed rather than being skipped.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Restore the original implementation of run_shell_stdout in init.rs that
actually executes shell commands instead of using a mock implementation.
Also fix imports by adding Command and Stdio and removing the redundant
Write import in the tests module.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Move the Command and Stdio imports from the top-level module to the test
module where they are actually used. This eliminates the unused import
warning during compilation.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Remove the commented-out import for the HistoryOverflowHandler that is
no longer needed since the module has been removed.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Update the import style in the test module to use multi-line format for
better readability and consistency with the preferred style.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Enable unstable rustfmt features to allow for more control over code
formatting, particularly for imports which will now use a multi-line
style for better readability.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Revert imports back to multi-line format to maintain consistent code style
throughout the codebase. This affects files in the q_cli crate including
init.rs, chat/mod.rs, and test files.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Restore original configuration settings in .rustfmt.toml:
- Set unstable_features back to false
- Move use_field_init_shorthand to its original position

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Restore the original formatting in init.rs and init_tests.rs files to
maintain consistent code style as they were at commit 853d86b.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Convert single-line imports to multi-line format in:
- conversation_state.rs
- mod.rs

This maintains consistent code style across the codebase by using
multi-line imports for better readability.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
Keep the file locally for future reference but remove it from version
control as it's not needed in the repository at this time.

🤖 Assisted by [Amazon Q Developer](https://aws.amazon.com/q/developer)
@mr-lee mr-lee requested a review from a team as a code owner March 24, 2025 06:25
@codecov-commenter
Copy link

codecov-commenter commented Mar 24, 2025

Codecov Report

Attention: Patch coverage is 20.25316% with 63 lines in your changes missing coverage. Please review.

Project coverage is 13.85%. Comparing base (3545e45) to head (c60f731).

Files with missing lines Patch % Lines
crates/q_cli/src/cli/chat/conversation_state.rs 24.61% 49 Missing ⚠️
crates/q_cli/src/cli/chat/mod.rs 0.00% 12 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #920      +/-   ##
==========================================
- Coverage   13.86%   13.85%   -0.01%     
==========================================
  Files        2370     2370              
  Lines      205481   205528      +47     
  Branches   185845   185892      +47     
==========================================
- Hits        28485    28479       -6     
- Misses     175473   175524      +51     
- Partials     1523     1525       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@@ -113,6 +117,70 @@ impl ConversationState {
self.history.clear();
}

/// Forces the history to be valid without clearing it
/// This is used when the user chooses to continue with a large history
pub fn force_valid_history(&mut self) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

force_valid_history -> trim_history_until_valid ?

/// Forces the history to be valid without clearing it
/// This is used when the user chooses to continue with a large history
pub fn force_valid_history(&mut self) {
// Find the first valid user message to keep
Copy link
Collaborator

Choose a reason for hiding this comment

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

could the definition of valid change over time? Might be a good idea to extract this into a function

if let Some(i) = self
.history
.iter()
.enumerate()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is it possible to mark a user-message as valid while the conversation is ongoing, instead of having to scan it retroactively?

Copy link
Collaborator

Choose a reason for hiding this comment

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

For example, we wanna make sure we don't consider this as a valid message -
https://github.com/aws/amazon-q-developer-cli/blob/main/crates/q_cli/src/cli/chat/mod.rs#L1130

Comment on lines +159 to +162
matches!(
m.user_input_message_context.as_ref(),
Some(ctx) if ctx.tool_results.as_ref().is_none_or(|v| v.is_empty())
) && !m.content.is_empty()
Copy link
Collaborator

Choose a reason for hiding this comment

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

Useful to add a helper is_valid_user_message fn here

@mr-lee
Copy link
Collaborator Author

mr-lee commented Mar 27, 2025

After discussing with the team - it'll be easier for us to simply increase history length than trying to solve this way. We'll be closing the PR.

@mr-lee mr-lee closed this Mar 27, 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.

4 participants