#2599·chat-ui

Shared conversation view returns 500 for logged-out visitors

Author: gary149Created Sep 21, 2026Updated Sep 21, 2026

Symptom

Opening a public share link (/chat/r/<shareId>) while logged out redirects to the home page instead of showing the conversation. The underlying call GET /api/v2/conversations/<shareId> returns HTTP 500 ({"message":"An error occurred"}). A random 7-character id returns a proper 404, so the share lookup itself works and the crash happens after it. Reproduced on production.

Cause

src/routes/api/v2/conversations/[id]/+server.ts looks up the last assistant message's turn state:

typescript
const turnStateDoc = lastAssistant
	? await collections.turnStates
			.findOne({ conversationId: new ObjectId(conversation._id), messageId: lastAssistant.id })
			.catch(() => null)
	: null;

For a share view, resolveConversation returns a sharedConversations document whose _id is the 7-character share id. new ObjectId("abcdefg") throws synchronously ("Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer"), so the .catch() on the query promise never runs and the request fails. The comment above the lookup ("ObjectId accepts both forms") is wrong for share ids.

Introduced in #2532 (turn continuity), so anonymous share links have been broken since then. Logged-in users are unaffected because /r/[id] imports the share and redirects to the imported conversation.

Suggested fix

Skip the turn-state lookup when conversation.shared is true, or guard it with ObjectId.isValid(conversation._id). Add a test for the share-id path so the GET handler can't regress on it.