Skip to content

fix(search): chat history leaves out inbox turns, reads newest first, and uses an indexed scan - #10560

Open
waleedlatif1 wants to merge 2 commits into
stagingfrom
fix/chat-history-audit
Open

waleedlatif1 wants to merge 2 commits into
stagingfrom
fix/chat-history-audit

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #10537 / #10548 from an audit of list_my_chat_history. No privacy leak was found; each item below was checked against the code first.

Summary

  • Inbox email turns are never the caller's questions. Inbox tasks save the email as a user message without an origin and run it as the workspace owner, and thread replies from any sender append to that chat. The history now leaves out any question whose copy (same message id and time, which forks keep) sits in a chat an inbox task points to. I did not add an inbox turn origin: TurnOrigin is generated from the Mothership protocol, and the anti-join already covers rows written before and after any stamp, forks included, with one rule shared with the adoption backfill (fix(insights): adoption history audit — consistent sources, buffered writer, UTC day bounds #10557)
  • Consistent with list_my_activity. Newest first by default (sortBy: 'newest' | 'oldest'; the cursor carries its order and is refused for the other one); startDate optional, defaulting to 7 days before endDate; a later endDate reads up to now; a startDate in the future says so. The Search MCP tool takes listMyChatHistoryMcpSchema (dates via mcpDateFilter: no offset reads as UTC, and tools/list carries no long regex). The Organization Search block gets an optional Start Date and an Order field
  • Reads.
    • New partial index copilot_messages_chat_user_created_at_idx on (chat_id, created_at, id) WHERE role = 'user' AND deleted_at IS NULL (0481, CREATE INDEX CONCURRENTLY below an embedded COMMIT, replay-safe as 0472; renumber at merge if needed)
    • The answer lookup is a seq range on copilot_messages_chat_seq_idx: the first reply after the question and before the chat's next question
    • The question (2,000) and answer (4,000) are cut in SQL with left() before the excerpt is made
  • OAuth consent copy for search:read now says it lists your own questions to Sim

Before / after (seeded local Postgres, 15,000 chats and 600,000 messages, one person with 300 chats; 30-day window, includeAnswers)

  • Before: Bitmap Index Scan on copilot_messages_chat_created_at_idx returns 3,949 rows (questions and replies) and the heap scan keeps 1,968; each answer is an Incremental Sort + Nested Loop Anti Join over the chat; about 9,250 shared buffers, about 12 ms warm
  • After: Bitmap Index Scan on copilot_messages_chat_user_created_at_idx returns exactly the 1,968 questions; each answer is one Index Scan using copilot_messages_chat_seq_idx with a seq range; inbox anti-join is a hash join over the (empty here) inbox tasks; about 7,200 shared buffers, about 11 ms warm
  • The gain grows with the reply share and transcript length; timings here are small because the seed fits in memory

Type of Change

  • Bug fix

Testing

  • lib/knowledge/application/chat-history.integration.ts (14): adds newest-first default with an order-bound cursor (red without the order check), the default 7-day window and the future-startDate refusal (red without the guard), and an inbox email turn plus its forked copy never listed (red without the anti-join); existing ownership, mode-access, window, paging and answer cases run on seq-numbered fixtures
  • lib/mothership/tools/server/knowledge/chat-history.integration.ts (3): the Assistant tool lists the member's own questions, is refused outside Assistant mode (red without the mode check) and when the organization has no Search (red without requireOrganizationSearchAvailable, which the Search MCP path shares through the same use case)
  • lib/internal/organization-search/execute-tool.integration.ts still green
  • Unit suites: lib/knowledge/mcp, lib/api/contracts, lib/mothership/tools/server, blocks, tools/organization, OAuth consent, lib/auth
  • bun run generate, generated tool catalog from the Mothership staging catalog, check:migrations, lint, check:file-names, check:boundaries, check:source-conventions, check:comment-hygiene, check:tool-effects, check:canvas-sentences, check:integration-catalog, check:unused-exports, block registry check

Mothership follow-up (not in this PR)

  • Regenerate sim-assistant-tools.generated.ts after merge: the input schema adds sortBy and makes startDate optional

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing (new tests pass the test-audit authoring gate)
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Dec 1, 2026 9:19am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5 · The latest changes appear safe to merge; no new blocking issue was found.RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge; no new blocking issue was found.

Summary

Chat history excludes inbox-linked questions and their copies, defaults to newest first, and uses bounded reads with a new question index.

  • Chat history leaves inbox email turns and their copied turns out.
  • Chat history defaults to the latest week and shows newest questions first.
  • Chat history reads questions and answer excerpts with less transcript data.

Reviews (6) · Last reviewed commit: "fix(search): never show thinking cut off..." · Reviewed by Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 19 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1
waleedlatif1 force-pushed the fix/chat-history-audit branch from c5b6a01 to 3c70422 Compare December 1, 2026 09:04
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 19 files

Reply with feedback, questions, or to request a fix.

Turn on auto-fix | Re-trigger cubic

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 19 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

You've manually re-run cubic several times on this PR. Each manual re-review checks the full PR again and counts toward your usage quota. To preserve your usage limits, we recommend letting cubic automatically review new commits.

Turn on auto-fix | Re-trigger cubic

This branch was previously deployed

1 inactive deployment
Preview — 7af1d052 Deployed Dec 1, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant