From 32a7e37c2bd49d8efb227b5afeba00d625a934d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Salavisa?= Date: Tue, 10 Mar 2026 21:19:06 +0000 Subject: [PATCH] fix: filter pending comments by session to prevent cross-session leakage (#408) --- .../task/chat/use-chat-panel-state.ts | 15 ++++++++----- .../domains/comments/use-diff-comments.ts | 9 ++++++-- .../domains/comments/use-pending-comments.ts | 22 ++++++++++++++----- 3 files changed, 33 insertions(+), 13 deletions(-) diff --git a/apps/web/components/task/chat/use-chat-panel-state.ts b/apps/web/components/task/chat/use-chat-panel-state.ts index 1c8f3e20ce..c9d78914b4 100644 --- a/apps/web/components/task/chat/use-chat-panel-state.ts +++ b/apps/web/components/task/chat/use-chat-panel-state.ts @@ -257,9 +257,10 @@ export function useCommentsState(resolvedSessionId: string | null): CommentsStat useEffect(() => { if (resolvedSessionId) hydrateComments(resolvedSessionId); }, [resolvedSessionId, hydrateComments]); - const planComments = usePendingPlanComments(); - const pendingCommentsByFile = usePendingDiffCommentsByFile(); - const pendingPRFeedback = usePendingPRFeedback(); + // Filter pending comments by sessionId to prevent cross-session leakage + const planComments = usePendingPlanComments(resolvedSessionId); + const pendingCommentsByFile = usePendingDiffCommentsByFile(resolvedSessionId); + const pendingPRFeedback = usePendingPRFeedback(resolvedSessionId); const markCommentsSent = useCommentsStore((state) => state.markCommentsSent); const removeComment = useCommentsStore((state) => state.removeComment); const clearSessionPlanComments = useCallback(() => { @@ -287,13 +288,17 @@ export function useCommentsState(resolvedSessionId: string | null): CommentsStat [removeComment], ); const handleClearPRFeedback = useCallback(() => { + if (!resolvedSessionId) return; const state = useCommentsStore.getState(); const allPending = [...state.pendingForChat]; for (const id of allPending) { const c = state.byId[id]; - if (c && isPRFeedbackComment(c)) state.removeComment(id); + // Only clear PR feedback for the current session + if (c && isPRFeedbackComment(c) && c.sessionId === resolvedSessionId) { + state.removeComment(id); + } } - }, []); + }, [resolvedSessionId]); return { planComments, pendingCommentsByFile, diff --git a/apps/web/hooks/domains/comments/use-diff-comments.ts b/apps/web/hooks/domains/comments/use-diff-comments.ts index 9b478f3713..9b79048152 100644 --- a/apps/web/hooks/domains/comments/use-diff-comments.ts +++ b/apps/web/hooks/domains/comments/use-diff-comments.ts @@ -34,8 +34,11 @@ const EMPTY_BY_FILE: Record = {}; /** * Get all pending diff comments grouped by file path. + * If sessionId is provided, only returns comments belonging to that session. */ -export function usePendingDiffCommentsByFile(): Record { +export function usePendingDiffCommentsByFile( + sessionId?: string | null, +): Record { const byId = useCommentsStore((state) => state.byId); const pendingForChat = useCommentsStore((state) => state.pendingForChat); @@ -45,10 +48,12 @@ export function usePendingDiffCommentsByFile(): Record { for (const id of pendingForChat) { const comment = byId[id]; if (comment && isDiffComment(comment)) { + // Filter by sessionId if provided + if (sessionId && comment.sessionId !== sessionId) continue; if (!byFile[comment.filePath]) byFile[comment.filePath] = []; byFile[comment.filePath].push(comment); } } return Object.keys(byFile).length === 0 ? EMPTY_BY_FILE : byFile; - }, [byId, pendingForChat]); + }, [byId, pendingForChat, sessionId]); } diff --git a/apps/web/hooks/domains/comments/use-pending-comments.ts b/apps/web/hooks/domains/comments/use-pending-comments.ts index 925cc5e061..8fe294903f 100644 --- a/apps/web/hooks/domains/comments/use-pending-comments.ts +++ b/apps/web/hooks/domains/comments/use-pending-comments.ts @@ -51,8 +51,9 @@ export function usePendingDiffComments(): DiffComment[] { /** * Get all pending plan comments. + * If sessionId is provided, only returns comments belonging to that session. */ -export function usePendingPlanComments(): PlanComment[] { +export function usePendingPlanComments(sessionId?: string | null): PlanComment[] { const byId = useCommentsStore((state) => state.byId); const pendingForChat = useCommentsStore((state) => state.pendingForChat); @@ -61,16 +62,21 @@ export function usePendingPlanComments(): PlanComment[] { const pending: PlanComment[] = []; for (const id of pendingForChat) { const comment = byId[id]; - if (comment && isPlanComment(comment)) pending.push(comment); + if (comment && isPlanComment(comment)) { + // Filter by sessionId if provided + if (sessionId && comment.sessionId !== sessionId) continue; + pending.push(comment); + } } return pending.length === 0 ? EMPTY_PLAN_COMMENTS : pending; - }, [byId, pendingForChat]); + }, [byId, pendingForChat, sessionId]); } /** * Get all pending PR feedback comments. + * If sessionId is provided, only returns comments belonging to that session. */ -export function usePendingPRFeedback(): PRFeedbackComment[] { +export function usePendingPRFeedback(sessionId?: string | null): PRFeedbackComment[] { const byId = useCommentsStore((state) => state.byId); const pendingForChat = useCommentsStore((state) => state.pendingForChat); @@ -79,8 +85,12 @@ export function usePendingPRFeedback(): PRFeedbackComment[] { const pending: PRFeedbackComment[] = []; for (const id of pendingForChat) { const comment = byId[id]; - if (comment && isPRFeedbackComment(comment)) pending.push(comment); + if (comment && isPRFeedbackComment(comment)) { + // Filter by sessionId if provided + if (sessionId && comment.sessionId !== sessionId) continue; + pending.push(comment); + } } return pending.length === 0 ? EMPTY_PR_FEEDBACK_COMMENTS : pending; - }, [byId, pendingForChat]); + }, [byId, pendingForChat, sessionId]); }