project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching0 forksRust3.6 MiBc49f5e7Tahinli1 month ago2 commits
loro-internal Guidelines
This crate contains Loro's unstable internal CRDT implementation. Public API
compatibility concerns still matter because crates/loro and crates/loro-wasm
wrap this crate directly, but the internal priority is preserving invariants
over graceful degradation.
Internal Map
src/undo.rs: one recorded item per undo/redo call, including no-ops: ../../context/undo-history-boundaries.md.- Undo transaction rollback and writer gate: ../../context/undo-atomicity.md.
src/loro.rs: document-level orchestration for commit, import/export, checkout, barriers, state/oplog coordination, and event emission.apply_diff/revert_toroll a failed batch back: ../../context/apply-diff-atomicity.md.src/encoding.rs: public/internalExportMode, binary header parsing, checksum verification,EncodeModedispatch, import metadata, and the bridge from decoded changes intoOpLog.src/encoding/: concrete binary and JSON encoding implementations. Readsrc/encoding/AGENTS.mdand ../../context/internal-encoding.md before changing binary layout, JSON schema, import metadata, shallow snapshot, or op/value encoding.src/oplog/andsrc/dag/: change storage, dependency ordering, pending changes, version vectors/frontiers, shallow roots, and history traversal.ChangeStoreis also read without the op log lock, so keep its lock order: ../../context/arena-parent-links.md.src/arena.rs: container IDs, indices, and parent links. How a parent is found in a lazily loaded document, and the locking rules: ../../context/arena-parent-links.md. Parsed change blocks keep text and list values out of doc-wide arenas; the trackers usediff_calc/tracker_texts.rsandtracker_values.rs: ../../context/text-arena-growth.md.src/utils/subscription.rs,src/subscription.rs,src/txn.rscommit: user callbacks that panic must not poison locks or drop events: ../../context/callback-panics.md.src/state.rsandsrc/state/: materialized document state, container stores, diff application, checkout/replay, deep value, dead-container tracking, and mergeable container visibility. Readsrc/state/AGENTS.mdand ../../context/mergeable-containers.md before changing mergeable containers.src/handler.rs: typed container handlers, local operation creation, andMapHandler::ensure_mergeable_*.src/handler/movable_list_apply_delta.rsapplies movable-list diffs forapply_diff;src/undo/movable_list.rspreserves undo identities/anchors: ../../context/movable-list-apply-diff.md.src/diff_calc/: diff calculation when moving between versions. The tree calculator's cache transitions: ../../context/tree-checkout-window.md. Which tree diff modes carry raw ops, and how tree events are built for each: ../../context/tree-events.md.src/container/richtext/: text state with style anchors. Where local inserts go next to anchors, the insert cursor cache, and deletestart_ids: ../../context/richtext-insert-positions.md.docs/diff_calc.md: design notes for diff calculation.docs/critical-version-spec.md: specification and proof skeleton for replay-base selection (Eg-walker-aligned terminology; defines critical version, the entry check, and the fallback sweep).docs/mergeable-container-id.md: current mergeable container id encoding.tests/mergeable_container/andtests/mergeable_cid_encoding.rs: focused mergeable container regression tests.src/tests/import_atomicity.rs: import rollback and malformed-input regressions. A failed import keeps its arena indices and drops only parent links, values, and the dead-container cache: ../../context/failed-import-arena-indices.md.- Movable-list
Move/Setelement validation on import, plus change-store rollback records and KV block-range decoding: ../../context/movable-list-op-validation.md. - Root container visibility after import/checkout (
materialize_touched_roots): ../../context/root-container-visibility.md. - Imported changes that reuse local op ids are checked against local history
before anything is applied (
src/oplog/known_history.rs): ../../context/import-peer-id-reuse.md. import_batchforce-detach, its batch-wide rollback scope, and the never-exit-detached invariant: ../../context/import-batch-atomicity.md.
Commands
Use narrow checks first:
cargo check -p loro-internalcargo test -p loro-internal --doccargo test -p loro-internal --test mergeable_containercargo test -p loro-internal --test mergeable_cid_encodingcargo test -p loro-internal import_atomicity
For broad shared behavior, run the root commands from AGENTS.md. For changes
to import, checkout, encoding, state replay, or diff calculation, consider fuzz
coverage under crates/fuzz and ask before running long fuzz targets.
Working Rules
- Replay-base selection uses Eg-walker terminology (arXiv:2409.14252 §3.5):
a version V is critical when every event outside
Events(V)happened after all ofEvents(V)— no concurrency crosses the cut. Non-Checkoutdiff modes are only sound when the base satisfies this (the tree calculator'sCheckoutpath takes its window from the two versions instead);dag.rs/oplog.rsenforce it via theImportGreaterUpdatesentry check and, on conservative retreat, the multi-head fixpointOpLog::latest_critical_version_below_meet, with thelatest_single_head_critical_versiondescent as fallback. Do not use "LCA" or "common ancestor" in new code or docs: the meet of two versions is generally NOT a safe replay base, and a caller that needs a critical version must ask for one (loro-dev/loro#1095). Readdocs/critical-version-spec.mdbefore touchingfind_replay_baseor diff modes.OpLog::iter_from_replay_base_causallymay bypass the DAG when the concurrency is register-only (docs/diff_calc.md, "Register-only concurrency"); keep that check container-granular and history-based. - Internal invariant violation should fail fast. Invalid external bytes or JSON
should return
Err. - Do not silently skip ops, containers, state entries, diffs, or pending changes.
- Snapshot/import paths must be atomic: if decode or state application fails, rollback must leave the document usable.
- Preserve attached/detached document state when export paths temporarily checkout another version.
- If a change affects
crates/loroorcrates/loro-wasmbehavior, add or update tests at the wrapper layer as well as the internal layer when practical.