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>
This commit is contained in:
2026-08-11 14:58:11 -07:00
co-authored by Claude Opus 5
parent d9306884d3
commit c3bbea5134
20 changed files with 2435 additions and 527 deletions
+60
View File
@@ -595,6 +595,66 @@ The OIDC client you use locally must accept
---
## Running the tests
```bash
pnpm test # all packages
pnpm --filter @shared-memory/web test:watch
```
Unit tests (e.g. `lib/memory-patch.test.ts`) need nothing but `pnpm install`.
The integration tests in `lib/mcp/tools.integration.test.ts` exercise the
real tool handlers against a **real Postgres with pgvector** — they assert on
stored rows, so there is no mock DB to drift from production behaviour. Spin
one up:
```bash
docker run -d --name sm-test-db \
-e POSTGRES_USER=test -e POSTGRES_PASSWORD=test \
-e POSTGRES_DB=shared_memory_test \
-p 55432:5432 pgvector/pgvector:pg16
for f in apps/web/drizzle/*.sql; do
docker exec -i sm-test-db psql -U test -d shared_memory_test -v ON_ERROR_STOP=1 -q < "$f"
done
pnpm test
```
The default `DATABASE_URL` assumes the published port is reachable on
localhost. If your test runner is itself inside a container, point it at the
database container's address instead:
```bash
DATABASE_URL="postgres://test:test@$(docker inspect -f \
'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' sm-test-db):5432/shared_memory_test" \
pnpm test
```
The embedder sidecar is stubbed in tests (it's an external ML service); the
stub is deterministic per-text, so re-embedding is verified by asserting the
stored vector actually changed — not by asserting a mock was called.
Teardown: `docker rm -f sm-test-db`.
### Linting
```bash
pnpm lint
```
Runs the ESLint CLI directly against `eslint.config.mjs`. Note that `next
lint` is deprecated (it goes away in Next 16) and had never been configured
here, so this replaces it. `eslint-config-next` is still published in the
legacy `.eslintrc` format, so the config bridges it through `FlatCompat`;
that bridge can be dropped once the package ships a native flat export.
The tree is currently clean at `--max-warnings=0`, so adding that flag to
the `lint` script is a cheap way to keep it that way.
---
## Troubleshooting
- **`401 claim invalid: aud`** from `/api/mcp` — your MCP client isn't