Files
outline/server/tools/util.test.ts
T
Tom MoorandClaude Opus 5 399cd4041d perf: Reduce database and Redis round-trips on the MCP endpoint (#13333)
* perf: Reduce database and Redis round-trips on the MCP endpoint

Profiling production traces for the /mcp endpoint showed the time is
spent in repeated permission and breadcrumb lookups rather than in the
MCP layer itself.

- Cache Document.membershipDocumentIds in Redis, mirroring the TTL and
  key pattern of User.collectionIds. The two are resolved together for
  every search and this half was uncached, measuring ~200ms.
- Resolve each collection's cached document structure once, concurrently,
  when building breadcrumbs for a batch of documents. It was awaited per
  document inside a loop, so a page of search results meant a serial
  Redis round-trip for every result.
- Walk each collection structure once into a document ID to breadcrumb
  map instead of searching the whole tree per document.
- Reuse the collection association already loaded by Document.findByPk
  when resolving a single document's breadcrumb, rather than querying it
  again under the same membership scope.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix: Invalidate cached document membership IDs on permission changes

The cache added alongside membershipDocumentIds relied on its TTL alone,
so a revoked grant stayed searchable until it expired. Clear the key on
the paths that change which documents a user is a member of, keeping the
TTL as a backstop.

- UserMembership create and destroy where documentId is set.
- GroupMembership create and destroy where documentId is set, for every
  user in the group.
- GroupUser create and destroy, as joining or leaving a group changes
  which group grants reach the user.

Sourced memberships are written with hooks disabled and destroyed without
individual hooks, so the parent membership invalidates once for the group
rather than once per descendant.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-07 19:01:02 -04:00

223 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { CollectionPermission, type NavigationNode } from "@shared/types";
import {
buildCollection,
buildDocument,
buildTeam,
buildUser,
} from "@server/test/factories";
import {
buildBreadcrumb,
getBreadcrumbsForDocuments,
optionalString,
success,
} from "./util";
const node = (
id: string,
title: string,
children: NavigationNode[] = []
): NavigationNode => ({
id,
title,
url: `/doc/${id}`,
children,
});
describe("success", () => {
it("returns a text block for empty list results", () => {
expect(success([])).toEqual({
content: [{ type: "text", text: "[]" }],
});
});
it("returns one text block per list item", () => {
expect(success([{ id: "a" }, { id: "b" }])).toEqual({
content: [
{ type: "text", text: JSON.stringify({ id: "a" }) },
{ type: "text", text: JSON.stringify({ id: "b" }) },
],
});
});
it("wraps a single object in one text block", () => {
expect(success({ success: true })).toEqual({
content: [{ type: "text", text: JSON.stringify({ success: true }) }],
});
});
});
describe("buildBreadcrumb", () => {
const structure: NavigationNode[] = [
node("a", "Onboarding", [
node("b", "Setup guide", [node("c", "Database")]),
node("d", "Glossary"),
]),
node("e", "Architecture"),
];
it("returns just the collection name for a root-level document", () => {
expect(buildBreadcrumb("a", structure, "Engineering")).toBe("Engineering");
expect(buildBreadcrumb("e", structure, "Engineering")).toBe("Engineering");
});
it("includes ancestor titles for a nested document", () => {
expect(buildBreadcrumb("b", structure, "Engineering")).toBe(
"Engineering › Onboarding"
);
expect(buildBreadcrumb("c", structure, "Engineering")).toBe(
"Engineering › Onboarding › Setup guide"
);
});
it("excludes the document's own title from the path", () => {
const result = buildBreadcrumb("c", structure, "Engineering");
expect(result).not.toContain("Database");
});
it("falls back to the collection name when the document is not in the structure", () => {
expect(buildBreadcrumb("missing", structure, "Engineering")).toBe(
"Engineering"
);
});
it("returns just the collection name when the structure is null", () => {
expect(buildBreadcrumb("a", null, "Engineering")).toBe("Engineering");
expect(buildBreadcrumb("a", undefined, "Engineering")).toBe("Engineering");
});
it("returns just the collection name when the structure is empty", () => {
expect(buildBreadcrumb("a", [], "Engineering")).toBe("Engineering");
});
});
describe("optionalString", () => {
const schema = optionalString();
it("returns undefined when input is omitted", () => {
expect(schema.parse(undefined)).toBeUndefined();
});
it("coerces an empty string to undefined", () => {
expect(schema.parse("")).toBeUndefined();
});
it("passes through a non-empty string", () => {
expect(schema.parse("hello")).toBe("hello");
});
it("preserves whitespace-only strings", () => {
expect(schema.parse(" ")).toBe(" ");
});
});
describe("getBreadcrumbsForDocuments", () => {
it("returns the collection name for a root-level document", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: CollectionPermission.ReadWrite,
name: "Engineering",
});
const doc = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
const result = await getBreadcrumbsForDocuments([doc], user);
expect(result.get(doc.id)).toBe("Engineering");
});
it("includes ancestor titles for a nested document", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: CollectionPermission.ReadWrite,
name: "Engineering",
});
const parent = await buildDocument({
teamId: team.id,
collectionId: collection.id,
title: "Onboarding",
});
const child = await buildDocument({
teamId: team.id,
collectionId: collection.id,
parentDocumentId: parent.id,
});
const result = await getBreadcrumbsForDocuments([child], user);
expect(result.get(child.id)).toBe("Engineering › Onboarding");
});
it("omits documents whose collection the user cannot read", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: null,
name: "Secrets",
});
const doc = await buildDocument({
teamId: team.id,
collectionId: collection.id,
});
const result = await getBreadcrumbsForDocuments([doc], user);
expect(result.has(doc.id)).toBe(false);
});
it("returns an empty map for empty input", async () => {
const user = await buildUser();
const result = await getBreadcrumbsForDocuments([], user);
expect(result.size).toBe(0);
});
it("omits documents that have no collection", async () => {
const user = await buildUser();
const result = await getBreadcrumbsForDocuments(
[{ id: "doc-without-collection", collectionId: null }],
user
);
expect(result.has("doc-without-collection")).toBe(false);
});
it("falls back to the collection name for a document missing from the structure", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
teamId: team.id,
permission: CollectionPermission.ReadWrite,
name: "Engineering",
});
const result = await getBreadcrumbsForDocuments(
[{ id: "not-in-structure", collectionId: collection.id }],
user
);
expect(result.get("not-in-structure")).toBe("Engineering");
});
it("resolves breadcrumbs across multiple collections in one call", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const c1 = await buildCollection({
teamId: team.id,
permission: CollectionPermission.ReadWrite,
name: "One",
});
const c2 = await buildCollection({
teamId: team.id,
permission: CollectionPermission.ReadWrite,
name: "Two",
});
const d1 = await buildDocument({ teamId: team.id, collectionId: c1.id });
const d2 = await buildDocument({ teamId: team.id, collectionId: c2.id });
const result = await getBreadcrumbsForDocuments([d1, d2], user);
expect(result.get(d1.id)).toBe("One");
expect(result.get(d2.id)).toBe("Two");
});
});