Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Jan 2, 2026

This is an automated pull request to merge tofik/agent-chat-history-clears-on-close into dev.
It was created by the [Auto Pull Request] action.

@vercel
Copy link

vercel bot commented Jan 2, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
app Ready Ready Preview, Comment Jan 2, 2026 8:15pm
1 Skipped Deployment
Project Deployment Review Updated (UTC)
portal Skipped Skipped Jan 2, 2026 8:15pm

@cursor
Copy link

cursor bot commented Jan 2, 2026

PR Summary

Introduces ephemeral assistant chat history persistence and UI integration.

  • Adds AssistantChatModule with GET/PUT/DELETE /v1/assistant-chat/history guarded by HybridAuthGuard, using @upstash/redis (with in-memory fallback) and a 7-day TTL; includes DTO validation and types
  • Wires module in AppModule and documents endpoints/schemas in openapi.json
  • Updates /api/chat route to require org context (X-Organization-Id or session), validate membership via db.member, and enriches system prompt with org/date context
  • Enhances components/ai/chat.tsx to hydrate from server history, persist changes (debounced, keepalive on unmount), scope by orgId:userId, send header X-Organization-Id, and add a "Clear chat" action

Written by Cursor Bugbot for commit 6adab96. This will update automatically on new commits. Configure here.

@graphite-app
Copy link

graphite-app bot commented Jan 2, 2026

Graphite Automations

"Auto-assign PRs to Author" took an action on this PR • (01/02/26)

1 reviewer was added to this PR based on Mariano Fuentes's automation.

@vercel vercel bot temporarily deployed to Preview – portal January 2, 2026 20:13 Inactive
@tofikwest tofikwest merged commit 4a47764 into main Jan 2, 2026
14 of 15 checks passed
@tofikwest tofikwest deleted the tofik/agent-chat-history-clears-on-close branch January 2, 2026 20:15
role: m.role,
text,
createdAt: Date.now(),
};
Copy link

Choose a reason for hiding this comment

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

Message timestamps reset on every chat load

The save effect always sets createdAt: Date.now() for all messages, overwriting the original timestamps loaded from the server. When the hydration effect completes, it sets lastSavedJsonRef.current to the server's JSON (with original timestamps), then sets isHydratingRef.current = false. The save effect then runs with messages that have new Date.now() timestamps, causing the JSON comparison to fail and triggering an immediate re-save. This means every time a user opens the chat, all historical message timestamps are replaced with the current time, causing progressive data loss without any user action.

Additional Locations (1)

Fix in Cursor Fix in Web

status: res.status,
error: res.error,
});
}
Copy link

Choose a reason for hiding this comment

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

GET failure allows overwrites causing server data loss

When the GET request for chat history fails (error or non-200 status), the code logs the error but continues execution without returning early. It then sets lastSavedJsonRef.current = "[]" and isHydratingRef.current = false, which allows the save effect to run when the user types new messages. If the user sends a message after a GET failure, the subsequent PUT request will overwrite the server's existing history with only the new message, causing permanent data loss. The error handler needs to return early to prevent saves when the initial load fails.

Fix in Cursor Fix in Web

return () => {
controller.abort();
};
}, [resolvedOrganizationId, setMessages, userId]);
Copy link

Choose a reason for hiding this comment

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

Messages sent during hydration are lost from UI

If a user sends a message while the hydration fetch is in progress, the message will be lost from the UI. The sequence is: hydration starts and sets isHydratingRef.current = true, user sends a message via sendMessage, the message is added to the useChat internal state and sent to the AI, then the GET request completes and setMessages(uiMessages) overwrites the state with server history, removing the user's newly sent message. The AI will still process and respond to the message, but the user's original message disappears from the conversation UI, causing a confusing mismatch between the displayed conversation and the AI's response.

Fix in Cursor Fix in Web

@claudfuen
Copy link
Contributor

🎉 This PR is included in version 1.72.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants