You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## What type of PR is this?
- [ ] feat (new feature)
- [ ] fix (bug fix)
- [ ] docs (documentation)
- [ ] style (formatting, no code change)
- [x] refactor (code change that neither fixes a bug nor adds a feature)
- [ ] perf (performance improvement)
- [ ] test (adding or updating tests)
- [ ] chore (maintenance, tooling)
- [ ] build / ci (build or CI changes)
## Which issue(s) this PR fixes
Fixes # matrixorigin/memoria-website#98
## What this PR does / why we need it
### Scope
These edits extend Memoria’s storage/API/MCP layers for **group
collaboration**, **branch metadata**, **list filtering**, **MatrixOne
compatibility**, and **operational observability**. They also align
tests with updated function signatures.
---
### 1. `memoria-storage` (`store.rs` + tests)
**Schema / migrations**
- **`CURRENT_USER_SCHEMA_VERSION`**: bumped from `1` → **`2`** so
existing user databases run compat migrations on next startup.
- **`author_id` on `mem_memories`**: migration is **idempotent** — adds
column only if missing (information_schema check); **`AFTER user_id`
removed** for broader MatrixOne compatibility.
- **`idx_author`**: added only if missing (handles “column exists but
index missing”).
- **Branch tables** (`mem_branches.table_name` → physical branch
tables): **same `author_id` + `idx_author` migration** applied per
active branch table. Rationale: upstream group mode added `author_id` to
inserts; **legacy branch tables** created before that column may not
have it; zero-copy branches don’t automatically pick up a later `ALTER`
on `mem_memories`.
**Behavior**
- **`active_table`**: when `mem_branches` has metadata for the active
branch, **trust it** and **skip** `information_schema.tables` existence
checks — MatrixOne zero-copy branch tables may not appear reliably
there; failures surface on actual DML instead.
- **`list_branches`**: return type extended to **`(name, table_name,
created_at)`**; query adds **`ORDER BY created_at ASC`** for stable
ordering.
- **`list_active_lite`**: new parameter **`trust_tier: Option<&str>`**
with SQL filtering when set.
**Tests**
- `branch_ops.rs`: destructuring updated for 3-tuple `list_branches`.
- `store_crud.rs`: `list_active_lite(..., None)` extended with extra
**`None`** for `trust_tier`.
---
### 2. `memoria-service` (`service.rs`)
- **`list_active_filtered`**: accepts **`trust_tier: Option<&str>`** and
filters in-memory when provided (consistent with REST/MCP).
---
### 3. `memoria-api`
**`routes/memory.rs`**
- **`ListQuery`**: optional **`trust_tier`** query param wired through
to **`list_active_filtered`**.
**`routes/snapshots.rs`**
- **`list_branches` JSON**: includes **`created_at`** where available
(tuple destructuring updated for new storage return type).
**`routes/admin.rs`**
- **`user_stats`**: counting memories for **group scopes** (`grp_*`)
adjusted so counts are not incorrectly filtered by personal `user_id`
only.
- **New handler `user_branch_stats`**: **`GET
/admin/users/:user_id/branch-stats`** — returns per-branch memory counts
(uses main count + iterates branches via `list_branches`), with
defensive error handling per branch.
**`lib.rs`**
- Registers the new admin route above.
---
### 4. `memoria-mcp`
**`server.rs`**
- **`ACTOR_USER_ID`** (task-local): captured before **`spawn_blocking`**
and re-entered inside the blocking closure so **active branch context
matches** between async REST/MCP paths and blocking Git/storage work
(fixes Dashboard vs MCP branch mismatch when using the same API key).
**`tools.rs`**
- **`memory_list` / list path**: passes **`trust_tier`** from tool args
into **`list_active_filtered`**.
**`git_tools.rs`**
- All **`list_branches`** call sites updated for **3-tuple** return
(third element ignored where unused).
---
### 5. `memoria-git` (`service.rs` + `Cargo.toml`)
**MatrixOne DDL reliability**
- **`exec_ddl`**: retries up to **3 attempts** with backoff when the DB
returns **20631** / **`txn need retry`** (“def changed” under RC mode).
Typical trigger: concurrent **`data branch merge`** vs **`ALTER TABLE`**
on branch tables during migration (transient; retry is the intended
client-side mitigation).
**Diff / compatibility**
- **`data branch diff`**: **`columns (...)` clause removed** from SQL —
filtered in Rust instead — for **MatrixOne version differences** (local
vs cloud).
- **`FIXME(memoria-team)`** comment documenting **duplicate `memory_id`
rows** in diff results / UI linkage — left for upstream; not “fixed”
here.
**Dependencies**
- **`tokio`** moved from **dev-dependencies only** to **normal
dependency** (needed for **`tokio::time::sleep`** in `exec_ddl` retry
loop).
---
### 6. Integration tests (`memoria-mcp`)
- **`branch_e2e.rs`**, **`integration_full.rs`**: tuple destructuring
updated for **`list_branches`** 3-tuple.
---
### Risks / review focus
1. **Migration + DDL concurrency**: `author_id` ALTERs on branch tables
can overlap with **`data branch merge`** — mitigated by **`exec_ddl`
retry**; reviewers may want to confirm whether long-term
**serialization** or **migration ordering** is preferable.
2. **`CURRENT_USER_SCHEMA_VERSION = 2`**: ensures one migration pass;
coordinate with upstream versioning if they already use `2` differently.
3. **Admin `user_branch_stats`**: new surface area — auth / master-key
expectations should match existing admin routes.
4. **Known open issue**: **`FIXME`** in `memoria-git` for duplicate diff
rows — document for Memoria team.
---
### How to verify locally
- **`cargo check`** / **`cargo test`** with a running MatrixOne and
**`DATABASE_URL`** pointing at the correct port (e.g. **`6001`** if
that’s where MO listens). Note: **`group_collab_api`** defaults to port
**`6666`** if **`DATABASE_URL`** is unset — set env explicitly if tests
fail with DB timeouts.
---
### Files changed (from your status)
| Area | Files |
|------|--------|
| API | `memoria-api/src/lib.rs`, `routes/admin.rs`, `routes/memory.rs`,
`routes/snapshots.rs` |
| Git / DDL | `memoria-git/Cargo.toml`, `memoria-git/src/service.rs` |
| MCP | `memoria-mcp/src/git_tools.rs`, `server.rs`, `tools.rs`, tests
`branch_e2e.rs`, `integration_full.rs` |
| Service | `memoria-service/src/service.rs` |
| Storage | `memoria-storage/src/store.rs`, tests `branch_ops.rs`,
`store_crud.rs` |
---
You can paste this block into a PR description or share it as an
internal review brief. If you want a shorter “changelog” paragraph only,
say so and I’ll trim it.
0 commit comments