1. lib/snippets.ts + lib/memory-actions.ts (×2): shared-project key
lookups were querying `projects` by key with no visibility scope. Since
`projects.key` is unique per user (not global), the unscoped match
could resolve another user's project entirely. Restricted the lookups
to `readableProjectIds(userId, groupNames)` — own + shared only.
2. lib/access.ts getAccessibleProjects: a stray `if (existing) continue`
inside the share-collapse loop short-circuited on the first match,
killing the rw-beats-ro upgrade path. Two-group cases where one share
was ro and another rw on the same project were incorrectly resolved
as ro. Replaced with explicit owner/rw skip.
3. lib/mcp/tools.ts snippetPut: removed a dead `void exists` block that
looked like an authorization pre-flight but was actually a no-op —
real write authorization lives inside putSnippet, called next. Added
a comment at the call site documenting where the check is.
4. memory.delete + snippet.delete: previously had no optimistic-lock
CAS, so a concurrent peer edit could be silently overwritten by a
delete on a stale view. Added optional `version` to MemoryDeleteInput
(new) and SnippetDeleteInput (extended); UPDATE WHERE now CASes on
version; 0-row response surfaces CONCURRENT_EDIT_ERROR. Web detail
pages pass `version` through hidden form inputs. When the caller
doesn't supply a version, falls back to the version we just read in
the same handler for in-handler consistency.
5. lib/mcp/tools.ts memorySearch re-fetch: missing `isNull(deletedAt)`
on the post-search row hydration left a TOCTOU window where a row
soft-deleted between the search and the re-fetch would be returned.
Visibility is still enforced by searchMemories itself.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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>