Files
huskies/server/src/crdt_state/mod.rs
T

70 lines
2.9 KiB
Rust
Raw Normal View History

//! CRDT state layer — manages pipeline state as a conflict-free replicated document backed by SQLite.
//!
//! The CRDT document is the primary source of truth for pipeline item
//! metadata (stage, name, agent, etc.). CRDT ops are persisted to SQLite so
//! state survives restarts. The filesystem `.huskies/work/` directories are
//! still updated as a secondary output for backwards compatibility.
//!
//! Stage transitions detected by `write_item()` are broadcast as [`CrdtEvent`]s
//! so subscribers (auto-assign, WebSocket, notifications) can react without
//! polling the filesystem.
#![allow(unused_imports, dead_code)]
use std::collections::HashMap;
/// A vector clock mapping node IDs (hex-encoded Ed25519 pubkeys) to the count
/// of ops seen from that node. Used for delta sync — a connecting peer sends
/// its clock so the other side can compute which ops are missing.
pub type VectorClock = HashMap<String, u64>;
2026-04-28 12:19:49 +00:00
mod gateway_config;
2026-04-28 09:49:57 +00:00
mod lww_maps;
mod ops;
mod presence;
mod read;
mod state;
mod types;
mod write;
2026-04-28 12:19:49 +00:00
pub use gateway_config::{read_gateway_active_project, write_gateway_active_project};
2026-04-28 09:49:57 +00:00
pub use lww_maps::{
2026-04-28 13:55:40 +00:00
delete_active_agent, delete_agent_throttle, delete_gateway_project, delete_merge_job,
delete_test_job, delete_token_usage, read_active_agent, read_agent_throttle,
read_all_active_agents, read_all_agent_throttles, read_all_gateway_projects,
read_all_merge_jobs, read_all_test_jobs, read_all_token_usage, read_gateway_project,
2026-04-28 09:49:57 +00:00
read_merge_job, read_test_job, read_token_usage, write_active_agent, write_agent_throttle,
2026-04-28 13:55:40 +00:00
write_gateway_project, write_merge_job, write_test_job, write_token_usage,
2026-04-28 09:49:57 +00:00
};
pub use ops::{all_ops_json, apply_remote_op, ops_since, our_vector_clock, subscribe_ops};
pub use presence::{
is_claimed_by_us, our_node_id, read_all_node_presence, release_claim, sign_challenge,
2026-04-27 23:44:36 +00:00
sign_versioned_challenge, write_claim, write_node_metadata, write_node_presence,
};
pub use read::{
CrdtItemDump, CrdtStateDump, check_archived_deps_crdt, check_unmet_deps_crdt,
dep_is_archived_crdt, dep_is_done_crdt, dump_crdt_state, evict_item, read_all_items, read_item,
};
pub use state::init;
pub use types::{
2026-04-28 09:49:57 +00:00
ActiveAgentCrdt, ActiveAgentView, AgentThrottleCrdt, AgentThrottleView, CrdtEvent,
2026-04-28 13:55:40 +00:00
GatewayConfigCrdt, GatewayProjectCrdt, GatewayProjectView, MergeJobCrdt, MergeJobView,
NodePresenceCrdt, NodePresenceView, PipelineDoc, PipelineItemCrdt, PipelineItemView,
TestJobCrdt, TestJobView, TokenUsageCrdt, TokenUsageView, subscribe,
};
pub use write::{
migrate_names_from_slugs, migrate_story_ids_to_numeric, name_from_story_id, write_item,
};
#[cfg(test)]
pub use state::init_for_test;
pub(crate) use state::{ALL_OPS, VECTOR_CLOCK};
/// Hex-encode a byte slice (no external dep needed).
pub(crate) mod hex {
2026-04-29 10:41:32 +00:00
/// Encode `bytes` as a lowercase hexadecimal string.
pub fn encode(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
}