fix(ctxmgr): normalize dismissed_unread_ids for Postgres array insert

Ensure nil dismissed ID slices serialize as empty bigint arrays and
preallocate compressDismissed output to avoid NULL write failures.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
WeiCHEN
2026-09-13 02:50:21 +00:00
committed by weichen
co-authored by Cursor
parent 222b0c2dd6
commit 0d395f2bf2
+7 -3
View File
@@ -328,13 +328,17 @@ func (m *ContextManager) markChatReadPG(ctx context.Context, req MarkChatReadReq
pos.LastReadMessageID, pos.DismissedUnreadIDs = compressDismissed(pos.LastReadMessageID, pos.DismissedUnreadIDs, futureIDs)
pos.UpdatedAt = time.Now()
if pos.DismissedUnreadIDs == nil {
pos.DismissedUnreadIDs = []int64{}
}
_, err = tx.Exec(ctx, `
INSERT INTO chat_read_positions (session_id, user_id, last_read_message_id, dismissed_unread_ids, updated_at)
VALUES ($1, $2, $3, $4, $5)
VALUES ($1, $2, $3, COALESCE($4, '{}'::bigint[]), $5)
ON CONFLICT (session_id, user_id)
DO UPDATE SET
last_read_message_id = EXCLUDED.last_read_message_id,
dismissed_unread_ids = EXCLUDED.dismissed_unread_ids,
dismissed_unread_ids = COALESCE(EXCLUDED.dismissed_unread_ids, '{}'::bigint[]),
updated_at = EXCLUDED.updated_at
`, pos.SessionID, pos.UserID, pos.LastReadMessageID, pos.DismissedUnreadIDs, pos.UpdatedAt)
if err != nil {
@@ -375,7 +379,7 @@ func compressDismissed(baseline int64, dismissed []int64, futureMsgIDs []int64)
}
}
var newDismissed []int64
newDismissed := make([]int64, 0, len(dMap))
for id := range dMap {
if id > baseline {
newDismissed = append(newDismissed, id)