Repository navigation
feat(memory): lossless source-position relocation - #1241
Open
rivetphilbot wants to merge 4 commits into
Open
rivetphilbot wants to merge 4 commits into
rivetphilbot wants to merge 4 commits into
Conversation
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
marked this pull request as ready for review
November 9, 2026 14:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Operators need one most-complete canonical source conversation, with every distinct verified message kept. Capture ingest is insert-only, so a wrong
metadata.positionstays 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.--applyrequires--backup(wx + fsync) before any write.Chosen design: typed
RepairPlan+ one SERIALIZABLE transaction + the same session advisory lock as capture ingest + apply-timeSHARE ROW EXCLUSIVEonros_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.
ordinaland derivedevent_idare recomputed (capture-core occurrence hash, oringestEventId); renamingsource_idalone 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 somemory_get_fullstill reads the original line.The apply backup stores each moved row's
id, full pre-applymetadata, andcreated_at. That is enough to restore every field this operation changes (position,ordinal,event_id, backfillsource_id,sub_indexwhen present) while UUID, content, role, chunks, andcreated_atstay intact. v1 has no restore CLI; restore is proven by writing those metadata blobs back.What it refuses (v1)
memory_browseorders bycreated_at, not source ordinal. Dates from the original wrong position are not destination proof. Reports always setdatesRepaired: false. A metadata relocation is not a claim that canonical visible order was repaired.created_atorder (timestamp_order)timestamp_unproven)split_entry)created_at(summary_contiguity). Keeping a message UUID is not proof a contextual summary still covers the right sequence.Existing
captureBatchinsert-only ingest is unchanged. No MCP update tool.Tests
Synthetic fixtures only (no house data). Runtime Postgres required for transaction tests.
Also:
tsc --noEmiton@rivetos/memory-postgresand@rivetos/cli;eslinton the repair dir andpackages/cli/src/commands/memory.ts.Postgres tests cover dry-run, simple relocation with ordinal/
event_idrecompute, 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-nativeevent_idretention,ingestEventIdrecompute, conversation-scopedsource_idcollision, 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,relocateSourcePositionsabort against an uncommitted ordinaryINSERTthen apply + idempotent reapply, and a synthetic adopted schema (CREATE VIEW ros_* AS SELECT * FROM rh_*, INSERT-only trigger onrh_messages, chunk FK on the base table).Checklist
@rivetos/typesonly)feat:,fix:,docs:,test:,chore:)npm testof the monorepo was not run.Remaining limitations / validation gates
created_at. If browse order must change, that is a later gated operation with source-proven destination timestamps.SHARE ROW EXCLUSIVElock onros_messages(on PG 16,LOCK TABLEof a simpleSELECT *view also locks therh_*base table). Acceptable for a rare operator repair; not for ingest.ordinal.test.ts.RIVETOS_PG_URLis set locally. This PR does not add a CI Postgres service.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 OFtriggers, join views, and a live adopted catalog are unproven.UPDATE metadatafrombackup.before. There is no operator restore command.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.--applyagainst real records needs a separate, explicit operator approval.