feat(sharing): project shares, co-edit safety, awareness UI (Phase 4c+d+e)
Adds project-level sharing via the new project_shares table plus the
infrastructure that makes multi-user editing safe and visible.
Authorization (lib/access.ts):
- getAccessibleProjects / getProjectAccess centralise the predicate
used by every read and write path.
- readableProjectIds / writableProjectIds drive listing-style queries.
- Web UI Server Actions and pages source group memberships from the
user_groups table so authorization works without depending on
Agent A's session callback shape.
Optimistic locking:
- memories + snippets gain version + last_edited_by columns. Every
UPDATE bumps version and stamps the editor; UPDATE WHERE clauses
require the caller's pre-fetched version, surfacing a clear
"refresh and try again" error on lost-write races rather than
silently clobbering.
- MemoryUpdateInput / SnippetPutInput accept an optional version
token.
MCP tools:
- memory.write / .update / .delete / .get / .list / .search,
snippet.put / .get / .list / .delete now respect shared-project
access (read = owner | any share, write = owner | rw share).
- project, defaults to ctx.defaultProjectKey from the X-Project-Key
header (populated by the MCP route — Agent A's wiring).
- project.identify returns shared projects you have access to and
prefers an owned project on key collision, audit-logging the
collision so an operator can debug it.
- Tool descriptions for memory.update, memory.write, snippet.put,
and project.identify updated with the co-edit / shared-project
notes.
Web UI:
- Project detail page: ownership badge, shared-with-N-groups badge,
owner-only "Manage sharing" section (add/flip/remove shares via
lib/share-actions.ts). Add-share is constrained to groups the
granter is already in.
- "Shared" chips on memory cards in /memories and /dashboard.
- "Last edited by ..." on memory + snippet detail pages, shown only
when the last editor isn't the row's original author so the chip
stays informative.
- Read-only viewers (ro shares) lose Edit/Delete affordances on
memories and snippets.
Migration 0004_project_shares.sql adds project_shares + the two new
columns on memories and snippets; it depends on Agent A's
0003_groups.sql for the groups, user_groups, and memory_access enum.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,6 +9,12 @@ import type { AuthenticatedClaims } from "@/lib/auth/jwt";
|
||||
* Resolves (or creates) the internal `users` row from the Authentik OIDC
|
||||
* claims so tools work with stable UUID foreign keys rather than raw `sub`
|
||||
* strings.
|
||||
*
|
||||
* `groups` and `defaultProjectKey` are populated by the MCP route handler
|
||||
* (Agent A's work): groups come from the JWT's `groups` claim cross-
|
||||
* checked against the `user_groups` table; defaultProjectKey is the value
|
||||
* of the `X-Project-Key` header attached to the inbound request, used as
|
||||
* a fallback when a tool call doesn't include `project` explicitly.
|
||||
*/
|
||||
export interface UserContext {
|
||||
/** Internal users.id UUID. */
|
||||
@@ -20,6 +26,19 @@ export interface UserContext {
|
||||
/** Optional profile fields if present in the access token. */
|
||||
email: string | null;
|
||||
name: string | null;
|
||||
/**
|
||||
* Group names this user belongs to (from the OIDC `groups` claim,
|
||||
* cross-checked against `user_groups`). Drives sharing authorization;
|
||||
* empty means the user is only a member of their own private projects.
|
||||
*/
|
||||
groups: string[];
|
||||
/**
|
||||
* Default project key from the inbound MCP request's `X-Project-Key`
|
||||
* header. Tools that accept a `project` argument fall back to this
|
||||
* when none is provided so a Claude Code session pre-pinned to a repo
|
||||
* doesn't have to repeat it on every call.
|
||||
*/
|
||||
defaultProjectKey?: string;
|
||||
}
|
||||
|
||||
export async function userContextFromClaims(claims: AuthenticatedClaims): Promise<UserContext> {
|
||||
@@ -56,8 +75,17 @@ export async function userContextFromClaims(claims: AuthenticatedClaims): Promis
|
||||
.where(and(eq(users.oidcIss, claims.iss), eq(users.oidcSub, claims.sub)))
|
||||
.limit(1);
|
||||
if (!existing[0]) throw new Error("user upsert failed and not found on re-read");
|
||||
return { userId: existing[0].id, sub: claims.sub, iss: claims.iss, email, name };
|
||||
return {
|
||||
userId: existing[0].id,
|
||||
sub: claims.sub,
|
||||
iss: claims.iss,
|
||||
email,
|
||||
name,
|
||||
groups: [],
|
||||
};
|
||||
}
|
||||
|
||||
return { userId, sub: claims.sub, iss: claims.iss, email, name };
|
||||
// `groups` is left empty here; Agent A's MCP route handler widens it
|
||||
// with the JWT's `groups` claim after this base context resolves.
|
||||
return { userId, sub: claims.sub, iss: claims.iss, email, name, groups: [] };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user