Skip to content

feat(backend): add global KV storage fallback for server-side data#243

Draft
Tjemmmic wants to merge 4 commits into
masterfrom
donovan/workspace-fixes
Draft

feat(backend): add global KV storage fallback for server-side data#243
Tjemmmic wants to merge 4 commits into
masterfrom
donovan/workspace-fixes

Conversation

@Tjemmmic
Copy link
Copy Markdown
Contributor

Summary

Adds a server-global key-value storage fallback to MemoryBackend and FileIOBackend for byte-map data that isn't associated with any registered client account.

Previously, byte-map operations (store_byte_map_value, get_byte_map_value, etc.) would silently no-op when session_cid didn't resolve to an existing CNAC — returning Ok(None) / Ok(Default::default()). This made it impossible for servers to persist application-level data (e.g., workspace metadata) using the standard backend KV API, since servers operate without their own CNAC and use CID 0 for global state.

Changes

  • memory.rs: Added global_kv: RwLock<HashMap<u64, HashMap<String, HashMap<String, Vec<u8>>>>> field to MemoryBackend. The four byte-map methods (get, remove, store, get_by_key, remove_by_key) now fall back to global_kv when no CNAC matches the session_cid, instead of returning empty results.
  • file_io_backend.rs:
    • On connect, loads the global KV store from <server_dir>/global_kv.bin if present.
    • Added save_global_kv() to persist the map via bincode.
    • Mutating byte-map operations now route persistence to save_global_kv() when save_cnac_by_cid returns ClientNonExists, instead of failing the call.
    • get_byte_map_values_by_key no longer attempts to save a CNAC on a read path (drive-by fix).

Introduces a server-global KV store in MemoryBackend and FileIOBackend to handle application-level key-value data not tied to a specific ClientNetworkAccount (CNAC). This is primarily used for workspace metadata where the session CID is 0. Updates the file I/O layer to persist and restore global_kv.bin from the server directory.
@Tjemmmic

This comment was marked as outdated.

Refactors the global_kv fallback mechanism in both FileIOBackend and MemoryBackend to properly namespace entries by session_cid. Previously, values stored without an active CNAC were only keyed by peer_cid, leading to cross-session state leakage. Includes integration tests to verify data isolation and persistence across reload cycles.
@Tjemmmic

This comment was marked as outdated.

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 20, 2026

Codecov Report

❌ Patch coverage is 94.80519% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.6516%. Comparing base (2e9e3fd) to head (21a9271).

Files with missing lines Patch % Lines
citadel_user/src/backend/memory.rs 92.7272% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@               Coverage Diff                @@
##             master       #243        +/-   ##
================================================
+ Coverage   83.3963%   83.6516%   +0.2553%     
================================================
  Files            91         91                
  Lines         14015      14136       +121     
================================================
+ Hits          11688      11825       +137     
+ Misses         2327       2311        -16     
Files with missing lines Coverage Δ
citadel_user/src/backend/file_io_backend.rs 96.1783% <100.0000%> (+0.6227%) ⬆️
citadel_user/src/backend/memory.rs 87.7907% <92.7272%> (+10.6822%) ⬆️

... and 8 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Tjemmmic and others added 2 commits April 20, 2026 01:13
Treats a failed bincode deserialization of the global_kv.bin file as a hard startup error. Previously this was logged but allowed startup to proceed with an empty map, meaning the next mutation would silently overwrite the corrupted file, permanently destroying all server-global data. Also expands test coverage for global KV session isolation.
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