Files
shadowdaoandClaude Opus 5 c3bbea5134 feat: add memory.patch, trim memory.get, unify the memory write path
memory.get no longer returns the embedding and content_tsv
----------------------------------------------------------
It used a bare select() and returned the raw DB row, while memory.list
and memory.search already projected an explicit 9-field shape. On a
~13k-char memory those two internal columns were 55% of the response
and pushed it past the MCP tool-output cap, so large memories could not
be fetched inline at all. memory.get now returns the same 9 fields as
its siblings; user_id is still selected for the authorization check and
stripped before responding.

memory.patch
------------
memory.update only accepts full replacement, so adding one line to a
large document meant resending the whole document — expensive enough
that edits were being skipped rather than risk silently truncating
shared team documents.

memory.patch replaces one exact occurrence of old_string. An absent or
ambiguous match is an error, never a silent no-op and never an
arbitrary pick; that refusal is what makes the operation safe to hand
to an agent. The semantics live in lib/memory-patch.ts as a pure
function, free of DB and auth, so both surfaces share them.

Shared mutation layer
---------------------
The MCP tools and the Web UI Server Actions each reimplemented
authorize -> mutate -> re-embed -> CAS -> audit, and had drifted. Both
now route through lib/memory-mutations.ts.

BEHAVIOUR CHANGE: memory.delete over MCP skipped the project ACL
whenever the caller authored the row, so a memory written while a share
was rw stayed deletable by its author after an owner downgraded that
share to ro. memory.update and the whole Web UI always checked.
Authoring a row now grants no standing write privilege on any path.

The one deliberate difference between the surfaces is injected as a
ProjectResolver: MCP refuses an unknown project key so an agent cannot
spawn near-miss projects off a typo, while the Web UI creates one
because a person typing a name into a form means to.

Tests and lint
--------------
Adds vitest. The integration tests run against a real Postgres rather
than a mocked DB. The embedder sidecar is the only stub and it is
deterministic per-text, so re-embedding is verified by asserting the
stored vector actually changed rather than that a mock was called. One
test pins that content_tsv is a generated column and therefore cannot
rot after a patch — only the embedding needs an explicit recompute.

pnpm lint previously dropped into an interactive `next lint` setup
prompt and exited 1; ESLint had never been configured here. Replaced
with the ESLint CLI and a flat config bridging eslint-config-next
through FlatCompat. Clean at --max-warnings=0.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-11 14:58:11 -07:00

191 lines
7.0 KiB
TypeScript

import Link from "next/link";
import { and, desc, eq, inArray, isNull, or, sql, count } from "drizzle-orm";
import { auth } from "@/auth";
import { db } from "@/lib/db/client";
import { memories, projects, projectShares } from "@/lib/db/schema";
import { getUserGroupNames, readableProjectIds } from "@/lib/access";
import { Container, PageHeader } from "@/app/_components/ui/container";
import { Card, CardBody } from "@/app/_components/ui/card";
import { Badge } from "@/app/_components/ui/badge";
import { Button } from "@/app/_components/ui/button";
import { EmptyState } from "@/app/_components/ui/empty-state";
export const dynamic = "force-dynamic";
export default async function DashboardPage() {
const session = await auth();
const userId = session!.user.id;
const groupNames = await getUserGroupNames(userId);
// Visibility widening — recent + counts include memories under
// projects shared with the user's groups.
const accessibleIds = await readableProjectIds(userId, groupNames);
const visibility =
accessibleIds.length > 0
? or(eq(memories.userId, userId), inArray(memories.projectId, accessibleIds))
: eq(memories.userId, userId);
// Dashboard's "Projects" card stays owned-only — the list of projects
// you actively own. Shared projects show up via the memory list and
// the per-project page; surfacing them here would make the panel
// confusing about who owns what.
const [counts, recent, topProjects] = await Promise.all([
db
.select({
total: count(memories.id),
})
.from(memories)
.where(and(visibility!, isNull(memories.deletedAt))),
db
.select({
id: memories.id,
content: memories.content,
scope: memories.scope,
tags: memories.tags,
createdAt: memories.createdAt,
projectId: memories.projectId,
projectKey: projects.key,
})
.from(memories)
.leftJoin(projects, eq(memories.projectId, projects.id))
.where(and(visibility!, isNull(memories.deletedAt)))
.orderBy(desc(memories.createdAt))
.limit(5),
db
.select({
id: projects.id,
key: projects.key,
displayName: projects.displayName,
memoryCount: sql<number>`count(${memories.id})::int`,
})
.from(projects)
.leftJoin(
memories,
and(eq(memories.projectId, projects.id), isNull(memories.deletedAt)),
)
.where(eq(projects.userId, userId))
.groupBy(projects.id)
.orderBy(desc(sql`count(${memories.id})`))
.limit(4),
]);
// Annotate "Shared" chips on the recent panel.
const projectIds = recent
.map((r) => r.projectId)
.filter((p): p is string => p !== null);
const sharedProjects =
projectIds.length > 0
? new Set(
(
await db
.selectDistinct({ projectId: projectShares.projectId })
.from(projectShares)
.where(inArray(projectShares.projectId, projectIds))
).map((r) => r.projectId),
)
: new Set<string>();
const memoryTotal = counts[0]?.total ?? 0;
return (
<Container className="pt-6">
<PageHeader
title={`Welcome, ${session!.user.name ?? session!.user.email ?? "there"}`}
description={`${memoryTotal} memor${memoryTotal === 1 ? "y" : "ies"} across ${topProjects.length} project${topProjects.length === 1 ? "" : "s"}.`}
actions={
<Link href="/memories/new" className="no-underline">
<Button>New memory</Button>
</Link>
}
/>
<div className="grid gap-6 md:grid-cols-3">
<section className="md:col-span-2 space-y-2">
<h2 className="text-sm font-medium text-fg-muted mb-2">Recent</h2>
{recent.length === 0 ? (
<EmptyState
title="No memories yet"
description="Write one from the MCP, or create one here."
action={
<Link href="/memories/new" className="no-underline">
<Button>Create the first one</Button>
</Link>
}
/>
) : (
recent.map((m) => (
<Link
key={m.id}
href={`/memories/${m.id}`}
className="block no-underline"
>
<Card className="hover:border-border-strong transition-colors">
<CardBody className="space-y-2">
<div className="flex items-center gap-2 text-xs text-fg-subtle">
<Badge tone={m.scope === "user" ? "accent" : "neutral"}>
{m.scope}
</Badge>
{m.projectId && sharedProjects.has(m.projectId) ? (
<Badge tone="accent" title="Shared with one or more groups">
Shared
</Badge>
) : null}
{m.projectKey ? <span>· {m.projectKey}</span> : null}
<span className="ml-auto">
{new Date(m.createdAt).toLocaleDateString()}
</span>
</div>
<p className="text-sm text-fg line-clamp-2">{m.content}</p>
{m.tags.length ? (
<div className="flex gap-1 flex-wrap">
{m.tags.slice(0, 6).map((t) => (
<Badge key={t}>{t}</Badge>
))}
</div>
) : null}
</CardBody>
</Card>
</Link>
))
)}
</section>
<section>
<h2 className="text-sm font-medium text-fg-muted mb-2">Projects</h2>
{topProjects.length === 0 ? (
<p className="text-sm text-fg-subtle">No projects yet.</p>
) : (
<Card>
{topProjects.map((p, i) => (
<Link
key={p.id}
href={`/projects/${encodeURIComponent(p.key)}`}
className={`block px-4 py-3 hover:bg-surface-2 no-underline ${i > 0 ? "border-t border-border" : ""}`}
>
<div className="flex items-center gap-2">
<span className="font-mono text-sm text-fg truncate">
{p.key}
</span>
<Badge className="ml-auto">{p.memoryCount}</Badge>
</div>
{p.displayName && p.displayName !== p.key ? (
<span className="block text-xs text-fg-muted truncate">
{p.displayName}
</span>
) : null}
</Link>
))}
<Link
href="/projects"
className="block px-4 py-2 text-xs text-fg-muted border-t border-border hover:bg-surface-2 no-underline"
>
All projects
</Link>
</Card>
)}
</section>
</div>
</Container>
);
}