Skip to content

refactor(memory): dedupe atomic-file-replace helpers across two state stores - #2402

Open
cyberlife-coder wants to merge 1 commit into
developfrom
refactor/mutation-online-migration-atomic-file-dedup
Open

cyberlife-coder wants to merge 1 commit into
developfrom
refactor/mutation-online-migration-atomic-file-dedup

Conversation

@cyberlife-coder

@cyberlife-coder cyberlife-coder commented Sep 25, 2026 •

Copy link
Copy Markdown
Owner

⚠️ Target branch (Git Flow)

refactor/* → targets develop. ✅

Description

Routine code-quality pass, found while looking for duplication candidates. mutation::controller::state and online_migration::job_state each carried an identical copy of the same five functions: a symlink-rejecting directory check, a symlink-rejecting regular-file check, an existence probe, and the platform-specific promote/sync_directory pair backing their staged-write-then-rename persistence.

This isn't cosmetic: it's the TOCTOU-safe symlink rejection (symlink_metadata + explicit is_symlink() reject) and the crash-safe atomic promote that the staged-write-then-promote scheme depends on in both stores. Two independent copies mean a future correctness fix to the symlink check, or a platform fix to promote/sync_directory, can land in one copy and silently miss the other.

Moved the five functions into a new shared crate::mutation::atomic_file module and updated both call sites to use it. The two message-bearing checks (validate_workspace, validate_regular_file) now take an entity: &str argument so every existing error string is reproduced byte-for-byte. The one exception: the "replacement unsupported" message on the #[cfg(not(any(unix, windows)))] branch already read three different ways across this crate (this pair plus mutation::journal::platform's own copy) for a branch that's unreachable on any platform this project ships for or runs CI on — unified to one wording rather than picking one of the three to keep arbitrarily.

Fixes # (none — proactive quality pass, no tracked issue)

Type of Change

  • 🔧 Refactoring (no functional changes)

How Has This Been Tested?

  • Unit tests
  • cargo check -p velesdb-memory (default features, and --features persistence)
  • cargo clippy -p velesdb-memory --all-targets -- -D warnings -D clippy::pedantic (clean)
  • cargo test -p velesdb-memory --lib --features persistence -- --test-threads=1 (817 passed, 0 failed, 9 ignored)
  • cargo fmt --all -- --check (clean)
  • python3 scripts/check_prod_unwraps.py / check-todo-annotations.py (clean; unaffected by this diff, re-verified anyway)

Test Configuration:

  • OS: Linux
  • Rust version: per rust-toolchain.toml (MSRV 1.90)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas — not applicable, no new logic beyond the module doc comment
  • I have made corresponding changes to the documentation — not applicable, no public API or doc changes
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works — not applicable, behavior-preserving refactor already covered by existing tests
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published — not applicable

Unsafe Code Checklist

Not applicable — no unsafe code touched.

High-Risk Change Checklist

Not applicable — doesn't touch hnsw, storage, Drop, unsafe, or SIMD dispatch paths.

Additional Notes

The same five-function shape is duplicated a few more times in this crate (e.g. mutation/journal.rs's own validate_workspace/validate_regular_file, plus similar patterns in online_migration/cleanup.rs and mcp/extraction_job_store.rs). Left untouched here to keep this PR mechanical, single-purpose and reviewable; a follow-up could migrate those call sites onto crate::mutation::atomic_file too.

@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics -17 complexity

Metric Results
Complexity -17

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

… stores

mutation::controller::state and online_migration::job_state each kept an
identical copy of the same five functions: a symlink-rejecting directory
check, a symlink-rejecting regular-file check, an existence probe, and the
platform-specific promote/sync-directory pair backing their staged-write-
then-rename persistence. The logic is safety-sensitive (TOCTOU-safe symlink
rejection, crash-safe atomic promote) and is now split across two files that
a future fix could touch in one and silently miss in the other.

Move the five functions into a new shared crate::mutation::atomic_file
module and have both call sites use it. The two message-bearing checks take
an `entity` argument so every existing error string is reproduced exactly;
the one string that already varied three ways across this crate for an
untestable, unreachable-on-CI platform branch is unified to one wording.

No behavior change on any platform this project ships for.
@cyberlife-coder
cyberlife-coder force-pushed the refactor/mutation-online-migration-atomic-file-dedup branch from e9a24c6 to 2d24315 Compare September 25, 2026 03:38

This branch has not been deployed

No deployments
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.

1 participant