Skip to content

improvement(copilot): drop unused columns from mothership chat detail reads#4640

Merged
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/san-juan-v9
May 17, 2026
Merged

improvement(copilot): drop unused columns from mothership chat detail reads#4640
waleedlatif1 merged 1 commit into
stagingfrom
waleedlatif1/san-juan-v9

Conversation

@waleedlatif1
Copy link
Copy Markdown
Collaborator

Summary

  • Added getAccessibleCopilotChatWithMessages that selects only the columns chat-detail callers actually consume — drops previewYaml, planArtifact, config, model, pinned, lastSeenAt from the SELECT
  • Migrated mothership chat GET, resolveOrCreateChat, and process-contents to the lean helper; resolveOrCreateChat's RETURNING clause now uses the same projection
  • Kept the original full-row helper for the legacy copilot chat detail endpoint that still consumes the dropped fields
  • Cuts Postgres TOAST detoast overhead on every mothership chat load — surfaced by PlanetScale insights showing p99 1.7s / max 55.4s / 208MB max egress on the existing full-row query
  • No client/contract changes — the route response shape was already a fixed subset

Type of Change

  • Improvement

Testing

Tested manually. Type-check, API contract audit, and 96 affected tests (15 files) all pass.

Checklist

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

@vercel
Copy link
Copy Markdown

vercel Bot commented May 17, 2026

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

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
docs Skipped Skipped May 17, 2026 4:56am

Request Review

@cursor
Copy link
Copy Markdown

cursor Bot commented May 17, 2026

PR Summary

Medium Risk
Moderate risk because it changes the DB projection and return types for chat loads/creation used by mothership chat GET and resolveOrCreateChat, which could surface runtime issues if any downstream code implicitly relied on dropped columns.

Overview
Improves mothership chat load performance by introducing getAccessibleCopilotChatWithMessages, which selects only the transcript/resource fields needed for chat-detail flows and avoids fetching large unused TOAST-able columns.

Migrates the mothership chat GET route, resolveOrCreateChat (including insert RETURNING), and past-chat context loading in process-contents to use the new lean loader, and updates tests/mocks accordingly while keeping the legacy full-row loader for callers that still need the extra columns.

Reviewed by Cursor Bugbot for commit 48c026d. Configure here.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 17, 2026

Greptile Summary

This PR introduces a lean getAccessibleCopilotChatWithMessages helper that selects only the columns consumed by chat-detail callers, dropping the TOAST-heavy copilot-only fields (previewYaml, planArtifact, config) and unused metadata (model, pinned, lastSeenAt). The mothership chat GET handler, resolveOrCreateChat, and processPastChatFromDb are migrated to the new helper; the original full-row helper is retained for the legacy copilot chat detail endpoint.

  • Adds copilotChatDetailColumns column set and exports CopilotChatDetailRow type from lifecycle.ts; updates ChatLoadResult.chat from the full inferred select to the narrowed type, and tightens INSERT … RETURNING to the same projection.
  • Updates the mothership chat GET route and process-contents.ts to use the new lean loader, and adjusts the test mock accordingly.

Confidence Score: 5/5

Safe to merge — all column accesses on the narrowed type are present in CopilotChatDetailRow, downstream callers of resolveOrCreateChat only touch title or don't use chat at all, and the INSERT RETURNING projection is consistent with the new type.

The change correctly narrows three independent read paths to a well-defined column subset. TypeScript enforces the boundary: any caller that tried to read a dropped field (e.g. model, pinned, previewYaml) would fail at compile time, and the PR description notes the type-check passed across 96 affected tests. The CopilotChatDetailRow type and copilotChatDetailColumns record are manually kept in sync, which is a minor maintenance concern but not a defect — the two definitions match today and a mismatch would produce a Drizzle type error at the call site.

No files require special attention.

Important Files Changed

Filename Overview
apps/sim/lib/copilot/chat/lifecycle.ts Adds copilotChatDetailColumns, CopilotChatDetailRow, and getAccessibleCopilotChatWithMessages; migrates resolveOrCreateChat to the lean loader and tightens RETURNING clause — logic is correct and type-safe.
apps/sim/app/api/mothership/chats/[chatId]/route.ts GET handler switched to getAccessibleCopilotChatWithMessages; every field accessed on chat (id, title, messages, conversationId, resources, createdAt, updatedAt) is present in CopilotChatDetailRow.
apps/sim/lib/copilot/chat/process-contents.ts Switches processPastChatFromDb to the lean helper; only messages, workspaceId, and workflowId are read, all present in the new type.
apps/sim/app/api/mothership/chats/[chatId]/route.test.ts Mock correctly updated to expose getAccessibleCopilotChatWithMessages alongside getAccessibleCopilotChatAuth.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Chat Detail Request] --> B{Chat Type?}
    B -- mothership GET --> C[getAccessibleCopilotChatWithMessages]
    B -- legacy copilot GET --> D[getAccessibleCopilotChat]
    B -- resolveOrCreateChat --> E{chatId provided?}

    E -- yes --> C
    E -- no --> F[INSERT copilotChats .returning copilotChatDetailColumns]

    C --> G[SELECT copilotChatDetailColumns id, userId, workflowId, workspaceId, type, title, messages, conversationId, resources, createdAt, updatedAt]
    D --> H[SELECT * all columns incl. previewYaml, planArtifact, config, model, pinned, lastSeenAt]

    G --> I[authorizeCopilotChatRow]
    F --> J[CopilotChatDetailRow]
    H --> I
    I --> J
Loading

Reviews (1): Last reviewed commit: "improvement(copilot): drop unused column..." | Re-trigger Greptile

@waleedlatif1 waleedlatif1 merged commit fd12137 into staging May 17, 2026
9 of 10 checks passed
@waleedlatif1 waleedlatif1 deleted the waleedlatif1/san-juan-v9 branch May 17, 2026 06:05
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