Skip to content

Branch a chat - #95

Open
sanskaraggarwal2025 wants to merge 2 commits into
mainfrom
get-messages-reference-approach
Open

sanskaraggarwal2025 wants to merge 2 commits into
mainfrom
get-messages-reference-approach

Conversation

@sanskaraggarwal2025

Copy link
Copy Markdown
Collaborator

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Summary of Changes

Hello @sanskaraggarwal2025, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the backend chat service to implement a hierarchical chat history model, addressing the 'reference approach' for chat messages. The core change involves modifying how chat messages are retrieved, moving from a flat list to a recursive query that includes messages from parent chats up to their branch points. Concurrently, the chat branching mechanism has been updated to no longer duplicate historical messages, instead relying on this new hierarchical retrieval to reference past conversations. This ensures a more efficient and logically consistent representation of chat history, with new validation checks added to maintain data integrity during branching operations.

Highlights

  • Hierarchical Chat Message Retrieval: The GetChatMessages function now utilizes a recursive Common Table Expression (CTE) to retrieve messages. This allows it to fetch not only messages from the current chat but also from its entire accessible parent chat hierarchy, up to the point where a branch was made. This is a fundamental shift towards a hierarchical chat history.
  • New Validation Methods for Chat Hierarchy: Two new DAO methods, ChatExists and MessageExistsInChatHierarchy, have been introduced. These methods are crucial for validating the existence and accessibility of chats and specific messages within the new hierarchical structure, ensuring data integrity during operations like branching.
  • Reference-Based Chat Branching: The BranchChat DAO method has been modified to no longer copy historical messages to the newly created branch. This change signifies a move to a reference-based branching model, where new branches implicitly reference the history of their parent chats, rather than duplicating data.
  • Updated Chat Branching Service Logic: The BranchAChat service logic has been updated to remove the previous IsMainBranch restriction. It now leverages the newly introduced ChatExists and MessageExistsInChatHierarchy DAO methods to perform robust validation, allowing users to branch from any valid and accessible message within their chat history.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a 'reference approach' for chat branching, where branched chats refer to their parent's messages instead of copying them. This is achieved by modifying GetChatMessages to use a recursive SQL query to walk the chat hierarchy. New DAO methods ChatExists and MessageExistsInChatHierarchy are added for validation.

My review has identified a critical bug in the implementation of MessageExistsInChatHierarchy that could lead to incorrect authorization checks for messages in parent chats. I have also pointed out some leftover debugging statements that should be removed. Overall, the changes align with the goal of a referential branching model, but the identified issues need to be addressed before merging.

Comment thread backend/chatservice/dao/dao_sqlite.go Outdated
WHERE cm.id = ? AND cm.user_id = ?
AND (
ch.level = 0 -- Current chat, any message is valid
OR (ch.level > 0 AND cm.id <= ch.parent_message_id) -- Parent chats, only up to branch point

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

critical

The logic for checking messages in parent chats is incorrect. ch.parent_message_id refers to the parent of the chat at the current level of recursion, not the message ID from which the original chat was branched.

Additionally, parent_message_id is a TEXT column, while cm.id is likely an integer. The comparison cm.id <= ch.parent_message_id will perform a lexicographical (string) comparison instead of a numerical one, which will produce incorrect results. You need to CAST parent_message_id to an integer for the comparison to be correct.

The correct approach is to get the parent_message_id from the original chat (at level = 0) and use that for comparison, similar to how it's done in GetChatMessages.

Suggested change
OR (ch.level > 0 AND cm.id <= ch.parent_message_id) -- Parent chats, only up to branch point
OR (ch.level > 0 AND cm.id <= (SELECT CAST(parent_message_id AS INTEGER) FROM chat_hierarchy WHERE level = 0)) -- Parent chats, only up to branch point

}

newChatId := uuid.New().String()
fmt.Println("newChatId", newChatId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This fmt.Println call appears to be a leftover debugging statement and should be removed before merging.

return "", fmt.Errorf("failed to create branch: %v", err)
}

fmt.Println("newChatId", newChatId)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This fmt.Println call appears to be a leftover debugging statement and should be removed before merging.

AddChatMessage(userID string, chatId string, role string, content string) error
AddChatMessageWithTokens(userID string, chatId string, role string, content string, model string, inputTokens int, outputTokens int) (int64, error)
GetChatMessages(userID string, chatId string) ([]ChatMessageRow, error)
ChatExists(userID string, chatId string) (bool, error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DoesChatExist is more readable

AddChatMessageWithTokens(userID string, chatId string, role string, content string, model string, inputTokens int, outputTokens int) (int64, error)
GetChatMessages(userID string, chatId string) ([]ChatMessageRow, error)
ChatExists(userID string, chatId string) (bool, error)
MessageExistsInChatHierarchy(userID string, chatId string, messageId string) (bool, error)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

DoesMessageExistInChatHistory

@xask00 xask00 changed the title get chat message query fix for reference approach Branch a chat Aug 24, 2025
@coderabbitai

coderabbitai Bot commented Nov 28, 2025

Copy link
Copy Markdown
Contributor

Warning

Rate limit exceeded

@sanskaraggarwal2025 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 13 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between b2bc8bc and c881821.

📒 Files selected for processing (2)
  • backend/chatservice/dao/dao.go (1 hunks)
  • backend/chatservice/service/service.go (1 hunks)

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch get-messages-reference-approach

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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