diff --git a/app/docs/mcp-parity.md b/app/docs/mcp-parity.md index f2fac9a9..64c9a43e 100644 --- a/app/docs/mcp-parity.md +++ b/app/docs/mcp-parity.md @@ -230,10 +230,10 @@ Keep this file current when adding an action or a tool. A new action is either c ## Embeddings and UMAP -| Action / query | MCP | Notes | -| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------- | -| `createEmbeddingRun`, `processEmbeddingRun`, `updateEmbeddingRun`, `deleteEmbeddingRun`, `fetchEmbeddingRun`, `fetchEmbeddingRuns`, `fetchDocSections` | human-only | Admin; `search` uses the newest run, and agent writes refresh it | -| `createUmapRun`, `projectUmapRun`, `updateUmapRun`, `deleteUmapRun`, `regenerateUmapRegionsAction`, `fetchUmapRun`, `fetchUmapRuns`, `fetchUmapPointsForRun`, `fetchUmapRegionDetail`, `fetchLatestUmapRun`, `fetchUmapPoints`, `fetchNearbyUmapDocs` | human-only | The map view; `search` covers "notes like this" | +| Action / query | MCP | Notes | +| ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------- | ---------------------------------------------------------------- | +| `createEmbeddingRun`, `processEmbeddingRun`, `updateEmbeddingRun`, `deleteEmbeddingRun`, `fetchEmbeddingRun`, `fetchEmbeddingRuns`, `fetchDocSections` | human-only | Admin; `search` uses the newest run, and agent writes refresh it | +| `createUmapRun`, `projectUmapRun`, `updateUmapRun`, `deleteUmapRun`, `regenerateUmapRegionsAction`, `fetchUmapRun`, `fetchUmapRuns`, `fetchUmapRegionDetail`, `fetchLatestUmapRun`, `fetchUmapPoints`, `fetchNearbyUmapDocs` | human-only | The map view; `search` covers "notes like this" | ## Admin and auth diff --git a/app/src/features/umap/detail-types.ts b/app/src/features/umap/detail-types.ts index 668eaf69..8bb3914e 100644 --- a/app/src/features/umap/detail-types.ts +++ b/app/src/features/umap/detail-types.ts @@ -5,6 +5,5 @@ import type { export type UmapDetailData = { meta: UmapRunDetail; - dims: number; points: UmapPoint[]; }; diff --git a/app/src/features/umap/useUmapCanvas.ts b/app/src/features/umap/useUmapCanvas.ts index d271d5f7..2a00fb67 100644 --- a/app/src/features/umap/useUmapCanvas.ts +++ b/app/src/features/umap/useUmapCanvas.ts @@ -35,7 +35,7 @@ export function useUmapCanvas(props: UseUmapCanvasProps) { createEffect(() => { const detail = props.detail(); const points = detail?.points ?? []; - const dims = detail?.dims ?? 2; + const dims = detail?.meta.dims ?? 2; const regions = detail?.meta.regions ?? null; const width = canvasWidth(); const height = canvasHeight(); diff --git a/app/src/routes/umap/[id]/index.tsx b/app/src/routes/umap/[id]/index.tsx index 401facc0..4a4a523c 100644 --- a/app/src/routes/umap/[id]/index.tsx +++ b/app/src/routes/umap/[id]/index.tsx @@ -9,10 +9,8 @@ import { import { Link } from "~/components/ui/link"; import { Text } from "~/components/ui/text"; import { Box, Container, Grid, Stack } from "styled-system/jsx"; -import { - fetchUmapPointsForRun, - fetchUmapRun, -} from "~/services/umap/umap.queries"; +import { fetchUmapPoints } from "~/services/umap.service"; +import { fetchUmapRun } from "~/services/umap/umap.queries"; import { deleteUmapRun, regenerateUmapRegionsAction, @@ -39,20 +37,16 @@ const UmapDetail: VoidComponent = () => { const run = await fetchUmapRun(params.id); if (!run) return null; - const pointsData = await fetchUmapPointsForRun(params.id).catch(() => null); + const points = await fetchUmapPoints(params.id).catch(() => []); - return { - meta: run, - dims: pointsData?.dims ?? run.dims, - points: pointsData?.points ?? [], - }; + return { meta: run, points }; }); const { setCanvasContainerEl, setCanvasEl } = useUmapCanvas({ detail }); const refreshPoints = () => { if (!params.id) return; - void revalidate(fetchUmapPointsForRun.keyFor(params.id)); + void revalidate(fetchUmapPoints.keyFor(params.id)); }; const handleDelete = async () => { @@ -78,7 +72,7 @@ const UmapDetail: VoidComponent = () => { await runRegenerateRegions({ id: params.id }); await Promise.all([ revalidate(fetchUmapRun.keyFor(params.id)), - revalidate(fetchUmapPointsForRun.keyFor(params.id)), + revalidate(fetchUmapPoints.keyFor(params.id)), ]); } catch (error) { console.error(error); @@ -131,7 +125,7 @@ const UmapDetail: VoidComponent = () => { alignItems="start" > => { - "use server"; - if (!runId) throw new Error("Missing runId"); - const run = await prisma.umapRun.findUnique({ - where: { id: runId }, - select: { id: true, dims: true }, - }); - if (!run) throw new Error("Run not found"); - - const pts = await prisma.umapPoint.findMany({ - where: { runId, doc: { archivedAt: null } }, - select: { docId: true, x: true, y: true, z: true }, - }); - return { - runId, - dims: run.dims, - points: pts.map((p) => ({ - docId: p.docId, - x: p.x, - y: p.y, - z: p.z, - })), - }; - }, - "umap-points", -); - export const fetchUmapRegionDetail = query( async (input: { runId: string; diff --git a/app/tests/unit/query-keys.test.ts b/app/tests/unit/query-keys.test.ts new file mode 100644 index 00000000..24cddc30 --- /dev/null +++ b/app/tests/unit/query-keys.test.ts @@ -0,0 +1,72 @@ +import { readdirSync, readFileSync } from "node:fs"; +import { join, relative } from "node:path"; +import ts from "typescript"; +import { describe, expect, it } from "vitest"; + +const SRC = join(__dirname, "../../src"); +const UNREADABLE = "(name the scan cannot read)"; + +/** A router import that registers queries under a name the scan can't follow. */ +const hidesQueries = (binding: ts.NamedImportBindings) => + ts.isNamespaceImport(binding) || + binding.elements.some((element) => { + const imported = (element.propertyName ?? element.name).text; + if (imported === "cache") return true; + return imported === "query" && element.name.text !== "query"; + }); + +/** Every `query(fn, name)` call in a file that imports the router's `query`. */ +const queryNames = (file: string) => { + const text = readFileSync(file, "utf8"); + if (!text.includes("@solidjs/router")) return []; + const source = ts.createSourceFile(file, text, ts.ScriptTarget.Latest); + const found: { name: string; file: string }[] = []; + const add = (name: string) => found.push({ name, file: relative(SRC, file) }); + const imports = source.statements.flatMap((node) => + ts.isImportDeclaration(node) && + ts.isStringLiteral(node.moduleSpecifier) && + node.moduleSpecifier.text === "@solidjs/router" && + node.importClause?.namedBindings + ? [node.importClause.namedBindings] + : [], + ); + if (imports.some(hidesQueries)) add(UNREADABLE); + // Elsewhere `query()` is usually a search-text signal. + const importsQuery = imports.some( + (binding) => + ts.isNamedImports(binding) && + binding.elements.some((element) => element.name.text === "query"), + ); + const walk = (node: ts.Node) => { + if ( + importsQuery && + ts.isCallExpression(node) && + ts.isIdentifier(node.expression) && + node.expression.text === "query" + ) { + const [, name, ...rest] = node.arguments; + const literal = name && ts.isStringLiteralLike(name) && !rest.length; + add(literal ? name.text : UNREADABLE); + } + ts.forEachChild(node, walk); + }; + walk(source); + return found; +}; + +describe("router query names", () => { + // The router caches by name plus arguments. Two queries with one name read + // each other's entries, whatever their return types say. + it("are readable and used once each", () => { + const all = readdirSync(SRC, { recursive: true, encoding: "utf8" }) + .filter((path) => /\.tsx?$/.test(path)) + .flatMap((path) => queryNames(join(SRC, path))); + const byName = Map.groupBy(all, (query) => query.name); + const clashes = [...byName] + .filter(([name, uses]) => uses.length > 1 || name === UNREADABLE) + .map(([name, uses]) => `${name}: ${uses.map((u) => u.file).join(", ")}`); + + expect(all.length).toBeGreaterThan(50); + expect(clashes).toEqual([]); + }); +});