Skip to content

feat(memory): lossless source-position relocation - #1241

Open
rivetphilbot wants to merge 4 commits into
mainfrom
cursor/source-position-relocate-3ad9
Open

rivetphilbot wants to merge 4 commits into
mainfrom
cursor/source-position-relocate-3ad9

Conversation

@rivetphilbot

@rivetphilbot rivetphilbot commented Nov 7, 2026 •

Copy link
Copy Markdown
Collaborator

Why

Operators need one most-complete canonical source conversation, with every distinct verified message kept. Capture ingest is insert-only, so a wrong metadata.position stays on the row and can point at a different message than the current transcript. This PR adds a supported, UUID-keyed repair that relocates source-position metadata in place. It does not invent sessions, merge distinct occurrences, or apply to any live records.

What

Operator library + CLI (rivetos memory relocate-source-positions). Default is dry-run. --apply requires --backup (wx + fsync) before any write.

Chosen design: typed RepairPlan + one SERIALIZABLE transaction + the same session advisory lock as capture ingest + apply-time SHARE ROW EXCLUSIVE on ros_messages. Rejected a unique index / SQL procedure because that would change insert-only ingest.

Row UUID is the only occurrence key. Content hash and tool-call id are never used to pick a row. ordinal and derived event_id are recomputed (capture-core occurrence hash, or ingestEventId); renaming source_id alone is not the repair. Storage has no unique (source_id) / (position, ordinal) constraint, so the engine handles exact-UUID mapping and conversation-scoped collisions.

Truncation pointers (session_jsonl_path, session_jsonl_line) are file offsets, not source position, and are left unchanged so memory_get_full still reads the original line.

The apply backup stores each moved row's id, full pre-apply metadata, and created_at. That is enough to restore every field this operation changes (position, ordinal, event_id, backfill source_id, sub_index when present) while UUID, content, role, chunks, and created_at stay intact. v1 has no restore CLI; restore is proven by writing those metadata blobs back.

What it refuses (v1)

  • Destination timestamps. memory_browse orders by created_at, not source ordinal. Dates from the original wrong position are not destination proof. Reports always set datesRepaired: false. A metadata relocation is not a claim that canonical visible order was repaired.
  • Plans whose destination positions would invert created_at order (timestamp_order)
  • Destination timestamp fields on the plan (timestamp_unproven)
  • Splitting a multi-sub source entry, including onto an occupied sibling (split_entry)
  • Summary source lists whose ordinals would run backward against destination positions or created_at (summary_contiguity). Keeping a message UUID is not proof a contextual summary still covers the right sequence.
  • Mixed already-applied / pending plans, occupied targets outside the plan, stale hashes, cross-conversation moves

Existing captureBatch insert-only ingest is unchanged. No MCP update tool.

Tests

Synthetic fixtures only (no house data). Runtime Postgres required for transaction tests.

cd plugins/memory/postgres
RIVETOS_PG_URL=postgres://rivet_test:rivet_test@127.0.0.1:5432/rivet_relocate_test \
  npx vitest run src/source-position-repair
# 3 files, 27 passed, 1 skipped (skip marker when URL is unset)

cd packages/cli
npx vitest run src/commands/memory.test.ts
# 33 passed

Also: tsc --noEmit on @rivetos/memory-postgres and @rivetos/cli; eslint on the repair dir and packages/cli/src/commands/memory.ts.

Postgres tests cover dry-run, simple relocation with ordinal/event_id recompute, UUID/content/chunk/FTS/access preservation, truncation file-pointer preservation, dependency chain, duplicate/occupied/repeated-content rejection, stale hash, timestamp-order inversion, split-entry, occupied sibling, hook-native event_id retention, ingestEventId recompute, conversation-scoped source_id collision, mixed already-applied/pending, summary contiguity, backup failure abort, leftover backup, restore of pre-apply metadata from a successful-apply backup, advisory lock vs competing session, relocateSourcePositions abort against an uncommitted ordinary INSERT then apply + idempotent reapply, and a synthetic adopted schema (CREATE VIEW ros_* AS SELECT * FROM rh_*, INSERT-only trigger on rh_messages, chunk FK on the base table).

Checklist

  • Tests added or updated
  • Documentation updated (if applicable)
  • Clean architecture respected (plugins depend on @rivetos/types only)
  • No API keys, tokens, or credentials in the diff
  • Conventional commit message (feat:, fix:, docs:, test:, chore:)
  • Targeted tests pass (repair PG + CLI flags). Full npm test of the monorepo was not run.

Remaining limitations / validation gates

  • v1 does not write destination created_at. If browse order must change, that is a later gated operation with source-proven destination timestamps.
  • Apply takes a table-level SHARE ROW EXCLUSIVE lock on ros_messages (on PG 16, LOCK TABLE of a simple SELECT * view also locks the rh_* base table). Acceptable for a rare operator repair; not for ingest.
  • Capture-core is ESM and this package is CJS, so the occurrence hash is a local clone pinned equal in ordinal.test.ts.
  • No unique source-position index added (would change ingest).
  • GitHub CI does not start Postgres; locking / view / restore tests skip unless RIVETOS_PG_URL is set locally. This PR does not add a CI Postgres service.
  • Adopted-schema proof is synthetic CREATE VIEW ros_* AS SELECT * FROM rh_* with an INSERT-only trigger on the base table. RivetHub adopt DDL is not in this repo. INSTEAD OF triggers, join views, and a live adopted catalog are unproven.
  • Backup restore is proven in-test by UPDATE metadata from backup.before. There is no operator restore command.
  • Hook-native event_ids that are not capture/ingest hashes are kept. A later ingest that mints a new native id at the new position can still insert a duplicate; v1 does not rewrite those ids.
  • Merging ships the library and CLI only. Default is dry-run; --apply against real records needs a separate, explicit operator approval.
Open in Web Open in Cursor 

rivetphilbot and others added 4 commits November 7, 2026 16:22
Capture ingest is insert-only, so a wrong metadata.position stays on the
row. Operators need a UUID-keyed, hash-checked repair that recomputes
ordinal and derived event_id without changing ingest, inventing sessions,
or claiming created_at browse order was repaired.
session_jsonl_line is a disk offset, often equal to the old position at
capture time. Relocating source position must not treat it as destination
proof or rewrite it, or memory_get_full would read the wrong jsonl line.
Relocate must work through ros_* views over rh_* tables, leave enough
before-state to restore metadata after a successful apply, and abort
all-or-none when an ordinary INSERT already holds the table.
@rivetphilbot
rivetphilbot marked this pull request as ready for review November 9, 2026 14:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants