Skip to content

Fix: regenerate chat message does not truncate history - #17087

Merged
wangq8 merged 2 commits into
infiniflow:mainfrom
euvre:fix/chat-regenerate-truncation
Jul 20, 2026
Merged

wangq8 merged 2 commits into
infiniflow:mainfrom
euvre:fix/chat-regenerate-truncation

Conversation

@euvre

@euvre euvre commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a bug where clicking the regenerate (refresh) button on a chat message did not reliably truncate the conversation. Depending on which message was regenerated, the history was either truncated correctly, fully restored after a browser refresh, or even grew by an extra Q&A pair.

Root causes in the web frontend:

  • Regenerating the first question produced an empty truncated history (messages.slice(0, 0)), which sendMessage treated as "not provided" and silently replaced with the full untruncated history. The backend then overwrote the session (pass_all_history_messages: true) with the full history plus a duplicated question, so the truncation was never persisted and a browser refresh restored (or grew) the message list.
  • The re-sent question was assigned a new uuid() while the local question kept its old id, diverging the question/answer pair ids between local state and the persisted session. Subsequent delete/regenerate operations then acted on stale ids, leaving orphan assistant messages whose corrupted order was later persisted by the next full-history submission.

Changes:

  • useRegenerateMessage: always pass the truncated history explicitly (even when empty) and keep the original message id so pair ids stay consistent between local state and the persisted session.
  • use-send-chat-message.ts: an explicitly provided message list is now authoritative even when empty; the derivedMessages fallback only applies when no list is provided. Normal sends are unaffected (empty currentMessages is passed as undefined).

When regenerating an answer, the truncated message list was not always
sent to the backend:

- Regenerating the first question produced an empty history list, which
  sendMessage treated as 'not provided' and silently replaced with the
  full untruncated history. The backend then overwrote the session with
  the full history plus a duplicated question, so the truncation was
  never persisted and a browser refresh restored (or grew) the list.
- The re-sent question was given a new id while the local question kept
  the old one, diverging the question/answer pair ids between local
  state and the persisted session and corrupting later delete/regenerate
  operations.

Keep the original message id when regenerating, always pass the
truncated history explicitly (even when empty), and only fall back to
derivedMessages when no message list is provided.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧰 typescript Pull requests that update Typescript code labels Jul 20, 2026
@coderabbitai

coderabbitai Bot commented Jul 20, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 4d21412e-40bb-41bd-acd4-934cf31d58b4

📥 Commits

Reviewing files that changed from the base of the PR and between 2e06c1d and 050ca0d.

📒 Files selected for processing (2)
  • web/src/hooks/logic-hooks.ts
  • web/src/pages/next-chats/hooks/use-send-chat-message.ts

📝 Walkthrough

Walkthrough

Regeneration now preserves message IDs and explicitly sends truncated history. Message submission distinguishes an explicitly empty history from an omitted history, allowing existing conversations to fall back to derived history.

Changes

Chat history handling

Layer / File(s) Summary
Select authoritative message history
web/src/pages/next-chats/hooks/use-send-chat-message.ts
sendMessage honors explicitly supplied arrays, including empty arrays, while Enter submission omits empty current history to use derived history.
Preserve identifiers during regeneration
web/src/hooks/logic-hooks.ts
Message regeneration retains the existing ID, removes UUID generation, and passes truncated history explicitly, including an empty array for the first question.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Poem

I’m a rabbit with messages neatly in line,
Keeping each ID stable and fine.
Empty histories now speak clear,
Truncated trails reach the backend’s ear.
Hop, send, regenerate—
No fresh UUIDs to complicate!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: chat message regeneration history truncation.
Description check ✅ Passed The description follows the template and provides clear summary, background context, and implementation details.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@euvre euvre added the ci Continue Integration label Jul 20, 2026
@wangq8
wangq8 requested a review from dcc123456 July 20, 2026 05:34
@wangq8
wangq8 merged commit 94708f8 into infiniflow:main Jul 20, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧰 typescript Pull requests that update Typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants