lean_ctx/core/hive/knowledge/
mod.rs1pub mod code_graph;
11pub mod doc;
12pub mod draft;
13pub mod import_draft;
14pub mod learn;
15pub mod pull;
16pub mod review;
17pub mod screening;
18pub mod states;
19
20use std::path::Path;
21
22use serde::{Deserialize, Serialize};
23
24use super::HiveError;
25use super::state::write_private;
26use lean_ctx_protocol::hive::knowledge::ResearchContextDocumentV1;
27use lean_ctx_protocol::hive::validate_vault_id;
28
29#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
32pub struct BaseRevision {
33 pub revision: u64,
34 #[serde(default, skip_serializing_if = "Option::is_none")]
35 pub etag: Option<String>,
36 pub document: ResearchContextDocumentV1,
37 #[serde(default, skip_serializing_if = "is_zero")]
40 pub quarantined_after: u64,
41}
42
43impl BaseRevision {
44 pub fn head(&self) -> u64 {
46 self.revision + self.quarantined_after
47 }
48}
49
50fn is_zero(value: &u64) -> bool {
51 *value == 0
52}
53
54fn base_dir(data_dir: &Path, vault_id: &str) -> Result<std::path::PathBuf, HiveError> {
55 validate_vault_id(vault_id).map_err(|e| HiveError::Protocol(e.to_string()))?;
56 Ok(data_dir.join("knowledge").join(vault_id))
57}
58
59pub fn load_base(data_dir: &Path, vault_id: &str) -> BaseRevision {
61 base_dir(data_dir, vault_id)
62 .ok()
63 .and_then(|dir| std::fs::read(dir.join("base.json")).ok())
64 .and_then(|bytes| serde_json::from_slice(&bytes).ok())
65 .unwrap_or_default()
66}
67
68pub fn save_base(data_dir: &Path, vault_id: &str, base: &BaseRevision) -> Result<(), HiveError> {
69 let bytes = serde_json::to_vec_pretty(base).map_err(|e| HiveError::Local(e.to_string()))?;
70 write_private(&base_dir(data_dir, vault_id)?, "base.json", &bytes)
71}