← Back to Pull requests

fix(chat): keep chat history per visitor, not per agent owner #64

Closed
unknown closed this pull request without merging on Nov 27, 2026 11:48 UTC
View on GitHub
unknown unknown commented on Nov 25, 2026 09:11 UTC
Author

Problem (privacy)


  • The gateway's /api/v1/chat stored history in sessions.get(userId).

  • The dashboard chat route always sent userId: owner.id and conversationId: agentId.
  • So every visitor talking to any of an owner's agents was appended to one shared transcript. A visitor's messages were sent to the model along with every earlier visitor's messages, which leaks one customer's context to the next and mixes agents together.

    Fix


  • Gateway: sessions are keyed by userId:conversationId when a conversation id is given. It still falls back to userId, so existing callers and tests are unchanged.

  • Dashboard route: conversationId is now ${agentId}:${visitorId}. visitorId comes from the request and must match ^[A-Za-z0-9_-]{8,64}$. A missing or malformed id gets a fresh crypto.randomUUID(), so the fallback is "no history", never "shared history".

  • ChatWidget: creates one visitor id per agent in localStorage and sends it.

  • Also fixed in the widget: it read data.data?.response, but the route returns { reply }, so the widget always showed its fallback text instead of the agent's answer.
  • Tests


  • Gateway integration: two visitors of the same owner get separate histories, and a returning visitor keeps theirs.

  • Dashboard: the conversation id is agent + visitor scoped. Missing or malformed ids get distinct random ids.
  • Both fail on main.
    <!-- devin-review-badge-begin -->

    ---

    <a href="https://app.devin.ai/review/mangeshraut712/gravity-saas-agent/pull/64" target="_blank"><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.devin.ai/assets/gh-devin-review-dark.svg?v=4"><img src="https://static.devin.ai/assets/gh-devin-review-light.svg?v=4" alt="Devin Review"></picture></a>
    <!-- devin-review-badge-end -->

    <!-- CURSOR_SUMMARY -->
    ---

    [!NOTE]

    High Risk

    Changes multi-tenant chat session isolation and what prior messages are sent to the model—directly affects visitor privacy and cross-customer data leakage.

    > Overview

    Fixes a privacy bug where every visitor chatting with an owner's agents shared one gateway transcript because sessions were keyed only by userId and the dashboard sent conversationId: agentId.

    > The gateway now keys in-memory sessions with a JSON [userId, conversationId] tuple (with collision-safe encoding), so each conversation keeps its own message history while still falling back to userId when no conversation id is sent.

    > The dashboard chat API accepts a validated visitorId from the client (or mints a random UUID when missing/invalid—never a shared fallback) and forwards conversationId: ${agentId}:${visitorId} to the gateway.

    > ChatWidget adds visitorIdFor() to persist one visitor id per agent via localStorage (with in-memory fallback when storage is blocked), includes it on each request, and reads the agent text from { reply } as the route actually returns.

    > Coverage adds gateway integration cases (isolated visitors, conversation continuation, id collision) plus unit tests for conversation scoping and visitorIdFor.

    > <sup>Reviewed by Cursor Bugbot for commit 8d787051e3937f2ecf37f6e5256dc94f13b177a9. Bugbot is set up for automated code reviews on this repo. Configure here.</sup>

    <!-- /CURSOR_SUMMARY -->

    Diff: 423 lines (+317 / -106) across 5 files Reviewer: mangeshraut712 Review duration: 3037m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 423 lines diff across 5 files
    0.45
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.362
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #64"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.1% (MEDIUM) · Higher = larger blast radius
    45.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 17.8% · Safe to merge
    17.8%