Skip to content

refactor(mesh): move tree path hashing out of mesh into kv-index - #1535

Merged
CatherineSue merged 1 commit into
mainfrom
refactor/move-tree-path-hash-to-kv-index
May 25, 2026
Merged

CatherineSue merged 1 commit into
mainfrom
refactor/move-tree-path-hash-to-kv-index

Conversation

@CatherineSue

@CatherineSue CatherineSue commented May 25, 2026 •

Copy link
Copy Markdown
Member

Description

Problem

crates/mesh/src/hash.rs exports three tree-node identification helpers — hash_node_path, hash_token_path, and GLOBAL_EVICTION_HASH — that hash a string prefix or token sequence to identify a radix-tree node in the TreeSync wire format. They lived in the mesh crate as leftovers from the v1 cleanup.

The mesh v2 spec (.claude/docs/mesh/mesh-v2-generic-kv.md) says explicitly:

The mesh crate should have ZERO knowledge of trees, workers, policies, or routing.

Tree-prefix hash helpers are by definition tree-specific. The mesh-side TreeSync adapter doesn't even use them — it forwards already-computed node_hash values on TreeDelta without ever computing one. The only caller that actually computes hashes is CacheAwarePolicy in the gateway, which already depends on kv-index for the radix tree types these hashes identify.

Solution

Move the three symbols to kv-index as a new path_hash module, re-exported at the crate root so existing kv_index::hash_node_path(...) / kv_index::hash_token_path(...) call shapes match the prior smg_mesh::hash_node_path(...) shape. No behavior change.

Changes

  • crates/kv_index/Cargo.toml — add blake3 workspace dependency.
  • crates/kv_index/src/path_hash.rs — new module (moved verbatim from crates/mesh/src/hash.rs).
  • crates/kv_index/src/lib.rs — register the module and re-export the three symbols at the crate root.
  • crates/mesh/src/hash.rs — deleted.
  • crates/mesh/src/lib.rs — drop the hash module declaration and the three re-exports.
  • model_gateway/src/policies/cache_aware.rs — switch 12 call sites from smg_mesh::hash_* to kv_index::hash_*.
  • model_gateway/src/mesh/adapters/tree_sync.rs — update one stale doc comment (smg_mesh::tree_ops::GLOBAL_EVICTION_HASH → kv_index::GLOBAL_EVICTION_HASH).

Mesh crate keeps blake3 as a workspace dependency for the unrelated replica_id derivation in kv.rs:536.

Test Plan

  • cargo test -p kv-index --lib path_hash — all 4 hash tests pass in their new home.
  • cargo test -p smg-mesh --lib — 146 pass (4 fewer than pre-move because the tests moved with the file).
  • cargo build -p smg-mesh -p smg — clean.
  • cargo clippy --workspace --all-targets --all-features -- -D warnings — clean.
Checklist
  • cargo +nightly fmt passes
  • cargo clippy --all-targets --all-features -- -D warnings passes
  • Documentation updated (tree_sync.rs doc reference)
  • (Optional) Please join us on Slack #sig-smg to discuss, review, and merge PRs

Summary by CodeRabbit

  • Refactor
    • Hash computation utilities have been reorganized across internal modules to improve code structure and maintainability.

Review Change Stack

`hash_node_path`, `hash_token_path`, and `GLOBAL_EVICTION_HASH` are
tree-node identification helpers — they hash a string prefix or token
sequence to identify a radix-tree node in the TreeSync wire format.
They lived in the mesh crate as leftovers from the v1 cleanup (the
v2 spec says "the mesh crate should have ZERO knowledge of trees,
workers, policies, or routing").

Move them to `kv-index`, the crate that owns the radix-tree
implementations they identify. Only consumer that actually computes
hashes is `CacheAwarePolicy` in the gateway; it already imports
`kv-index` for `Tree`, `TokenTree`, and friends, so the move adds no
new dependency edges. The mesh-side TreeSync adapter never computed
hashes — it just forwards the already-computed `node_hash` field on
`TreeDelta` — so the mesh crate's public surface shrinks by three
symbols with no functional change.

Mesh crate keeps `blake3` as a workspace dependency for the unrelated
replica_id derivation in `kv.rs`.

Signed-off-by: Chang Su <8605658+CatherineSue@users.noreply.github.com>
@github-actions github-actions Bot added dependencies Dependency updates model-gateway Model gateway crate changes kv-index KV index crate changes mesh Mesh crate changes labels May 25, 2026
@coderabbitai

coderabbitai Bot commented May 25, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 92d3dc22-83cd-42af-bb0b-98505bffa292

📥 Commits

Reviewing files that changed from the base of the PR and between fd89557 and 907aead.

📒 Files selected for processing (6)
  • crates/kv_index/Cargo.toml
  • crates/kv_index/src/lib.rs
  • crates/kv_index/src/path_hash.rs
  • crates/mesh/src/lib.rs
  • model_gateway/src/mesh/adapters/tree_sync.rs
  • model_gateway/src/policies/cache_aware.rs
💤 Files with no reviewable changes (1)
  • crates/mesh/src/lib.rs

📝 Walkthrough

Walkthrough

This PR migrates three hashing utilities (hash_node_path, hash_token_path, GLOBAL_EVICTION_HASH) from the smg_mesh crate to kv_index. The blake3 dependency is added to kv_index, and all call sites in model_gateway are updated to use the new module path. Documentation and tests are synchronized accordingly.

Changes

Hash Function Relocation

Layer / File(s) Summary
kv_index hash module setup
crates/kv_index/Cargo.toml, crates/kv_index/src/lib.rs
Blake3 is added as a workspace dependency. The path_hash submodule is declared and its hashing utilities are publicly re-exported from kv_index.
smg_mesh hash module removal
crates/mesh/src/lib.rs
The internal hash module and its public re-exports are removed from smg_mesh as functions are now sourced from kv_index.
model_gateway routing and repair logic migration
model_gateway/src/policies/cache_aware.rs
All hash function calls are switched from smg_mesh::hash_* to kv_index::hash_* across imbalanced worker selection, cold-start repair, and balanced tree routing paths.
model_gateway documentation and tests
model_gateway/src/mesh/adapters/tree_sync.rs, model_gateway/src/policies/cache_aware.rs
Documentation comment and all unit tests are updated to reference kv_index hash functions instead of smg_mesh equivalents.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

  • lightseekorg/smg#1476: Introduces and publicly re-exports GLOBAL_EVICTION_HASH, hash_node_path, and hash_token_path from crates/mesh/src/hash.rs, which are the exact utilities being relocated by this PR.
  • lightseekorg/smg#1364: Modifies cache membership logic in cache_aware.rs and tree_sync.rs using the same hash functions; this PR updates those call sites to use the new module location.

Suggested labels

dependencies, kv-index, model-gateway, mesh, tests

Suggested reviewers

  • tonyluj
  • llfl
  • slin1237

Poem

🐰 Hash functions hop to a new home today,
From mesh to kv_index they make their way,
Blake3 dependencies and tests align,
Migration complete—the logic will shine! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'refactor(mesh): move tree path hashing out of mesh into kv-index' directly summarizes the main change—relocating tree path hashing functions from the mesh crate to kv-index.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch refactor/move-tree-path-hash-to-kv-index

Comment @coderabbitai help to get the list of available commands and usage tips.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean refactoring — moves path hashing from mesh into kv_index where it logically belongs. File rename is 100% identical, all call sites updated, no stale references remain. blake3 dependency correctly added to kv_index/Cargo.toml. Doc comment in tree_sync.rs updated to reference the new location. LGTM.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request relocates hashing utilities, specifically hash_node_path, hash_token_path, and the GLOBAL_EVICTION_HASH constant, from the mesh crate to the kv_index crate. The changes include adding the blake3 dependency to kv_index, creating the path_hash module, and updating all call sites within the model_gateway and mesh crates to point to the new location. I have no feedback to provide.

@CatherineSue
CatherineSue merged commit a38b5a2 into main May 25, 2026
50 checks passed
@CatherineSue
CatherineSue deleted the refactor/move-tree-path-hash-to-kv-index branch May 25, 2026 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Dependency updates kv-index KV index crate changes mesh Mesh crate changes model-gateway Model gateway crate changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant