-
Notifications
You must be signed in to change notification settings - Fork 2.4k
fix: preserve original first message context during conversation condensing #7939
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,11 +223,24 @@ export function getMessagesSinceLastSummary(messages: ApiMessage[]): ApiMessage[ | |
| const messagesSinceSummary = messages.slice(lastSummaryIndex) | ||
|
|
||
| // Bedrock requires the first message to be a user message. | ||
| // We preserve the original first message to maintain context. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we expand this comment to explain why preserving the original first message is important for maintaining task context? This would help future developers understand the reasoning beyond just Bedrock compatibility. |
||
| // See https://github.com/RooCodeInc/Roo-Code/issues/4147 | ||
| const userMessage: ApiMessage = { | ||
| role: "user", | ||
| content: "Please continue from the following summary:", | ||
| ts: messages[0]?.ts ? messages[0].ts - 1 : Date.now(), | ||
| if (messagesSinceSummary.length > 0 && messagesSinceSummary[0].role !== "user") { | ||
| // Get the original first message (should always be a user message with the task) | ||
| const originalFirstMessage = messages[0] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this edge case intentional? The function checks if the first message after a summary is not a user message, but what happens if the original messages array itself doesn't start with a user message? While the fallback handles this gracefully, could we add a validation or warning when the original conversation doesn't follow the expected pattern? |
||
| if (originalFirstMessage && originalFirstMessage.role === "user") { | ||
| // Use the original first message unchanged to maintain full context | ||
| return [originalFirstMessage, ...messagesSinceSummary] | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we consider extracting this logic into a helper function for better readability? Something like: This would make the intent clearer and potentially allow for reuse. |
||
| } else { | ||
| // Fallback to generic message if no original first message exists (shouldn't happen) | ||
| const userMessage: ApiMessage = { | ||
| role: "user", | ||
| content: "Please continue from the following summary:", | ||
| ts: messages[0]?.ts ? messages[0].ts - 1 : Date.now(), | ||
| } | ||
| return [userMessage, ...messagesSinceSummary] | ||
| } | ||
| } | ||
| return [userMessage, ...messagesSinceSummary] | ||
|
|
||
| return messagesSinceSummary | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test update! Consider adding an explicit test case for when the original messages array doesn't start with a user message to ensure the fallback behavior is properly covered.