#[cfg(test)]
use crate::api::{
KnowledgeEntityDeleteBatchOutput, KnowledgeEntityDeleteBatchRequest,
KnowledgeMemoryEvolvesCreateBatchOutput, KnowledgeMemoryEvolvesCreateBatchRequest,
KnowledgeMemoryLifecycleBatchOutput, KnowledgeMemoryLifecycleBatchRequest,
};
use crate::search::{
AdaptiveVectorSearchOptions, CompressedVectorSearchMode, SearchFallbackReasonCode,
SearchLexicalProductionQualificationReport, SearchOutOfCoreConfig,
SearchOutOfCoreHydrationOutput, SearchOutOfCoreMetrics, SearchOutOfCoreReader,
SearchQueryOptions, SearchRangeReadConfig, VectorRecallProductionQualificationReport,
VectorRecallValidationOptions, VectorRecallValidationReport, VectorSearchExecutionOptions,
NOWLEDGE_SEARCH_PROJECTION_SCAN_FILTER_FIELDS,
};
use crate::search_projection_evidence::{
nowledge_search_projection_evidence_json, nowledge_search_projection_shadow_evidence_json,
NowledgeSearchProjectionEvidenceReport,
};
use crate::{
cypher, BackgroundMaintenanceKind, BackgroundMaintenanceOptions, BackgroundMaintenanceSummary,
BackgroundWorkHint, BackgroundWorkPlan, BoundedReadQueryOutput, Database, DatabaseConfig,
DatabaseReadTransaction, GraphRagGeneratedQuery, GraphRagSchemaContext,
GraphRagSchemaContextOptions, HawDBError, HawDBLightningBootstrapManifest,
HawDBLightningInitialImportApplyReport, HawDBLightningInitialImportCheckpoint,
HawDBLightningInitialImportCutoverCatchUpReport, HawDBLightningInitialImportDocumentIdentity,
HawDBLightningInitialImportRecoveryReadinessReport, KnowledgeRetrievalOutput,
KnowledgeRetrievalRequest, LocalQosPolicy, LocalQosState, NowledgeGraphStatement,
PlanCacheLookup, QueryOutput, QueryStreamOptions, QueryStreamReport, ReadBudgetResource,
ReadExecutionProfile, Result, ScheduledSearchProjectionCatchUpReport, SearchDocument,
SearchIndex, SearchProjectionCatchUpReport, SearchProjectionChangeBatch,
SearchProjectionChangefeedReadiness, SearchProjectionDelta, SearchProjectionDeltaReport,
SearchProjectionFreshness, SearchProjectionGraphDeltaRequest, SearchProjectionProbeOptions,
SearchProjectionRelationalDelta, SearchResultSet, StorageResourceProfileLimits,
StorageResourceProfileReport, TelemetrySink, Value, STORAGE_RESOURCE_PROFILE_PROTOCOL,
};
use crate::{
graph_route_readiness::NMEM_GRAPH_ROUTE_READINESS_PROTOCOL,
nowledge_inventory::{
background_maintenance_evidence_health, background_maintenance_summary_to_json,
replacement_readiness_family_evidence_health, REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES,
},
route_ownership::NowledgeMemRouteOwnershipReadinessReport,
search_route_ownership::{
NowledgeMemActiveSearchRouteOwnershipReadinessReport,
NowledgeMemActiveSearchRouteReadinessReport,
NowledgeMemSearchRouteOwnershipReadinessReport,
NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTE_READINESS_PROTOCOL,
NOWLEDGE_MEM_SEARCH_ROUTE_OWNERSHIP_PROTOCOL, REQUIRED_NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTES,
REQUIRED_NOWLEDGE_MEM_SEARCH_ROUTES,
},
workload_fixtures::{
NowledgeGraphRouteWorkloadFixtureReport, NOWLEDGE_GRAPH_ROUTE_WORKLOAD_FIXTURE_PROTOCOL,
},
};
use hawdb_core::RuntimeTaskContext;
mod incremental_search;
use hawdb_optimizer::AdaptiveVectorBackendPolicy;
use hawdb_qos::{
IoConcurrencyBudget, RuntimeGovernor, RuntimeGovernorConfig, RuntimeGovernorSnapshot,
RuntimePermit, RuntimeWorkKind, RuntimeWorkPriority, RuntimeWorkRequest, StorageDeviceProfile,
WorkPriority,
};
pub use hawdb_readiness::bounded_read_evidence::{
nowledge_mem_bounded_read_evidence_json,
nowledge_mem_bounded_read_evidence_json_with_route_readiness,
nowledge_mem_bounded_read_evidence_json_with_routes, NowledgeMemGraphMode,
NowledgeMemReadReport, NowledgeMemRouteReadinessSummary,
NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL, NOWLEDGE_MEM_READ_REPORT_PROTOCOL,
};
use hawdb_readiness::nowledge_mem_query_report::nowledge_mem_plan_cache_report;
pub use hawdb_readiness::nowledge_mem_query_report::{
nowledge_mem_fast_path_classification, NowledgeMemFastPathClassification,
NowledgeMemQueryApiBehavior, NowledgeMemQueryExecutionPath, NowledgeMemQueryOutput,
NowledgeMemQueryOutputRowShape, NowledgeMemQueryReport, NowledgeMemQueryReportOptions,
NOWLEDGE_MEM_QUERY_REPORT_PROTOCOL,
};
pub use hawdb_readiness::query_runtime_preflight::NowledgeQueryRuntimePreflightProbe;
use hawdb_readiness::query_runtime_preflight_report::{
hawdb_error_class, nowledge_query_runtime_route_coverage,
query_runtime_preflight_blocker_codes, query_runtime_probe_blocker_codes,
};
pub use hawdb_readiness::query_runtime_preflight_report::{
NowledgeQueryRuntimePreflightProbeReport, NowledgeQueryRuntimePreflightRedactionSummary,
NowledgeQueryRuntimePreflightReport,
};
pub use hawdb_readiness::slow_query::{
NowledgeMemSlowQueryRecord, NowledgeMemSlowQueryReport, NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL,
};
pub use hawdb_readiness::{NowledgeMemReadinessAreaMap, NowledgeMemReadinessAreaSummary};
pub use incremental_search::NowledgeMemIncrementalSearchMaintenanceOptions;
#[cfg(test)]
mod slow_query_facade_tests {
use super::*;
use std::any::TypeId;
#[test]
fn facade_reexports_slow_query_contracts_without_conversion() {
assert_eq!(
TypeId::of::<NowledgeMemSlowQueryRecord>(),
TypeId::of::<hawdb_readiness::slow_query::NowledgeMemSlowQueryRecord>(),
);
assert_eq!(
TypeId::of::<NowledgeMemSlowQueryReport>(),
TypeId::of::<hawdb_readiness::slow_query::NowledgeMemSlowQueryReport>(),
);
assert_eq!(
NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL,
hawdb_readiness::slow_query::NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL,
);
}
}
use hawdb_search::candidate_evidence::{
advised_compressed_vector_search_mode, effective_search_candidate_mode,
nowledge_mem_search_candidate_report, retrieval_projection_advisor_blocker_codes,
retrieval_projection_advisor_json,
};
pub use hawdb_search::candidate_evidence::{
nowledge_mem_search_candidate_shadow_evidence_json, NowledgeMemRetrievalProjectionAdvisor,
NowledgeMemSearchCandidateFieldSummary, NowledgeMemSearchCandidateFilterPushdownEvidence,
NowledgeMemSearchCandidateOutput, NowledgeMemSearchCandidateReadinessOptions,
NowledgeMemSearchCandidateReadinessReport, NowledgeMemSearchCandidateReport,
NowledgeMemSearchCandidateRequest, NowledgeMemSearchCandidateShadowAccumulator,
NowledgeMemSearchCandidateShadowEvidence, NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_ROUTE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_SOURCE, NOWLEDGE_MEM_SEARCH_CANDIDATE_PRIMARY_ENGINE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_READINESS_PROTOCOL,
NOWLEDGE_MEM_SEARCH_CANDIDATE_REPORT_PROTOCOL, NOWLEDGE_MEM_SEARCH_CANDIDATE_SHADOW_ENGINE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_SHADOW_EVIDENCE_PROTOCOL,
NOWLEDGE_MEM_SEARCH_CANDIDATE_TRACE_EVIDENCE_SOURCE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_TRACE_PRIMARY_ENGINE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_TRACE_SHADOW_ENGINE,
};
use std::collections::{BTreeMap, BTreeSet};
use std::mem::size_of;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::time::Instant;
#[cfg(test)]
#[path = "nowledge_mem_app_read_snapshot_tests.rs"]
mod app_read_snapshot_tests;
#[cfg(test)]
mod candidate_facade_tests;
#[cfg(test)]
mod runtime_status_facade_tests {
use super::{
NowledgeMemCutoverControls, NowledgeMemCutoverControlsReport, NowledgeMemProductionStatus,
NowledgeMemReadControl, NowledgeMemRuntimeStatus, NowledgeMemWorkControl,
};
use std::any::TypeId;
#[test]
fn root_facade_preserves_runtime_status_type_identity() {
assert_eq!(
TypeId::of::<NowledgeMemRuntimeStatus>(),
TypeId::of::<hawdb_readiness::nowledge_mem_runtime_status::NowledgeMemRuntimeStatus>()
);
assert_eq!(
TypeId::of::<NowledgeMemProductionStatus>(),
TypeId::of::<hawdb_readiness::nowledge_mem_runtime_status::NowledgeMemProductionStatus>(
)
);
assert_eq!(
TypeId::of::<NowledgeMemReadControl>(),
TypeId::of::<hawdb_readiness::nowledge_mem_cutover::NowledgeMemReadControl>()
);
assert_eq!(
TypeId::of::<NowledgeMemWorkControl>(),
TypeId::of::<hawdb_readiness::nowledge_mem_cutover::NowledgeMemWorkControl>()
);
assert_eq!(
TypeId::of::<NowledgeMemCutoverControls>(),
TypeId::of::<hawdb_readiness::nowledge_mem_cutover::NowledgeMemCutoverControls>()
);
assert_eq!(
TypeId::of::<NowledgeMemCutoverControlsReport>(),
TypeId::of::<hawdb_readiness::nowledge_mem_cutover::NowledgeMemCutoverControlsReport>()
);
}
}
pub use hawdb_readiness::nowledge_mem_serving_path::{
NowledgeMemServingEntrypoint, NowledgeMemServingPathReadiness,
NOWLEDGE_MEM_SERVING_PATH_READINESS_PROTOCOL,
};
const TYPED_CONTROL_STATEMENT_MEMORY_BYTES: u64 = 1024 * 1024;
const SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES: usize = 1024;
pub fn nowledge_mem_graph_config(mode: NowledgeMemGraphMode) -> DatabaseConfig {
nowledge_mem_graph_config_with_search_mode(mode, CompressedVectorSearchMode::Disabled)
}
pub fn nowledge_mem_graph_config_with_search_mode(
mode: NowledgeMemGraphMode,
compressed_vector_search_mode: CompressedVectorSearchMode,
) -> DatabaseConfig {
DatabaseConfig {
read_only: matches!(mode, NowledgeMemGraphMode::ShadowReadOnly),
compressed_vector_search_mode,
..DatabaseConfig::default()
}
}
fn default_nowledge_mem_runtime_governor(
path: &Path,
database_config: &DatabaseConfig,
) -> RuntimeGovernor {
let storage_device = StorageDeviceProfile::detect(path);
let mut governor_config = RuntimeGovernorConfig::shared_host();
if let Some(max_payload_bytes) = database_config.max_read_result_payload_bytes {
governor_config.result_budget_bytes = u64::try_from(max_payload_bytes).unwrap_or(u64::MAX);
}
RuntimeGovernor::detect(
governor_config,
IoConcurrencyBudget::shared_host_for_device(storage_device),
)
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemOpenOptions {
pub graph_path: PathBuf,
pub search_projection_path: Option<PathBuf>,
pub search_projection_open_mode: NowledgeMemSearchProjectionOpenMode,
pub mode: NowledgeMemGraphMode,
pub database_config: Option<DatabaseConfig>,
pub compressed_vector_search_mode: CompressedVectorSearchMode,
pub adaptive_vector_backend_policy: AdaptiveVectorBackendPolicy,
pub retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor,
pub search_range_read_config: Option<SearchRangeReadConfig>,
pub system_schema_registries: Vec<crate::SystemSchemaRegistry>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemQualifiedOutOfCoreSearchOptions {
pub config: SearchOutOfCoreConfig,
pub qualification: SearchLexicalProductionQualificationReport,
pub expected_identity: crate::ProductionQualificationIdentity,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NowledgeMemSearchProjectionOpenMode {
FullResidencyMaintenance,
IncrementalMaintenance(Box<NowledgeMemIncrementalSearchMaintenanceOptions>),
QualifiedOutOfCore(Box<NowledgeMemQualifiedOutOfCoreSearchOptions>),
}
impl NowledgeMemSearchProjectionOpenMode {
fn role(&self) -> NowledgeMemSearchProjectionRole {
match self {
Self::FullResidencyMaintenance => {
NowledgeMemSearchProjectionRole::FullResidencyMaintenance
}
Self::IncrementalMaintenance(_) => {
NowledgeMemSearchProjectionRole::IncrementalMaintenance
}
Self::QualifiedOutOfCore(_) => NowledgeMemSearchProjectionRole::QualifiedOutOfCore,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NowledgeMemSearchProjectionRole {
FullResidencyMaintenance,
IncrementalMaintenance,
QualifiedOutOfCore,
}
impl NowledgeMemSearchProjectionRole {
pub fn as_str(self) -> &'static str {
match self {
Self::FullResidencyMaintenance => "full_residency_maintenance",
Self::IncrementalMaintenance => "incremental_maintenance",
Self::QualifiedOutOfCore => "qualified_out_of_core",
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NowledgeMemOpenDiagnosticOptions {
pub include_local_paths: bool,
}
impl NowledgeMemOpenOptions {
pub fn graph_only(graph_path: impl Into<PathBuf>, mode: NowledgeMemGraphMode) -> Self {
Self {
graph_path: graph_path.into(),
search_projection_path: None,
search_projection_open_mode:
NowledgeMemSearchProjectionOpenMode::FullResidencyMaintenance,
mode,
database_config: None,
compressed_vector_search_mode: CompressedVectorSearchMode::Disabled,
adaptive_vector_backend_policy: AdaptiveVectorBackendPolicy::default(),
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor::default(),
search_range_read_config: None,
system_schema_registries: Vec::new(),
}
}
pub fn with_search_projection(
graph_path: impl Into<PathBuf>,
search_projection_path: impl Into<PathBuf>,
mode: NowledgeMemGraphMode,
) -> Self {
Self {
graph_path: graph_path.into(),
search_projection_path: Some(search_projection_path.into()),
search_projection_open_mode:
NowledgeMemSearchProjectionOpenMode::FullResidencyMaintenance,
mode,
database_config: None,
compressed_vector_search_mode: CompressedVectorSearchMode::Disabled,
adaptive_vector_backend_policy: AdaptiveVectorBackendPolicy::default(),
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor::default(),
search_range_read_config: None,
system_schema_registries: Vec::new(),
}
}
pub fn with_qualified_out_of_core_search_projection(
graph_path: impl Into<PathBuf>,
search_projection_path: impl Into<PathBuf>,
mode: NowledgeMemGraphMode,
qualified: NowledgeMemQualifiedOutOfCoreSearchOptions,
) -> Self {
Self {
graph_path: graph_path.into(),
search_projection_path: Some(search_projection_path.into()),
search_projection_open_mode: NowledgeMemSearchProjectionOpenMode::QualifiedOutOfCore(
Box::new(qualified),
),
mode,
database_config: None,
compressed_vector_search_mode: CompressedVectorSearchMode::Disabled,
adaptive_vector_backend_policy: AdaptiveVectorBackendPolicy::default(),
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor::default(),
search_range_read_config: None,
system_schema_registries: Vec::new(),
}
}
pub fn with_compressed_vector_search_mode(mut self, mode: CompressedVectorSearchMode) -> Self {
self.compressed_vector_search_mode = mode;
self
}
pub fn with_database_config(mut self, config: DatabaseConfig) -> Self {
self.database_config = Some(config);
self
}
pub fn with_system_schema_registry(mut self, registry: crate::SystemSchemaRegistry) -> Self {
self.system_schema_registries.push(registry);
self
}
pub fn with_adaptive_vector_backend_policy(
mut self,
policy: AdaptiveVectorBackendPolicy,
) -> Self {
self.adaptive_vector_backend_policy = policy;
self
}
pub fn with_retrieval_projection_advisor(
mut self,
advisor: NowledgeMemRetrievalProjectionAdvisor,
) -> Self {
self.retrieval_projection_advisor = advisor;
self
}
pub fn with_search_range_read_config(mut self, config: SearchRangeReadConfig) -> Self {
self.search_range_read_config = Some(config);
self
}
fn effective_compressed_vector_search_mode(&self) -> CompressedVectorSearchMode {
advised_compressed_vector_search_mode(
self.compressed_vector_search_mode,
&self.retrieval_projection_advisor,
)
}
fn effective_database_config(&self) -> DatabaseConfig {
let mut config = self.database_config.clone().unwrap_or_default();
config.read_only = matches!(self.mode, NowledgeMemGraphMode::ShadowReadOnly);
config.compressed_vector_search_mode = self.effective_compressed_vector_search_mode();
config.adaptive_vector_backend_policy = self.adaptive_vector_backend_policy;
config
}
fn validate(&self) -> Result<()> {
match (
self.search_projection_path.as_ref(),
&self.search_projection_open_mode,
) {
(None, NowledgeMemSearchProjectionOpenMode::IncrementalMaintenance(_)) => {
return Err(HawDBError::Semantic(
"incremental search maintenance requires search_projection_path".into(),
));
}
(Some(_), NowledgeMemSearchProjectionOpenMode::IncrementalMaintenance(options)) => {
options.validate()?;
if self.search_range_read_config.is_some() {
return Err(HawDBError::Semantic(
"incremental search maintenance uses its typed reader configuration".into(),
));
}
}
(None, NowledgeMemSearchProjectionOpenMode::QualifiedOutOfCore(_)) => {
return Err(HawDBError::Semantic(
"qualified out-of-core search requires search_projection_path".to_string(),
));
}
(Some(_), NowledgeMemSearchProjectionOpenMode::QualifiedOutOfCore(qualified)) => {
qualified.expected_identity.validate()?;
if self.search_range_read_config.is_some() {
return Err(HawDBError::Semantic(
"search_range_read_config applies only to the full-residency maintenance projection"
.to_string(),
));
}
}
_ => {}
}
if self.search_projection_path.is_none() && self.search_range_read_config.is_some() {
return Err(HawDBError::Semantic(
"search_range_read_config requires search_projection_path".to_string(),
));
}
let mut schema_owners = BTreeSet::new();
for registry in &self.system_schema_registries {
if !schema_owners.insert(registry.owner()) {
return Err(HawDBError::Semantic(format!(
"system schema owner {} is registered more than once",
registry.owner()
)));
}
}
Ok(())
}
pub fn sanitized_report(&self) -> NowledgeMemOpenReport {
let effective_compressed_vector_search_mode =
self.effective_compressed_vector_search_mode();
NowledgeMemOpenReport {
protocol: NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL.to_string(),
mode: self.mode,
graph_configured: true,
search_projection_configured: self.search_projection_path.is_some(),
search_projection_role: self
.search_projection_path
.as_ref()
.map(|_| self.search_projection_open_mode.role()),
compressed_vector_search_mode: effective_compressed_vector_search_mode,
requested_compressed_vector_search_mode: self.compressed_vector_search_mode,
adaptive_vector_backend_policy: self.adaptive_vector_backend_policy,
retrieval_projection_advisor: self.retrieval_projection_advisor.clone(),
retrieval_projection_advisor_blocker_codes: if self.compressed_vector_search_mode
== effective_compressed_vector_search_mode
{
Vec::new()
} else {
retrieval_projection_advisor_blocker_codes(&self.retrieval_projection_advisor)
},
graph_opened: false,
search_projection_opened: false,
search_production_qualification_bound: false,
system_schema_upgrades: Vec::new(),
}
}
pub fn diagnostic_report_json(
&self,
options: NowledgeMemOpenDiagnosticOptions,
) -> serde_json::Value {
let mut report = self.sanitized_report().json();
if let Some(object) = report.as_object_mut() {
object.insert(
"debug_local_paths_included".to_string(),
serde_json::Value::Bool(options.include_local_paths),
);
object.insert(
"local_paths_redacted".to_string(),
serde_json::Value::Bool(!options.include_local_paths),
);
if options.include_local_paths {
object.insert(
"graph_path".to_string(),
serde_json::Value::String(self.graph_path.to_string_lossy().into_owned()),
);
object.insert(
"search_projection_path".to_string(),
self.search_projection_path
.as_ref()
.map(|path| serde_json::Value::String(path.to_string_lossy().into_owned()))
.unwrap_or(serde_json::Value::Null),
);
}
}
report
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemOpenReport {
pub protocol: String,
pub mode: NowledgeMemGraphMode,
pub graph_configured: bool,
pub search_projection_configured: bool,
pub search_projection_role: Option<NowledgeMemSearchProjectionRole>,
pub compressed_vector_search_mode: CompressedVectorSearchMode,
pub requested_compressed_vector_search_mode: CompressedVectorSearchMode,
pub adaptive_vector_backend_policy: AdaptiveVectorBackendPolicy,
pub retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor,
pub retrieval_projection_advisor_blocker_codes: Vec<String>,
pub graph_opened: bool,
pub search_projection_opened: bool,
pub search_production_qualification_bound: bool,
pub system_schema_upgrades: Vec<crate::SystemSchemaUpgradeReport>,
}
impl NowledgeMemOpenReport {
pub fn json(&self) -> serde_json::Value {
serde_json::json!({
"protocol": self.protocol,
"mode": self.mode.as_str(),
"graph_configured": self.graph_configured,
"search_projection_configured": self.search_projection_configured,
"search_projection_role": self.search_projection_role.map(NowledgeMemSearchProjectionRole::as_str),
"compressed_vector_search_mode": self.compressed_vector_search_mode.as_str(),
"requested_compressed_vector_search_mode": self.requested_compressed_vector_search_mode.as_str(),
"adaptive_vector_backend_policy": {
"flat_scan_max_documents": self.adaptive_vector_backend_policy.flat_scan_max_documents,
"high_filter_selectivity_per_million": self.adaptive_vector_backend_policy.high_filter_selectivity_per_million,
"flat_scan_memory_budget_bytes": self.adaptive_vector_backend_policy.flat_scan_memory_budget_bytes,
},
"retrieval_projection_advisor": retrieval_projection_advisor_json(&self.retrieval_projection_advisor),
"retrieval_projection_advisor_blocker_codes": self.retrieval_projection_advisor_blocker_codes,
"graph_opened": self.graph_opened,
"search_projection_opened": self.search_projection_opened,
"search_production_qualification_bound": self.search_production_qualification_bound,
"system_schema_upgrades": self.system_schema_upgrades.iter().map(|upgrade| serde_json::json!({
"owner": upgrade.owner,
"previous_version": upgrade.previous_version,
"current_version": upgrade.current_version,
"applied_versions": upgrade.applied_versions,
"commit_epoch_before": upgrade.commit_epoch_before,
"commit_epoch_after": upgrade.commit_epoch_after,
})).collect::<Vec<_>>(),
})
}
}
pub use hawdb_readiness::nowledge_mem_runtime_status::{
NowledgeMemProductionStatus, NowledgeMemRuntimeStatus, NOWLEDGE_MEM_PRODUCTION_STATUS_PROTOCOL,
NOWLEDGE_MEM_RUNTIME_STATUS_PROTOCOL,
};
pub use hawdb_readiness::nowledge_mem_cutover::{
NowledgeMemCutoverControls, NowledgeMemCutoverControlsReport, NowledgeMemReadControl,
NowledgeMemWorkControl,
};
pub use hawdb_nowledge_contracts::{
NowledgeMemReadSnapshotBudget, NowledgeMemReadSnapshotReport,
NowledgeMemStorageLifecycleActionKind, NowledgeMemStorageLifecycleDecision,
NowledgeMemStorageRecoveryReport, NOWLEDGE_MEM_READ_SNAPSHOT_REPORT_PROTOCOL,
NOWLEDGE_MEM_STORAGE_LIFECYCLE_DECISION_PROTOCOL,
};
#[cfg(test)]
mod storage_lifecycle_facade_tests {
use super::*;
use std::any::TypeId;
#[test]
fn facade_reexports_storage_lifecycle_contracts_without_conversion() {
assert_eq!(
TypeId::of::<NowledgeMemStorageRecoveryReport>(),
TypeId::of::<hawdb_nowledge_contracts::NowledgeMemStorageRecoveryReport>(),
);
assert_eq!(
TypeId::of::<NowledgeMemStorageLifecycleDecision>(),
TypeId::of::<hawdb_nowledge_contracts::NowledgeMemStorageLifecycleDecision>(),
);
assert_eq!(
TypeId::of::<NowledgeMemStorageLifecycleActionKind>(),
TypeId::of::<hawdb_nowledge_contracts::NowledgeMemStorageLifecycleActionKind>(),
);
}
#[test]
fn facade_reexports_read_snapshot_contracts_without_conversion() {
assert_eq!(
TypeId::of::<NowledgeMemReadSnapshotBudget>(),
TypeId::of::<hawdb_nowledge_contracts::NowledgeMemReadSnapshotBudget>(),
);
assert_eq!(
TypeId::of::<NowledgeMemReadSnapshotReport>(),
TypeId::of::<hawdb_nowledge_contracts::NowledgeMemReadSnapshotReport>(),
);
assert_eq!(
NOWLEDGE_MEM_READ_SNAPSHOT_REPORT_PROTOCOL,
hawdb_nowledge_contracts::NOWLEDGE_MEM_READ_SNAPSHOT_REPORT_PROTOCOL,
);
}
}
pub use hawdb_readiness::source_mutation::{
nowledge_mem_source_mutation_dual_write_evidence_all_ready,
nowledge_mem_source_mutation_dual_write_readiness,
nowledge_mem_source_mutation_family_requirements, NowledgeMemSourceMutationDualWriteEvidence,
NowledgeMemSourceMutationDualWriteReadinessReport, NowledgeMemSourceMutationFamilyRequirement,
NOWLEDGE_MEM_SOURCE_MUTATION_DUAL_WRITE_READINESS_PROTOCOL,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_CONTENT_REFRESH_REPARSE,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_GRAPH_DELETE,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_INDEXED_TRANSITION,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_INGEST_CREATE,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_LIFECYCLE,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_PATCH_DELETE,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_REVISION_EDGES,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_SEARCH_PROJECTION_EFFECTS,
REQUIRED_NOWLEDGE_MEM_SOURCE_MUTATION_FAMILIES,
};
pub const NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL: &str = "hawdb-nowledge-mem-open-report";
pub use hawdb_readiness::previous_wrapper_preflight::{
NOWLEDGE_MEM_CUTOVER_CONTROLS_PROTOCOL, NOWLEDGE_MEM_LIBRARY_READINESS_PROTOCOL,
NOWLEDGE_MEM_OPERATIONS_READINESS_PROTOCOL, NOWLEDGE_QUERY_RUNTIME_PREFLIGHT_PROTOCOL,
};
pub const NOWLEDGE_MEM_RETRIEVAL_REPORT_PROTOCOL: &str = "hawdb-nowledge-mem-retrieval-report";
pub const NOWLEDGE_MEM_READINESS_DASHBOARD_PROTOCOL: &str =
"hawdb-nowledge-mem-readiness-dashboard-v1";
pub use hawdb_route_ownership::graph::{
nowledge_mem_graph_read_route_catalog_digest, nowledge_mem_graph_read_route_spec,
nowledge_mem_graph_read_route_spec_json, nowledge_mem_graph_read_route_specs_json,
nowledge_mem_required_query_families_for_route, NowledgeMemGraphReadRouteEvidenceKind,
NowledgeMemGraphReadRouteOwner, NowledgeMemGraphReadRouteSpec,
NOWLEDGE_MEM_GRAPH_READ_ROUTE_CATALOG_VERSION, NOWLEDGE_MEM_GRAPH_READ_ROUTE_SPECS,
NOWLEDGE_MEM_SEARCH_ROUTE, REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES,
};
pub const DEFAULT_NOWLEDGE_MEM_READ_MAX_ROWS: usize = 512;
pub const DEFAULT_NOWLEDGE_MEM_READ_MAX_ESTIMATED_PAYLOAD_BYTES: usize = 4 * 1024 * 1024;
#[derive(Debug)]
pub struct NowledgeMemGraph {
db: Database,
mode: NowledgeMemGraphMode,
runtime_governor: RuntimeGovernor,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemReadOptions {
pub max_rows: Option<usize>,
pub max_estimated_payload_bytes: Option<usize>,
}
impl Default for NowledgeMemReadOptions {
fn default() -> Self {
Self {
max_rows: Some(DEFAULT_NOWLEDGE_MEM_READ_MAX_ROWS),
max_estimated_payload_bytes: Some(
DEFAULT_NOWLEDGE_MEM_READ_MAX_ESTIMATED_PAYLOAD_BYTES,
),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemReadOutput {
pub output: QueryOutput,
pub report: NowledgeMemReadReport,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub struct NowledgeMemReadinessOptions {
pub bounded_read_probe: Option<NowledgeGraphStatement>,
pub bounded_read_evidence: Option<serde_json::Value>,
pub covered_routes: Vec<String>,
pub graph_route_readiness: Option<NowledgeMemRouteReadinessSummary>,
pub search_route_ownership: Option<NowledgeMemSearchRouteOwnershipReadinessReport>,
pub active_search_route_ownership: Option<NowledgeMemActiveSearchRouteOwnershipReadinessReport>,
pub active_search_route_readiness: Option<NowledgeMemActiveSearchRouteReadinessReport>,
pub replacement_readiness_by_query_family: Option<serde_json::Value>,
pub read_options: NowledgeMemReadOptions,
pub search_projection_evidence: Option<serde_json::Value>,
pub search_projection_probe_options: SearchProjectionProbeOptions,
pub primary_search_projection_probe: Option<serde_json::Value>,
pub search_projection_shadow_evidence: Option<serde_json::Value>,
pub search_candidate_shadow_evidence: Option<serde_json::Value>,
pub workload_fixture_evidence: Option<NowledgeGraphRouteWorkloadFixtureReport>,
pub production_resource_profile: Option<StorageResourceProfileReport>,
pub qos_policy: LocalQosPolicy,
pub qos_state: LocalQosState,
pub background_maintenance_options: BackgroundMaintenanceOptions,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemLibraryReadinessReport {
pub protocol: String,
pub present: bool,
pub ready: bool,
pub mode: NowledgeMemGraphMode,
pub redaction: NowledgeMemReadinessRedactionSummary,
pub production_path: NowledgeMemLibraryProductionPathSummary,
pub blocker_codes: Vec<String>,
pub readiness_by_area: NowledgeMemReadinessAreaMap,
pub ready_area_count: usize,
pub blocked_area_count: usize,
pub graph_open: bool,
pub graph_read_only: bool,
pub graph_route_readiness: serde_json::Value,
pub search_route_ownership: serde_json::Value,
pub active_search_route_ownership: serde_json::Value,
pub active_search_route_readiness: serde_json::Value,
pub bounded_read_evidence: serde_json::Value,
pub storage_recovery: serde_json::Value,
pub background_maintenance: serde_json::Value,
pub query_family_evidence: serde_json::Value,
pub search_projection_evidence: serde_json::Value,
pub search_projection_shadow_evidence: serde_json::Value,
pub search_candidate_shadow_evidence: serde_json::Value,
pub workload_fixture_evidence: serde_json::Value,
pub production_resource_profile: serde_json::Value,
}
impl NowledgeMemLibraryReadinessReport {
pub fn areas(&self) -> Vec<NowledgeMemReadinessAreaSummary> {
self.readiness_by_area.areas()
}
pub fn json(&self) -> serde_json::Value {
let areas = self.areas();
serde_json::json!({
"protocol": self.protocol,
"present": self.present,
"ready": self.ready,
"mode": self.mode.as_str(),
"redaction": self.redaction.json(),
"production_path": self.production_path.json(),
"blocker_codes": self.blocker_codes,
"readiness_by_area": self.readiness_by_area.json(),
"areas": areas.iter().map(NowledgeMemReadinessAreaSummary::json).collect::<Vec<_>>(),
"ready_area_count": self.ready_area_count,
"blocked_area_count": self.blocked_area_count,
"graph": {
"open": self.graph_open,
"mode": self.mode.as_str(),
"read_only": self.graph_read_only,
},
"graph_route_readiness": self.graph_route_readiness,
"search_route_ownership": self.search_route_ownership,
"active_search_route_ownership": self.active_search_route_ownership,
"active_search_route_readiness": self.active_search_route_readiness,
"bounded_read_evidence": self.bounded_read_evidence,
"storage_recovery": self.storage_recovery,
"background_maintenance": self.background_maintenance,
"query_family_evidence": self.query_family_evidence,
"search_projection_evidence": self.search_projection_evidence,
"search_projection_shadow_evidence": self.search_projection_shadow_evidence,
"search_candidate_shadow_evidence": self.search_candidate_shadow_evidence,
"workload_fixture_evidence": self.workload_fixture_evidence,
"production_resource_profile": self.production_resource_profile,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NowledgeMemLibraryProductionPathSummary {
pub in_process: bool,
pub cli_required: bool,
pub env_control_plane_required: bool,
pub spawned_helper_required: bool,
}
impl Default for NowledgeMemLibraryProductionPathSummary {
fn default() -> Self {
Self {
in_process: true,
cli_required: false,
env_control_plane_required: false,
spawned_helper_required: false,
}
}
}
impl NowledgeMemLibraryProductionPathSummary {
pub fn ready(&self) -> bool {
self.in_process
&& !self.cli_required
&& !self.env_control_plane_required
&& !self.spawned_helper_required
}
fn json(&self) -> serde_json::Value {
serde_json::json!({
"ready": self.ready(),
"in_process": self.in_process,
"cli_required": self.cli_required,
"env_control_plane_required": self.env_control_plane_required,
"spawned_helper_required": self.spawned_helper_required,
})
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NowledgeMemReadinessRedactionSummary {
pub query_text_copied: bool,
pub parameters_copied: bool,
pub local_paths_copied: bool,
}
impl NowledgeMemReadinessRedactionSummary {
pub fn ready(&self) -> bool {
!self.query_text_copied && !self.parameters_copied && !self.local_paths_copied
}
fn json(&self) -> serde_json::Value {
serde_json::json!({
"ready": self.ready(),
"query_text_copied": self.query_text_copied,
"parameters_copied": self.parameters_copied,
"local_paths_copied": self.local_paths_copied,
})
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemReadinessDashboard {
pub protocol: String,
pub ready: bool,
pub mode: NowledgeMemGraphMode,
pub area_count: usize,
pub ready_area_count: usize,
pub blocked_area_count: usize,
pub blocker_codes: Vec<String>,
pub areas: Vec<NowledgeMemReadinessAreaSummary>,
pub storage_lifecycle_action: NowledgeMemStorageLifecycleActionKind,
pub storage_lifecycle_ready: bool,
pub slow_query_ready: bool,
pub slow_query_record_count: usize,
}
impl NowledgeMemReadinessDashboard {
fn from_reports(
library: &NowledgeMemLibraryReadinessReport,
storage_lifecycle: &NowledgeMemStorageLifecycleDecision,
slow_query: &NowledgeMemSlowQueryReport,
) -> Self {
let areas = nowledge_mem_readiness_dashboard_areas(library, slow_query);
let blocked_area_count = areas.iter().filter(|area| !area.ready).count();
let ready_area_count = areas.len().saturating_sub(blocked_area_count);
Self {
protocol: NOWLEDGE_MEM_READINESS_DASHBOARD_PROTOCOL.to_string(),
ready: library.ready && slow_query.ready,
mode: library.mode,
area_count: areas.len(),
ready_area_count,
blocked_area_count,
blocker_codes: library.blocker_codes.clone(),
areas,
storage_lifecycle_action: storage_lifecycle.action,
storage_lifecycle_ready: storage_lifecycle.ready_for_mem_lifecycle,
slow_query_ready: slow_query.ready,
slow_query_record_count: slow_query.record_count,
}
}
pub fn json(&self) -> serde_json::Value {
serde_json::json!({
"protocol": self.protocol,
"ready": self.ready,
"mode": self.mode.as_str(),
"area_count": self.area_count,
"ready_area_count": self.ready_area_count,
"blocked_area_count": self.blocked_area_count,
"blocker_codes": self.blocker_codes,
"areas": self.areas.iter().map(NowledgeMemReadinessAreaSummary::json).collect::<Vec<_>>(),
"storage_lifecycle": {
"ready": self.storage_lifecycle_ready,
"action": self.storage_lifecycle_action.as_str(),
},
"slow_query": {
"ready": self.slow_query_ready,
"record_count": self.slow_query_record_count,
},
"redaction": {
"query_text_copied": false,
"parameters_copied": false,
"local_paths_copied": false
},
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemOperationsReadinessReport {
pub protocol: String,
pub present: bool,
pub ready: bool,
pub mode: NowledgeMemGraphMode,
pub graph_open: bool,
pub graph_read_only: bool,
pub search_projection_open: bool,
pub graph_commit_epoch: u64,
pub projection_commit_lag: u64,
pub projection_stale: bool,
pub storage_lifecycle_action: NowledgeMemStorageLifecycleActionKind,
pub storage_lifecycle_ready: bool,
pub storage_recovery_ready: bool,
pub slow_query_ready: bool,
pub background_maintenance_ready: bool,
pub blocker_codes: Vec<String>,
pub runtime_status: NowledgeMemRuntimeStatus,
pub storage_lifecycle_decision: NowledgeMemStorageLifecycleDecision,
pub storage_recovery: NowledgeMemStorageRecoveryReport,
pub slow_query: NowledgeMemSlowQueryReport,
pub background_maintenance: NowledgeMemBackgroundMaintenanceReport,
}
impl NowledgeMemOperationsReadinessReport {
fn from_reports(
mode: NowledgeMemGraphMode,
graph_read_only: bool,
runtime_status: NowledgeMemRuntimeStatus,
storage_recovery: NowledgeMemStorageRecoveryReport,
slow_query: NowledgeMemSlowQueryReport,
background_maintenance: NowledgeMemBackgroundMaintenanceReport,
) -> Self {
let search_projection_open = runtime_status.projection_freshness.is_some();
let projection_commit_lag = runtime_status.projection_commit_lag();
let projection_stale = search_projection_open && runtime_status.projection_stale();
let storage_lifecycle_decision =
NowledgeMemStorageLifecycleDecision::from_storage_recovery(storage_recovery.clone());
let mut blocker_codes = Vec::new();
if !storage_lifecycle_decision.ready_for_mem_lifecycle {
blocker_codes.push("storage_recovery_not_ready".to_string());
blocker_codes.extend(
storage_lifecycle_decision
.blocker_codes
.iter()
.map(|code| format!("storage_lifecycle.{code}")),
);
}
if !slow_query.ready {
blocker_codes.push("slow_query_report_not_ready".to_string());
}
if !background_maintenance.ready {
blocker_codes.push("background_maintenance_not_ready".to_string());
}
if search_projection_open && projection_stale {
blocker_codes.push("search_projection_stale".to_string());
}
Self {
protocol: NOWLEDGE_MEM_OPERATIONS_READINESS_PROTOCOL.to_string(),
present: true,
ready: blocker_codes.is_empty(),
mode,
graph_open: true,
graph_read_only,
search_projection_open,
graph_commit_epoch: runtime_status.graph_commit_epoch,
projection_commit_lag,
projection_stale,
storage_lifecycle_action: storage_lifecycle_decision.action,
storage_lifecycle_ready: storage_lifecycle_decision.ready_for_mem_lifecycle,
storage_recovery_ready: storage_recovery.ready,
slow_query_ready: slow_query.ready,
background_maintenance_ready: background_maintenance.ready,
blocker_codes,
runtime_status,
storage_lifecycle_decision,
storage_recovery,
slow_query,
background_maintenance,
}
}
pub fn json(&self) -> serde_json::Value {
serde_json::json!({
"protocol": self.protocol,
"present": self.present,
"ready": self.ready,
"mode": self.mode.as_str(),
"graph": {
"open": self.graph_open,
"read_only": self.graph_read_only,
"commit_epoch": self.graph_commit_epoch,
},
"search_projection": {
"open": self.search_projection_open,
"commit_lag": self.projection_commit_lag,
"stale": self.projection_stale,
},
"storage_lifecycle": {
"ready": self.storage_lifecycle_ready,
"action": self.storage_lifecycle_action.as_str(),
},
"readiness": {
"storage_lifecycle_ready": self.storage_lifecycle_ready,
"storage_recovery_ready": self.storage_recovery_ready,
"slow_query_ready": self.slow_query_ready,
"background_maintenance_ready": self.background_maintenance_ready,
},
"blocker_codes": self.blocker_codes,
"runtime_status": self.runtime_status.json(),
"storage_lifecycle_decision": self.storage_lifecycle_decision.json(),
"storage_recovery": self.storage_recovery.json(),
"slow_query": self.slow_query.json(),
"background_maintenance": self.background_maintenance.json(),
"redaction": {
"query_text_copied": false,
"parameters_copied": false,
"local_paths_copied": false,
},
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemBackgroundMaintenanceReport {
pub protocol: String,
pub present: bool,
pub ready: bool,
pub total_candidates: usize,
pub admitted_count: usize,
pub deferred_count: usize,
pub rejected_count: usize,
pub total_estimated_operations: usize,
pub admitted_estimated_operations: usize,
pub deferred_estimated_operations: usize,
pub rejected_estimated_operations: usize,
pub executable_search_projection_graph_delta_count: usize,
pub admitted_search_projection_graph_delta_count: usize,
pub deferred_search_projection_graph_delta_count: usize,
pub rejected_search_projection_graph_delta_count: usize,
pub executable_search_projection_graph_delta_operations: usize,
pub admitted_search_projection_graph_delta_operations: usize,
pub max_search_projection_graph_delta_complete_through_graph_commit_epoch: Option<u64>,
pub foreground_admission_probe_ready: Option<bool>,
pub foreground_admission_probe_admission_name: Option<String>,
pub memory_pressure_ready: Option<bool>,
pub memory_budget_bytes: Option<u64>,
pub estimated_memory_bytes: Option<u64>,
pub slow_query_ready: Option<bool>,
pub slow_query_record_count: Option<u64>,
pub slow_query_capacity: Option<u64>,
pub slow_query_redaction_ready: Option<bool>,
pub top_admitted_kind: Option<BackgroundMaintenanceKind>,
pub top_admitted_name: Option<String>,
pub ranked_count: u64,
pub foreground_ranked_count: u64,
pub unknown_admission_count: u64,
pub blocker_codes: Vec<String>,
summary: serde_json::Value,
}
impl NowledgeMemBackgroundMaintenanceReport {
pub fn from_summary(summary: &BackgroundMaintenanceSummary) -> Self {
Self::from_summary_with_slow_query(summary, None)
}
fn from_summary_with_slow_query(
summary: &BackgroundMaintenanceSummary,
slow_query: Option<&NowledgeMemSlowQueryReport>,
) -> Self {
let mut json = background_maintenance_summary_to_json(summary);
if let Some(object) = json.as_object_mut() {
object.insert(
"protocol".to_string(),
serde_json::Value::String("hawdb-background-maintenance-report".to_string()),
);
if let Some(slow_query) = slow_query {
object.insert(
"slow_query".to_string(),
background_maintenance_slow_query_json(slow_query),
);
object.insert(
"slow_query_ready".to_string(),
serde_json::Value::Bool(slow_query.ready),
);
object.insert(
"slow_query_record_count".to_string(),
serde_json::json!(slow_query.record_count),
);
object.insert(
"slow_query_capacity".to_string(),
serde_json::json!(slow_query.capacity),
);
}
}
let health = background_maintenance_evidence_health(Some(&json), true);
Self {
protocol: "hawdb-background-maintenance-report".to_string(),
present: true,
ready: health.ready,
total_candidates: summary.total_candidates,
admitted_count: summary.admitted_count,
deferred_count: summary.deferred_count,
rejected_count: summary.rejected_count,
total_estimated_operations: summary.total_estimated_operations,
admitted_estimated_operations: summary.admitted_estimated_operations,
deferred_estimated_operations: summary.deferred_estimated_operations,
rejected_estimated_operations: summary.rejected_estimated_operations,
executable_search_projection_graph_delta_count: summary
.executable_search_projection_graph_delta_count,
admitted_search_projection_graph_delta_count: summary
.admitted_search_projection_graph_delta_count,
deferred_search_projection_graph_delta_count: summary
.deferred_search_projection_graph_delta_count,
rejected_search_projection_graph_delta_count: summary
.rejected_search_projection_graph_delta_count,
executable_search_projection_graph_delta_operations: summary
.executable_search_projection_graph_delta_operations,
admitted_search_projection_graph_delta_operations: summary
.admitted_search_projection_graph_delta_operations,
max_search_projection_graph_delta_complete_through_graph_commit_epoch: summary
.max_search_projection_graph_delta_complete_through_graph_commit_epoch,
foreground_admission_probe_ready: health.foreground_admission_probe_ready,
foreground_admission_probe_admission_name: health
.foreground_admission_probe_admission_name,
memory_pressure_ready: health.memory_pressure_ready,
memory_budget_bytes: health.memory_budget_bytes,
estimated_memory_bytes: health.estimated_memory_bytes,
slow_query_ready: health.slow_query_ready,
slow_query_record_count: health.slow_query_record_count,
slow_query_capacity: health.slow_query_capacity,
slow_query_redaction_ready: health.slow_query_redaction_ready,
top_admitted_kind: summary.top_admitted_kind,
top_admitted_name: summary.top_admitted_name.clone(),
ranked_count: health.ranked_count.unwrap_or_default(),
foreground_ranked_count: health.foreground_ranked_count,
unknown_admission_count: health.unknown_admission_count,
blocker_codes: health.blocker_codes,
summary: json,
}
}
pub fn json(&self) -> serde_json::Value {
self.summary.clone()
}
}
fn background_maintenance_slow_query_json(
slow_query: &NowledgeMemSlowQueryReport,
) -> serde_json::Value {
serde_json::json!({
"protocol": slow_query.protocol,
"ready": slow_query.ready,
"capacity": slow_query.capacity,
"record_count": slow_query.record_count,
"latest_sequence": slow_query.latest_sequence,
"max_elapsed_micros": slow_query.max_elapsed_micros,
"redaction": {
"query_text_copied": false,
"parameters_copied": false,
"local_paths_copied": false,
},
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NowledgeMemRetrievalReport {
pub protocol: String,
pub mode: NowledgeMemGraphMode,
pub compressed_vector_search_mode: CompressedVectorSearchMode,
pub graph_commit_epoch: u64,
pub projection_source_graph_commit_epoch: Option<u64>,
pub projection_commit_lag: u64,
pub projection_stale: bool,
pub search_document_count: usize,
pub search_filtered_document_count: usize,
pub search_total_hits: usize,
pub candidate_count: usize,
pub candidate_total_count: usize,
pub evidence_count: usize,
pub graph_seed_count: usize,
pub graph_context_path_count: usize,
pub search_backend: Option<String>,
pub vector_backend: Option<String>,
pub text_backend: Option<String>,
pub search_fallback_reason_codes: Vec<String>,
pub retriever_fallback_reason_codes: Vec<String>,
pub knowledge_fallback_reason_codes: Vec<String>,
pub truncation_reason_codes: Vec<String>,
pub warning_count: usize,
pub warnings: Vec<String>,
}
impl NowledgeMemRetrievalReport {
pub fn json(&self) -> serde_json::Value {
serde_json::json!({
"protocol": self.protocol,
"mode": self.mode.as_str(),
"compressed_vector_search_mode": self.compressed_vector_search_mode.as_str(),
"graph_commit_epoch": self.graph_commit_epoch,
"projection_source_graph_commit_epoch": self.projection_source_graph_commit_epoch,
"projection_commit_lag": self.projection_commit_lag,
"projection_stale": self.projection_stale,
"search_document_count": self.search_document_count,
"search_filtered_document_count": self.search_filtered_document_count,
"search_total_hits": self.search_total_hits,
"candidate_count": self.candidate_count,
"candidate_total_count": self.candidate_total_count,
"evidence_count": self.evidence_count,
"graph_seed_count": self.graph_seed_count,
"graph_context_path_count": self.graph_context_path_count,
"search_backend": self.search_backend,
"vector_backend": self.vector_backend,
"text_backend": self.text_backend,
"search_fallback_reason_codes": self.search_fallback_reason_codes,
"retriever_fallback_reason_codes": self.retriever_fallback_reason_codes,
"knowledge_fallback_reason_codes": self.knowledge_fallback_reason_codes,
"truncation_reason_codes": self.truncation_reason_codes,
"warning_count": self.warning_count,
"warnings": self.warnings,
})
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemRetrievalOutput {
pub output: KnowledgeRetrievalOutput,
pub report: NowledgeMemRetrievalReport,
pub out_of_core_search_metrics: Option<SearchOutOfCoreMetrics>,
}
impl NowledgeMemGraph {
pub fn open(path: impl AsRef<Path>, mode: NowledgeMemGraphMode) -> Result<Self> {
let path = path.as_ref();
Self::open_with_config(path, nowledge_mem_graph_config(mode))
}
pub fn open_with_config(path: impl AsRef<Path>, config: DatabaseConfig) -> Result<Self> {
let path = path.as_ref();
let runtime_governor = default_nowledge_mem_runtime_governor(path, &config);
Self::open_with_config_and_runtime_governor(path, config, runtime_governor)
}
pub fn open_with_config_and_runtime_governor(
path: impl AsRef<Path>,
config: DatabaseConfig,
runtime_governor: RuntimeGovernor,
) -> Result<Self> {
let mode = if config.read_only {
NowledgeMemGraphMode::ShadowReadOnly
} else {
NowledgeMemGraphMode::WritableCutover
};
let mut db = Database::open_with_config(path, config)?;
db.set_runtime_governor(runtime_governor.clone());
Ok(Self {
db,
mode,
runtime_governor,
})
}
pub fn from_database(db: Database, mode: NowledgeMemGraphMode) -> Self {
let runtime_governor = default_nowledge_mem_runtime_governor(Path::new("."), db.config());
Self::from_database_with_runtime_governor(db, mode, runtime_governor)
}
pub fn from_database_with_runtime_governor(
mut db: Database,
mode: NowledgeMemGraphMode,
runtime_governor: RuntimeGovernor,
) -> Self {
db.set_runtime_governor(runtime_governor.clone());
Self {
db,
mode,
runtime_governor,
}
}
pub fn mode(&self) -> NowledgeMemGraphMode {
self.mode
}
pub fn database(&self) -> &Database {
&self.db
}
pub fn database_mut(&mut self) -> &mut Database {
&mut self.db
}
pub fn runtime_governor(&self) -> &RuntimeGovernor {
&self.runtime_governor
}
pub fn runtime_governor_snapshot(&self) -> RuntimeGovernorSnapshot {
self.runtime_governor.snapshot()
}
pub fn refresh_runtime_resources(&self) -> bool {
self.runtime_governor.refresh_from_host()
}
pub fn into_database(self) -> Database {
self.db
}
pub fn query(&mut self, cypher: &str) -> Result<QueryOutput> {
self.query_with_params(cypher, &BTreeMap::new())
}
pub fn query_with_report(&mut self, cypher: &str) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report(cypher, &BTreeMap::new())
}
pub fn query_with_report_options(
&mut self,
cypher: &str,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options(cypher, &BTreeMap::new(), options)
}
pub fn query_with_params(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
) -> Result<QueryOutput> {
Ok(self
.query_with_params_with_report(cypher, parameters)?
.output)
}
pub fn query_with_params_with_report(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options(
cypher,
parameters,
NowledgeMemQueryReportOptions::default(),
)
}
pub fn query_with_params_with_report_options(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
let mut external = crate::executor::NoExternalReadOperator;
self.query_with_params_with_report_options_and_external(
cypher,
parameters,
options,
&mut external,
)
}
pub fn query_with_params_with_report_options_context(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
task_context: &RuntimeTaskContext,
) -> Result<NowledgeMemQueryOutput> {
let mut external = crate::executor::NoExternalReadOperator;
self.query_with_params_with_report_options_and_external_context(
cypher,
parameters,
options,
&mut external,
task_context,
)
}
fn query_with_params_with_report_options_and_external(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
external: &mut dyn crate::executor::ExternalReadOperator,
) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options_and_external_context(
cypher,
parameters,
options,
external,
&RuntimeTaskContext::default(),
)
}
fn query_with_params_with_report_options_and_external_context(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
external: &mut dyn crate::executor::ExternalReadOperator,
task_context: &RuntimeTaskContext,
) -> Result<NowledgeMemQueryOutput> {
self.check_runtime_context(task_context)?;
let (permit, is_mutation) = self.admit_materialized_query(cypher, parameters)?;
let execution_task_context = permit.bind_task_context(task_context.clone());
let started = Instant::now();
let result = self.db.query_with_params_trace_and_external_with_context(
cypher,
parameters,
options.capture_physical_plan,
external,
None,
Some(&execution_task_context),
);
self.record_runtime_cancellation(result.as_ref().err(), task_context);
let (output, execution_trace) = result?;
if !is_mutation {
self.check_runtime_context(task_context)?;
}
let elapsed_micros = started.elapsed().as_micros();
let report = nowledge_mem_query_report(NowledgeMemQueryReportInput {
mode: self.mode,
statement: &execution_trace.statement,
trace: execution_trace.optimizer_trace.as_ref(),
plan_cache_lookup: execution_trace.plan_cache_lookup,
execution_profile: execution_trace.execution_profile.as_ref(),
output: &output,
options,
elapsed_micros,
});
Ok(NowledgeMemQueryOutput { output, report })
}
fn admit_materialized_query(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
) -> Result<(RuntimePermit, bool)> {
let admission = self.db.runtime_admission_plan(cypher, parameters)?;
let is_mutation = admission.is_mutation;
let governor_budget = self.runtime_governor.snapshot().limits.result_budget_bytes;
let result_budget = if admission.is_mutation {
governor_budget
} else {
let configured = self
.db
.config()
.max_read_result_payload_bytes
.ok_or_else(|| {
HawDBError::Execution(
"admitted materialized query requires max_read_result_payload_bytes"
.to_string(),
)
})?;
let configured = u64::try_from(configured).unwrap_or(u64::MAX);
if configured > governor_budget {
return Err(HawDBError::Execution(format!(
"admitted materialized query payload budget {configured} exceeds runtime result budget {governor_budget}"
)));
}
configured
};
self.try_admit_runtime(
admission
.runtime_work_request_for_snapshot(result_budget, self.runtime_governor.snapshot()),
)
.map(|permit| (permit, is_mutation))
}
fn admit_streaming_query(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
result_budget_bytes: usize,
) -> Result<RuntimePermit> {
let admission = self.db.runtime_admission_plan(cypher, parameters)?;
if admission.is_mutation {
return Err(HawDBError::Execution(
"admitted streaming query must be read-only".to_string(),
));
}
self.try_admit_runtime(admission.runtime_work_request_for_snapshot(
u64::try_from(result_budget_bytes).unwrap_or(u64::MAX),
self.runtime_governor.snapshot(),
))
}
fn try_admit_runtime(&self, request: hawdb_qos::RuntimeWorkRequest) -> Result<RuntimePermit> {
self.runtime_governor.try_admit(request).map_err(|error| {
if error.is_retryable() {
self.runtime_governor
.record_admission_wait(request, error.code, 0);
}
HawDBError::Execution(error.to_string())
})
}
fn admitted_streaming_result_bytes(&self, options: &NowledgeMemReadOptions) -> Result<usize> {
let governor_budget =
usize::try_from(self.runtime_governor.snapshot().limits.result_budget_bytes)
.unwrap_or(usize::MAX);
let configured = self
.db
.config()
.max_read_result_payload_bytes
.unwrap_or(governor_budget);
let requested = options
.max_estimated_payload_bytes
.unwrap_or(governor_budget);
let admitted = governor_budget.min(configured).min(requested);
if admitted == 0 {
return Err(HawDBError::Execution(
"admitted streaming query requires a non-zero result byte budget".to_string(),
));
}
Ok(admitted)
}
fn check_runtime_context(&self, task_context: &RuntimeTaskContext) -> Result<()> {
task_context.checkpoint().map_err(|reason| {
self.runtime_governor.record_cancellation(reason);
HawDBError::Execution(format!("runtime task {reason}"))
})
}
fn record_runtime_cancellation(
&self,
error: Option<&HawDBError>,
task_context: &RuntimeTaskContext,
) {
if error.is_some()
&& let Err(reason) = task_context.checkpoint()
{
self.runtime_governor.record_cancellation(reason);
}
}
pub fn slow_query_report(&self) -> NowledgeMemSlowQueryReport {
let config = self.db.config();
NowledgeMemSlowQueryReport::from_summaries(
self.mode,
config.slow_query_log_capacity,
config.slow_query_log_threshold_micros,
self.db.slow_query_log_snapshot(),
)
}
pub fn slow_query_report_json(&self) -> serde_json::Value {
self.slow_query_report().json()
}
pub fn read_query(&self, cypher: &str) -> Result<NowledgeMemReadOutput> {
self.read_query_with_params(cypher, &BTreeMap::new(), &NowledgeMemReadOptions::default())
}
pub fn read_query_with_options(
&self,
cypher: &str,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.read_query_with_params(cypher, &BTreeMap::new(), options)
}
pub fn read_query_with_params(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
let mut output = self
.read_query_with_params_streaming_collect(cypher, parameters, options)
.map_err(|error| legacy_nowledge_mem_read_error(error, options))?;
output.report.streaming = false;
Ok(output)
}
pub fn read_query_with_params_streaming(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.read_query_with_params_streaming_context(
cypher,
parameters,
options,
&RuntimeTaskContext::default(),
consumer,
)
}
pub fn read_query_with_params_streaming_context(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
task_context: &RuntimeTaskContext,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.check_runtime_context(task_context)?;
let max_payload_bytes = self.admitted_streaming_result_bytes(options)?;
let permit = self.admit_streaming_query(cypher, parameters, max_payload_bytes)?;
let execution_task_context = permit.bind_task_context(task_context.clone());
let result = self
.db
.begin_read_transaction()?
.query_with_params_streaming_context(
cypher,
parameters,
QueryStreamOptions {
max_rows: options.max_rows,
max_payload_bytes: Some(max_payload_bytes),
},
&execution_task_context,
consumer,
);
self.record_runtime_cancellation(result.as_ref().err(), task_context);
result
}
pub fn read_query_with_params_streaming_collect(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
let mut rows = Vec::new();
let streamed =
self.read_query_with_params_streaming(cypher, parameters, options, |row| {
rows.push(row);
Ok(())
})?;
streamed_nowledge_mem_read_output(self.mode, rows, streamed, options)
}
pub fn graph_rag_schema_context(
&self,
options: GraphRagSchemaContextOptions,
) -> Result<GraphRagSchemaContext> {
self.db.graph_rag_schema_context(options)
}
pub fn read_generated_graph_rag(
&self,
query: &GraphRagGeneratedQuery,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
let bounded = self
.db
.begin_read_transaction()?
.query_generated_graph_rag_bounded_profile(query, parameters, options.max_rows)?;
bounded_nowledge_mem_read_output(self.mode, bounded, options)
}
}
#[derive(Debug)]
pub struct NowledgeMemSearchProjection {
index: SearchIndex,
}
#[derive(Debug)]
pub struct NowledgeMemOutOfCoreSearchProjection {
reader: SearchOutOfCoreReader,
maintenance: Option<(PathBuf, NowledgeMemIncrementalSearchMaintenanceOptions)>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemOutOfCoreSearchCandidateOutput {
pub result: SearchResultSet,
pub report: NowledgeMemSearchCandidateReport,
pub metrics: SearchOutOfCoreMetrics,
}
#[derive(Debug, Clone, PartialEq)]
pub struct NowledgeMemSearchHydrationOutput {
pub documents: Vec<SearchDocument>,
pub out_of_core_metrics: Option<SearchOutOfCoreMetrics>,
}
impl From<NowledgeMemOutOfCoreSearchCandidateOutput> for NowledgeMemSearchCandidateOutput {
fn from(output: NowledgeMemOutOfCoreSearchCandidateOutput) -> Self {
Self {
result: output.result,
report: output.report,
out_of_core_metrics: Some(output.metrics),
}
}
}
impl NowledgeMemOutOfCoreSearchProjection {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
reader: SearchOutOfCoreReader::open(path)?,
maintenance: None,
})
}
pub fn open_with_config(path: impl AsRef<Path>, config: SearchOutOfCoreConfig) -> Result<Self> {
Ok(Self {
reader: SearchOutOfCoreReader::open_with_config(path, config)?,
maintenance: None,
})
}
pub fn open_production(
path: impl AsRef<Path>,
qualification: &SearchLexicalProductionQualificationReport,
expected_identity: &crate::ProductionQualificationIdentity,
) -> Result<Self> {
let projection = Self::open(path)?;
projection.validate_lexical_production_qualification(qualification, expected_identity)?;
Ok(projection)
}
pub fn open_production_with_config(
path: impl AsRef<Path>,
config: SearchOutOfCoreConfig,
qualification: &SearchLexicalProductionQualificationReport,
expected_identity: &crate::ProductionQualificationIdentity,
) -> Result<Self> {
let projection = Self::open_with_config(path, config)?;
projection.validate_lexical_production_qualification(qualification, expected_identity)?;
Ok(projection)
}
pub fn validate_lexical_production_qualification(
&self,
qualification: &SearchLexicalProductionQualificationReport,
expected_identity: &crate::ProductionQualificationIdentity,
) -> Result<()> {
qualification.validate_for_projection_and_release(
&self.reader.production_qualification_identity(),
expected_identity,
)
}
pub fn reader(&self) -> &SearchOutOfCoreReader {
&self.reader
}
pub fn freshness(&self) -> SearchProjectionFreshness {
self.reader.projection_freshness()
}
fn runtime_admission_memory_bytes(
&self,
working_memory_bytes: u64,
result_budget_bytes: u64,
) -> u64 {
let config = self.reader.config();
working_memory_bytes
.saturating_add(config.max_vector_search_working_bytes.get() as u64)
.saturating_add(config.max_uncompressed_segment_bytes.get())
.saturating_add(config.max_candidate_block_bytes.get())
.saturating_add(config.max_hydrated_bytes.get().min(result_budget_bytes))
.saturating_add(config.max_matched_span_bytes.get())
}
pub fn hydrate_documents(
&self,
document_ids: &[String],
) -> Result<SearchOutOfCoreHydrationOutput> {
self.reader.hydrate_documents(document_ids)
}
pub fn search_candidates(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<SearchResultSet> {
Ok(self.search_candidates_with_report(request)?.result)
}
pub fn search_candidates_with_report(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<NowledgeMemOutOfCoreSearchCandidateOutput> {
self.search_candidates_with_report_context(request, VectorSearchExecutionOptions::default())
}
pub fn search_candidates_with_report_context(
&self,
request: &NowledgeMemSearchCandidateRequest,
vector_execution_options: VectorSearchExecutionOptions<'_>,
) -> Result<NowledgeMemOutOfCoreSearchCandidateOutput> {
let effective_compressed_vector_search_mode = effective_search_candidate_mode(request);
let output = self
.reader
.search_with_options_compressed_vector_projection_execution_options(
&request.query_text,
request.query_embedding.as_deref(),
request.mode,
SearchQueryOptions {
limit: request.limit,
offset: request.offset,
rank_window: request.rank_window,
fusion_weights: request.fusion_weights,
metadata_filters: request.metadata_filters.clone(),
policy_epoch: None,
},
effective_compressed_vector_search_mode,
vector_execution_options,
)?;
let report = nowledge_mem_search_candidate_report(
request,
effective_compressed_vector_search_mode,
&output.result,
);
Ok(NowledgeMemOutOfCoreSearchCandidateOutput {
result: output.result,
report,
metrics: output.metrics,
})
}
}
impl NowledgeMemSearchProjection {
pub fn open(path: impl AsRef<Path>) -> Result<Self> {
Ok(Self {
index: SearchIndex::open(path)?,
})
}
pub fn from_index(index: SearchIndex) -> Self {
Self { index }
}
pub fn set_range_read_config(&mut self, config: SearchRangeReadConfig) {
self.index.set_range_read_config(config);
}
pub fn index(&self) -> &SearchIndex {
&self.index
}
pub fn index_mut(&mut self) -> &mut SearchIndex {
&mut self.index
}
pub fn set_telemetry_sink(&mut self, telemetry: Option<Arc<dyn TelemetrySink>>) {
self.index.set_telemetry_sink(telemetry);
}
pub fn into_index(self) -> SearchIndex {
self.index
}
pub fn probe_json(&self, options: SearchProjectionProbeOptions) -> serde_json::Value {
self.index.nowledge_search_projection_probe_json(options)
}
pub fn evidence_json(&self, options: SearchProjectionProbeOptions) -> serde_json::Value {
nowledge_search_projection_evidence_json(&self.probe_json(options))
}
pub fn evidence_report(
&self,
options: SearchProjectionProbeOptions,
) -> NowledgeSearchProjectionEvidenceReport {
NowledgeSearchProjectionEvidenceReport::from_probe(&self.probe_json(options))
}
pub fn validate_sampled_vector_recall(
&self,
options: VectorRecallValidationOptions,
) -> VectorRecallValidationReport {
self.index.validate_sampled_vector_recall(options)
}
pub fn qualify_sampled_vector_recall_for_production(
&self,
options: VectorRecallValidationOptions,
evidence_binding: crate::ProductionEvidenceBinding,
expected_identity: crate::ProductionQualificationIdentity,
) -> VectorRecallProductionQualificationReport {
self.index.qualify_sampled_vector_recall_for_production(
options,
evidence_binding,
expected_identity,
)
}
pub fn shadow_evidence_json(
&self,
primary_probe: &serde_json::Value,
options: SearchProjectionProbeOptions,
) -> serde_json::Value {
nowledge_search_projection_shadow_evidence_json(primary_probe, &self.probe_json(options))
}
pub fn freshness(&self) -> SearchProjectionFreshness {
self.index.projection_freshness()
}
pub fn search_candidates(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<SearchResultSet> {
self.search_candidates_with_report(request)
.map(|output| output.result)
}
pub fn search_candidates_with_report(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<NowledgeMemSearchCandidateOutput> {
let effective_compressed_vector_search_mode = effective_search_candidate_mode(request);
let result = self.index.search_with_options_adaptive_vector_projection(
&request.query_text,
request.query_embedding.as_deref(),
request.mode,
SearchQueryOptions {
limit: request.limit,
offset: request.offset,
rank_window: request.rank_window,
fusion_weights: request.fusion_weights,
metadata_filters: request.metadata_filters.clone(),
policy_epoch: None,
},
AdaptiveVectorSearchOptions {
compression_mode: effective_compressed_vector_search_mode,
backend_policy: request.adaptive_vector_backend_policy,
recall_validation_probe: request.recall_validation_probe,
},
)?;
let report = nowledge_mem_search_candidate_report(
request,
effective_compressed_vector_search_mode,
&result,
);
Ok(NowledgeMemSearchCandidateOutput {
result,
report,
out_of_core_metrics: None,
})
}
pub fn try_search_candidates_with_report(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<NowledgeMemSearchCandidateOutput> {
self.try_search_candidates_with_report_context(
request,
VectorSearchExecutionOptions::default(),
)
}
pub fn try_search_candidates_with_report_context(
&self,
request: &NowledgeMemSearchCandidateRequest,
vector_execution_options: VectorSearchExecutionOptions<'_>,
) -> Result<NowledgeMemSearchCandidateOutput> {
let effective_compressed_vector_search_mode = effective_search_candidate_mode(request);
let result = self
.index
.try_search_with_options_adaptive_vector_projection_context(
&request.query_text,
request.query_embedding.as_deref(),
request.mode,
SearchQueryOptions {
limit: request.limit,
offset: request.offset,
rank_window: request.rank_window,
fusion_weights: request.fusion_weights,
metadata_filters: request.metadata_filters.clone(),
policy_epoch: None,
},
AdaptiveVectorSearchOptions {
compression_mode: effective_compressed_vector_search_mode,
backend_policy: request.adaptive_vector_backend_policy,
recall_validation_probe: request.recall_validation_probe,
},
vector_execution_options,
)?;
let report = nowledge_mem_search_candidate_report(
request,
effective_compressed_vector_search_mode,
&result,
);
Ok(NowledgeMemSearchCandidateOutput {
result,
report,
out_of_core_metrics: None,
})
}
pub fn search_candidate_readiness(
&self,
request: &NowledgeMemSearchCandidateRequest,
options: &NowledgeMemSearchCandidateReadinessOptions,
) -> Result<NowledgeMemSearchCandidateReadinessReport> {
self.search_candidates_with_report(request)
.map(|output| output.readiness_report(options))
}
pub fn search_candidate_shadow_evidence<I, S>(
&self,
request: &NowledgeMemSearchCandidateRequest,
primary_candidate_ids: I,
) -> Result<NowledgeMemSearchCandidateShadowEvidence>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let output = self.search_candidates_with_report(request)?;
let mut accumulator = NowledgeMemSearchCandidateShadowAccumulator::new();
accumulator.record_search_candidate_output(primary_candidate_ids, &output);
Ok(accumulator.evidence())
}
pub fn search_candidate_shadow_evidence_json<I, S>(
&self,
request: &NowledgeMemSearchCandidateRequest,
primary_candidate_ids: I,
) -> Result<serde_json::Value>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.search_candidate_shadow_evidence(request, primary_candidate_ids)
.map(|evidence| evidence.json())
}
}
struct SearchProjectionExternalReadOperator<'a> {
projection: Option<&'a NowledgeMemSearchProjection>,
out_of_core_projection: Option<&'a NowledgeMemOutOfCoreSearchProjection>,
vector_seed_execution_count: usize,
}
impl crate::executor::ExternalReadOperator for SearchProjectionExternalReadOperator<'_> {
fn execute_vector_seed(
&mut self,
request: crate::executor::VectorSeedExecutionRequest<'_>,
) -> Result<crate::executor::VectorSeedExecutionOutput> {
request.resources.checkpoint()?;
let top_k = vector_plan_top_k(request.vector_plan)?.min(request.resources.result.max_rows);
let search_request =
NowledgeMemSearchCandidateRequest::vector(request.embedding.to_vec(), top_k)
.with_metadata_filters(request.metadata_filters.clone());
let vector_execution_options = VectorSearchExecutionOptions::bounded(
request.resources.max_parallelism,
request.resources.max_working_memory_bytes.get(),
request.resources.task_context,
);
let output = match (self.projection, self.out_of_core_projection) {
(Some(projection), None) => projection.try_search_candidates_with_report_context(
&search_request,
vector_execution_options,
)?,
(None, Some(projection)) => projection
.search_candidates_with_report_context(&search_request, vector_execution_options)?
.into(),
(None, None) => return Err(missing_search_projection_error()),
(Some(_), Some(_)) => {
return Err(HawDBError::Storage(
"nowledge mem search projection ownership is ambiguous".to_string(),
));
}
};
let retriever = output
.result
.retrievers
.iter()
.find(|retriever| retriever.name == "vector")
.ok_or_else(|| {
HawDBError::Execution(
"vector search did not produce a vector retriever report".to_string(),
)
})?;
let candidate_score_source = vector_score_source(&retriever.candidate_score_source)?;
let execution_output = crate::executor::VectorSeedExecutionOutput {
rows: output
.result
.hits
.into_iter()
.map(|hit| crate::executor::VectorSeedExecutionRow {
id: hit.id,
external_id: hit.external_id,
score: hit.vector_score,
})
.collect(),
report: hawdb_executor::VectorExecutionReport {
backend: vector_execution_backend(candidate_score_source),
compression_mode: vector_compression_mode(
output.report.compressed_vector_search_mode,
),
candidate_source: vector_plan_candidate_source(request.vector_plan)?,
backend_selection_reason: retriever.backend_selection_reason,
estimated_raw_vector_bytes: retriever.estimated_raw_vector_bytes,
filter_selectivity_per_million: retriever.filter_selectivity_per_million,
candidate_score_source,
final_score_source: vector_score_source(&retriever.final_score_source)?,
generated_candidate_count: retriever.generated_candidate_count,
descriptor_pruned_count: retriever.descriptor_pruned_count,
scalar_filtered_count: retriever.scalar_filtered_count,
residual_filtered_count: retriever.residual_filtered_count,
candidate_scan_rounds: retriever.candidate_scan_rounds,
reranked_candidate_count: retriever.reranked_candidate_count,
returned_count: retriever.candidate_count,
raw_vector_bytes_read: retriever.raw_vector_bytes_read,
candidate_scan_metrics: retriever.candidate_scan_kernel.as_ref().map(|kernel| {
hawdb_executor::VectorCandidateScanMetrics {
kernel: kernel.clone(),
worker_count: retriever.candidate_scan_worker_count,
segment_count: retriever.candidate_scan_segment_count,
scanned_segment_count: retriever.candidate_scan_scanned_segment_count,
scored_document_count: retriever.candidate_scan_scored_document_count,
filtered_document_count: retriever.candidate_scan_filtered_document_count,
scanned_block_count: retriever.candidate_scan_scanned_block_count,
skipped_block_count: retriever.candidate_scan_skipped_block_count,
payload_bytes_read: retriever.candidate_scan_payload_bytes_read,
admitted_working_bytes: retriever.candidate_scan_admitted_working_bytes,
}
}),
index_covered_document_count: Some(retriever.index_covered_document_count),
index_candidate_document_count: Some(retriever.index_candidate_document_count),
index_coverage_complete: Some(retriever.index_coverage_complete),
fallback_reason_codes: retriever
.fallback_reason_codes
.iter()
.filter_map(|code| vector_fallback_reason_code(*code))
.collect(),
},
};
execution_output.validate_result_budget(request.resources.result)?;
request.resources.checkpoint()?;
self.vector_seed_execution_count = self
.vector_seed_execution_count
.checked_add(1)
.ok_or_else(|| {
HawDBError::Execution(
"bounded external vector seed execution count overflowed".to_string(),
)
})?;
Ok(execution_output)
}
}
fn vector_plan_top_k(plan: &hawdb_plan_cypher::VectorPhysicalPlan) -> Result<usize> {
match plan {
hawdb_plan_cypher::VectorPhysicalPlan::TopK { limit, .. } => Ok(*limit),
_ => Err(HawDBError::Execution(
"vector seed physical plan is missing TopK".to_string(),
)),
}
}
fn vector_plan_candidate_source(
plan: &hawdb_plan_cypher::VectorPhysicalPlan,
) -> Result<hawdb_plan_cypher::VectorCandidateSource> {
match plan {
hawdb_plan_cypher::VectorPhysicalPlan::VectorCandidateScan { source, .. } => Ok(*source),
hawdb_plan_cypher::VectorPhysicalPlan::ResidualFilter { input, .. }
| hawdb_plan_cypher::VectorPhysicalPlan::RawVectorRerank { input, .. }
| hawdb_plan_cypher::VectorPhysicalPlan::TopK { input, .. } => {
vector_plan_candidate_source(input)
}
hawdb_plan_cypher::VectorPhysicalPlan::Filter { .. } => Err(HawDBError::Execution(
"vector seed physical plan is missing VectorCandidateScan".to_string(),
)),
}
}
fn vector_score_source(value: &str) -> Result<hawdb_executor::VectorScoreSource> {
match value {
"unavailable" | "none" => Ok(hawdb_executor::VectorScoreSource::Unavailable),
"raw_vector" => Ok(hawdb_executor::VectorScoreSource::RawVector),
"ann_approximate" => Ok(hawdb_executor::VectorScoreSource::AnnApproximate),
"quantized_approximate" => Ok(hawdb_executor::VectorScoreSource::QuantizedApproximate),
_ => Err(HawDBError::Execution(
"vector search returned an unsupported score source".to_string(),
)),
}
}
fn vector_execution_backend(
score_source: hawdb_executor::VectorScoreSource,
) -> hawdb_executor::VectorExecutionBackend {
match score_source {
hawdb_executor::VectorScoreSource::Unavailable => {
hawdb_executor::VectorExecutionBackend::Unavailable
}
hawdb_executor::VectorScoreSource::RawVector => {
hawdb_executor::VectorExecutionBackend::ScalarFlat
}
hawdb_executor::VectorScoreSource::AnnApproximate => {
hawdb_executor::VectorExecutionBackend::AnnProjection
}
hawdb_executor::VectorScoreSource::QuantizedApproximate => {
hawdb_executor::VectorExecutionBackend::QuantizedProjection
}
}
}
fn vector_compression_mode(
mode: CompressedVectorSearchMode,
) -> hawdb_executor::VectorCompressionMode {
match mode {
CompressedVectorSearchMode::Disabled => hawdb_executor::VectorCompressionMode::Disabled,
CompressedVectorSearchMode::Preferred => hawdb_executor::VectorCompressionMode::Preferred,
CompressedVectorSearchMode::Required => hawdb_executor::VectorCompressionMode::Required,
}
}
fn vector_fallback_reason_code(
code: SearchFallbackReasonCode,
) -> Option<hawdb_executor::VectorFallbackReasonCode> {
match code {
SearchFallbackReasonCode::VectorDimensionMismatch => {
Some(hawdb_executor::VectorFallbackReasonCode::VectorDimensionMismatch)
}
SearchFallbackReasonCode::VectorIndexEmpty => {
Some(hawdb_executor::VectorFallbackReasonCode::VectorIndexEmpty)
}
SearchFallbackReasonCode::CompressedVectorProjectionUnavailable => {
Some(hawdb_executor::VectorFallbackReasonCode::CompressedVectorProjectionUnavailable)
}
SearchFallbackReasonCode::CompressedVectorBudgetExceeded => {
Some(hawdb_executor::VectorFallbackReasonCode::CompressedVectorBudgetExceeded)
}
SearchFallbackReasonCode::QueryEmbeddingMissing => {
Some(hawdb_executor::VectorFallbackReasonCode::QueryEmbeddingMissing)
}
SearchFallbackReasonCode::TextQueryEmpty => None,
}
}
#[derive(Debug)]
pub struct NowledgeMemEmbeddedStore {
graph: NowledgeMemGraph,
search_projection: Option<NowledgeMemSearchProjection>,
out_of_core_search_projection: Option<NowledgeMemOutOfCoreSearchProjection>,
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor,
}
#[derive(Debug, Clone)]
pub struct NowledgeMemEmbeddedStoreHandle {
inner: Arc<RwLock<NowledgeMemEmbeddedStore>>,
published_read: Arc<Mutex<Option<PublishedCanonicalRead>>>,
read_publication_failed: Arc<AtomicBool>,
}
#[derive(Debug)]
struct PublishedCanonicalRead {
snapshot: crate::api::DatabaseReadSnapshot,
governor: RuntimeGovernor,
}
impl PublishedCanonicalRead {
fn capture(store: &NowledgeMemEmbeddedStore) -> Result<Self> {
Ok(Self {
snapshot: store.graph.database().read_snapshot()?,
governor: store.graph.runtime_governor().clone(),
})
}
}
struct EmbeddedStoreWriteGuard<'a> {
store: RwLockWriteGuard<'a, NowledgeMemEmbeddedStore>,
published: &'a Mutex<Option<PublishedCanonicalRead>>,
read_publication_failed: &'a AtomicBool,
}
impl std::ops::Deref for EmbeddedStoreWriteGuard<'_> {
type Target = NowledgeMemEmbeddedStore;
fn deref(&self) -> &Self::Target {
&self.store
}
}
impl std::ops::DerefMut for EmbeddedStoreWriteGuard<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.store
}
}
impl Drop for EmbeddedStoreWriteGuard<'_> {
fn drop(&mut self) {
if std::thread::panicking() {
self.read_publication_failed.store(true, Ordering::Release);
return;
}
if self.read_publication_failed.load(Ordering::Acquire) {
return;
}
let next = match PublishedCanonicalRead::capture(&self.store) {
Ok(next) => next,
Err(_) => {
self.read_publication_failed.store(true, Ordering::Release);
return;
}
};
let previous = self
.published
.lock()
.unwrap_or_else(|error| error.into_inner())
.replace(next);
drop(previous);
}
}
pub struct NowledgeMemReadSnapshot<'a> {
transaction: crate::DatabaseReadTransaction,
external: SearchProjectionExternalReadOperator<'a>,
search_projection_source_graph_commit_epoch: Option<u64>,
search_projection_durable_source_graph_commit_epoch: Option<u64>,
budget: NowledgeMemReadSnapshotBudget,
cypher_statement_count: usize,
sql_statement_count: usize,
output_rows: usize,
output_payload_bytes: usize,
}
impl<'a> NowledgeMemReadSnapshot<'a> {
fn new(
transaction: crate::DatabaseReadTransaction,
budget: NowledgeMemReadSnapshotBudget,
projection: Option<&'a NowledgeMemSearchProjection>,
out_of_core_projection: Option<&'a NowledgeMemOutOfCoreSearchProjection>,
) -> Self {
let freshness = match (projection, out_of_core_projection) {
(Some(projection), None) => Some(projection.freshness()),
(None, Some(projection)) => Some(projection.freshness()),
(None, None) | (Some(_), Some(_)) => None,
};
Self {
transaction,
external: SearchProjectionExternalReadOperator {
projection,
out_of_core_projection,
vector_seed_execution_count: 0,
},
search_projection_source_graph_commit_epoch: freshness
.as_ref()
.and_then(|value| value.source_graph_commit_epoch),
search_projection_durable_source_graph_commit_epoch: freshness
.as_ref()
.and_then(|value| value.durable_source_graph_commit_epoch),
budget,
cypher_statement_count: 0,
sql_statement_count: 0,
output_rows: 0,
output_payload_bytes: 0,
}
}
pub fn commit_epoch(&self) -> u64 {
self.transaction.commit_epoch()
}
pub fn query_cypher(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
max_rows: usize,
) -> Result<QueryOutput> {
Ok(self
.query_cypher_profiled(cypher, parameters, max_rows)?
.output)
}
pub fn query_cypher_profiled(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
max_rows: usize,
) -> Result<BoundedReadQueryOutput> {
self.query_request_profiled(
crate::QueryRequest::new(cypher).with_params(parameters),
max_rows,
)
}
pub fn query_request(
&mut self,
request: crate::QueryRequest<'_>,
max_rows: usize,
) -> Result<QueryOutput> {
Ok(self.query_request_profiled(request, max_rows)?.output)
}
pub fn query_request_profiled(
&mut self,
request: crate::QueryRequest<'_>,
max_rows: usize,
) -> Result<BoundedReadQueryOutput> {
let completed_statement_count =
self.cypher_statement_count.checked_add(1).ok_or_else(|| {
HawDBError::Execution(
"bounded read snapshot Cypher statement count overflowed".to_string(),
)
})?;
let max_rows = self.statement_row_budget(max_rows)?;
let max_payload_bytes = self.remaining_payload_bytes()?;
let mut rows = Vec::new();
let report = self.transaction.query_request_streaming_with_external(
request.with_restrictive_output_limits(QueryStreamOptions {
max_rows: Some(max_rows),
max_payload_bytes: Some(max_payload_bytes),
}),
&mut self.external,
|row| {
rows.push(row);
Ok(())
},
)?;
self.consume(report.output_rows, report.output_payload_bytes)?;
self.cypher_statement_count = completed_statement_count;
Ok(BoundedReadQueryOutput {
output: QueryOutput { rows: rows.into() },
execution_profile: report.execution_profile,
})
}
pub fn query_sql(
&mut self,
sql: &str,
parameters: &[Value],
max_rows: usize,
) -> Result<QueryOutput> {
let completed_statement_count =
self.sql_statement_count.checked_add(1).ok_or_else(|| {
HawDBError::Execution(
"bounded read snapshot SQL statement count overflowed".to_string(),
)
})?;
let max_rows = self.statement_row_budget(max_rows)?;
let max_payload_bytes = self.remaining_payload_bytes()?;
let output = self.transaction.query_sql_with_params_options(
sql,
parameters,
QueryStreamOptions {
max_rows: Some(max_rows),
max_payload_bytes: Some(max_payload_bytes),
},
)?;
let output_rows = output.rows.len();
let output_payload_bytes = estimate_query_output_payload_bytes(&output);
self.consume(output_rows, output_payload_bytes)?;
self.sql_statement_count = completed_statement_count;
Ok(output)
}
pub fn report(&self) -> NowledgeMemReadSnapshotReport {
NowledgeMemReadSnapshotReport {
commit_epoch: self.commit_epoch(),
search_projection_present: self.external.projection.is_some()
|| self.external.out_of_core_projection.is_some(),
search_projection_source_graph_commit_epoch: self
.search_projection_source_graph_commit_epoch,
search_projection_durable_source_graph_commit_epoch: self
.search_projection_durable_source_graph_commit_epoch,
max_rows: self.budget.max_rows,
max_payload_bytes: self.budget.max_payload_bytes,
cypher_statement_count: self.cypher_statement_count,
sql_statement_count: self.sql_statement_count,
vector_seed_execution_count: self.external.vector_seed_execution_count,
output_rows: self.output_rows,
output_payload_bytes: self.output_payload_bytes,
remaining_rows: self.budget.max_rows.saturating_sub(self.output_rows),
remaining_payload_bytes: self
.budget
.max_payload_bytes
.saturating_sub(self.output_payload_bytes),
}
}
fn statement_row_budget(&self, requested: usize) -> Result<usize> {
if requested == 0 {
return Err(HawDBError::Execution(
"bounded read statement requires max_rows greater than zero".to_string(),
));
}
let remaining = self.budget.max_rows.saturating_sub(self.output_rows);
if remaining == 0 {
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::Rows,
self.budget.max_rows,
"bounded read snapshot exhausted max_rows",
));
}
Ok(requested.min(remaining))
}
fn remaining_payload_bytes(&self) -> Result<usize> {
let remaining = self
.budget
.max_payload_bytes
.saturating_sub(self.output_payload_bytes);
if remaining == 0 {
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::PayloadBytes,
self.budget.max_payload_bytes,
"bounded read snapshot exhausted max_payload_bytes",
));
}
Ok(remaining)
}
fn consume(&mut self, rows: usize, payload_bytes: usize) -> Result<()> {
let output_rows = self.output_rows.saturating_add(rows);
let output_payload_bytes = self.output_payload_bytes.saturating_add(payload_bytes);
if output_rows > self.budget.max_rows {
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::Rows,
self.budget.max_rows,
format!(
"bounded read snapshot produced {output_rows} rows, exceeding max_rows {}",
self.budget.max_rows
),
));
}
if output_payload_bytes > self.budget.max_payload_bytes {
return Err(HawDBError::read_budget_exceeded(ReadBudgetResource::PayloadBytes, self.budget.max_payload_bytes, format!(
"bounded read snapshot produced {output_payload_bytes} payload bytes, exceeding max_payload_bytes {}",
self.budget.max_payload_bytes
)));
}
self.output_rows = output_rows;
self.output_payload_bytes = output_payload_bytes;
Ok(())
}
}
impl NowledgeMemEmbeddedStoreHandle {
pub fn new(store: NowledgeMemEmbeddedStore) -> Result<Self> {
let published_read = PublishedCanonicalRead::capture(&store)?;
Ok(Self {
inner: Arc::new(RwLock::new(store)),
published_read: Arc::new(Mutex::new(Some(published_read))),
read_publication_failed: Arc::new(AtomicBool::new(false)),
})
}
pub fn open_with_options(
options: NowledgeMemOpenOptions,
) -> Result<(Self, NowledgeMemOpenReport)> {
let (store, report) = NowledgeMemEmbeddedStore::open_with_options(options)?;
Ok((Self::new(store)?, report))
}
pub fn open_with_options_and_runtime_governor(
options: NowledgeMemOpenOptions,
runtime_governor: RuntimeGovernor,
) -> Result<(Self, NowledgeMemOpenReport)> {
let (store, report) = NowledgeMemEmbeddedStore::open_with_options_and_runtime_governor(
options,
runtime_governor,
)?;
Ok((Self::new(store)?, report))
}
pub fn runtime_governor_snapshot(&self) -> Result<RuntimeGovernorSnapshot> {
Ok(self.read_store()?.runtime_governor_snapshot())
}
pub fn storage_residency_report(&self) -> Result<crate::StorageResidencyReport> {
self.read_store()?
.graph
.database()
.storage_residency_report()
}
pub fn storage_handle_poisoned(&self) -> Result<bool> {
self.read_store()?
.graph
.database()
.storage_handle_poisoned()
}
pub fn serving_path_readiness(&self) -> NowledgeMemServingPathReadiness {
NowledgeMemServingPathReadiness::embedded_store_handle()
}
pub fn refresh_runtime_resources(&self) -> Result<bool> {
Ok(self.read_store()?.refresh_runtime_resources())
}
pub fn checkpoint(&self) -> Result<()> {
let shadow_admission_bytes = self
.read_store()?
.graph
.database()
.columnar_shadow_admission_bytes()?;
let _permit = self.admit_typed_maintenance(
usize::try_from(shadow_admission_bytes).unwrap_or(usize::MAX),
1,
)?;
self.write_store()?
.graph_mut()
.database_mut()
.checkpoint_with_shadow_admission(crate::store::ColumnarShadowAdmission::pre_admitted(
shadow_admission_bytes,
))
}
pub fn query_with_report(&self, cypher: &str) -> Result<NowledgeMemQueryOutput> {
self.write_store()?.query_with_report(cypher)
}
pub fn set_telemetry_sink(&self, telemetry: Option<Arc<dyn TelemetrySink>>) -> Result<()> {
self.write_store()?.set_telemetry_sink(telemetry);
Ok(())
}
pub fn query_with_report_options(
&self,
cypher: &str,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
self.write_store()?
.query_with_report_options(cypher, options)
}
pub fn query_with_params_with_report(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
) -> Result<NowledgeMemQueryOutput> {
self.write_store()?
.query_with_params_with_report(cypher, parameters)
}
pub fn search_projection_documents(
&self,
document_ids: &[String],
max_documents: usize,
) -> Result<Vec<SearchDocument>> {
Ok(self
.search_projection_documents_with_report(document_ids, max_documents)?
.documents)
}
pub fn search_projection_documents_with_report(
&self,
document_ids: &[String],
max_documents: usize,
) -> Result<NowledgeMemSearchHydrationOutput> {
if document_ids.len() > max_documents {
return Err(HawDBError::Execution(format!(
"search projection document hydration requested {} rows, limit is {max_documents}",
document_ids.len()
)));
}
let _permit = self.admit_typed_search()?;
let store = self.read_store()?;
let max_payload_bytes = store
.graph
.database()
.config()
.max_read_result_payload_bytes
.ok_or_else(|| {
HawDBError::Execution(
"admitted search hydration requires max_read_result_payload_bytes".to_string(),
)
})?;
match (
store.search_projection.as_ref(),
store.out_of_core_search_projection.as_ref(),
) {
(Some(projection), None) => {
let mut documents = Vec::with_capacity(document_ids.len());
let mut payload_bytes = 0usize;
for id in document_ids {
let Some(document) = projection.index().document(id) else {
continue;
};
payload_bytes =
payload_bytes.saturating_add(search_document_payload_bytes(document));
if payload_bytes > max_payload_bytes {
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::PayloadBytes,
max_payload_bytes,
format!(
"search projection document hydration produced {payload_bytes} payload bytes, limit is {max_payload_bytes}"
),
));
}
documents.push(document.clone());
}
Ok(NowledgeMemSearchHydrationOutput {
documents,
out_of_core_metrics: None,
})
}
(None, Some(projection)) => {
let output = projection.hydrate_documents(document_ids)?;
if output.metrics.hydrated_bytes
> u64::try_from(max_payload_bytes).unwrap_or(u64::MAX)
{
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::PayloadBytes,
max_payload_bytes,
format!(
"search projection document hydration produced {} payload bytes, limit is {max_payload_bytes}",
output.metrics.hydrated_bytes
),
));
}
Ok(NowledgeMemSearchHydrationOutput {
documents: output.documents,
out_of_core_metrics: Some(output.metrics),
})
}
(None, None) => Err(missing_search_projection_error()),
(Some(_), Some(_)) => Err(ambiguous_search_projection_error()),
}
}
}
#[cfg(test)]
impl NowledgeMemEmbeddedStoreHandle {
pub fn create_knowledge_memory_evolves_batch(
&self,
request: &KnowledgeMemoryEvolvesCreateBatchRequest,
) -> Result<KnowledgeMemoryEvolvesCreateBatchOutput> {
let _permit = self.admit_typed_mutation()?;
self.write_store()?
.graph_mut()
.database_mut()
.create_knowledge_memory_evolves_batch(request)
}
pub fn update_knowledge_memory_lifecycle_batch(
&self,
request: &KnowledgeMemoryLifecycleBatchRequest,
) -> Result<KnowledgeMemoryLifecycleBatchOutput> {
let _permit = self.admit_typed_mutation()?;
self.write_store()?
.graph_mut()
.database_mut()
.update_knowledge_memory_lifecycle_batch(request)
}
pub fn delete_knowledge_entity_batch(
&self,
request: &KnowledgeEntityDeleteBatchRequest,
) -> Result<KnowledgeEntityDeleteBatchOutput> {
let _permit = self.admit_typed_mutation()?;
self.write_store()?
.graph_mut()
.database_mut()
.delete_knowledge_entity_batch(request)
}
}
impl NowledgeMemEmbeddedStoreHandle {
pub fn transaction(
&self,
statements: &[NowledgeGraphStatement],
) -> Result<crate::NowledgeGraphTransactionOutput> {
let permit = self.admit_transaction(statements)?;
let task_context = permit.bind_task_context(RuntimeTaskContext::default());
let _permit = permit;
let mut store = self.write_store()?;
let db = store.graph_mut().database_mut();
let mut transaction = db.begin_transaction_with_context(&task_context)?;
let mut statement_outputs = Vec::with_capacity(statements.len());
for statement in statements {
statement_outputs
.push(transaction.query_with_params(&statement.cypher, &statement.parameters)?);
}
let commit_output = transaction.commit()?;
Ok(crate::NowledgeGraphTransactionOutput {
statement_outputs,
commit_output,
})
}
pub fn with_transaction<T>(
&self,
operation: impl FnOnce(&mut crate::DatabaseTransaction<'_>) -> Result<T>,
) -> Result<T> {
let permit = self.admit_typed_transaction()?;
let task_context = permit.bind_task_context(RuntimeTaskContext::default());
let _permit = permit;
let mut store = self.write_store()?;
let mut transaction = store
.graph_mut()
.database_mut()
.begin_transaction_with_context(&task_context)?;
let result = match operation(&mut transaction) {
Ok(output) => transaction.commit().map(|_| output),
Err(error) => {
transaction.rollback();
Err(error)
}
};
store.graph.database().poison_on_storage_error(&result);
result
}
pub fn with_read_transaction<T>(
&self,
max_estimated_payload_bytes: usize,
operation: impl FnOnce(&mut crate::DatabaseReadTransaction) -> Result<T>,
) -> Result<T> {
let (_permit, mut transaction) =
self.canonical_read_transaction(max_estimated_payload_bytes, None)?;
let result = operation(&mut transaction);
self.finish_canonical_read(result)
}
pub fn with_bounded_read_snapshot<T>(
&self,
budget: NowledgeMemReadSnapshotBudget,
operation: impl FnOnce(&mut NowledgeMemReadSnapshot<'_>) -> Result<T>,
) -> Result<T> {
self.with_bounded_read_snapshot_context(budget, &RuntimeTaskContext::default(), operation)
}
pub fn with_bounded_read_snapshot_context<T>(
&self,
budget: NowledgeMemReadSnapshotBudget,
task_context: &RuntimeTaskContext,
operation: impl FnOnce(&mut NowledgeMemReadSnapshot<'_>) -> Result<T>,
) -> Result<T> {
self.with_bounded_read_snapshot_kind(budget, true, task_context, operation)
}
pub fn with_bounded_graph_read_snapshot<T>(
&self,
budget: NowledgeMemReadSnapshotBudget,
operation: impl FnOnce(&mut NowledgeMemReadSnapshot<'_>) -> Result<T>,
) -> Result<T> {
self.with_bounded_graph_read_snapshot_context(
budget,
&RuntimeTaskContext::default(),
operation,
)
}
pub fn with_bounded_graph_read_snapshot_context<T>(
&self,
budget: NowledgeMemReadSnapshotBudget,
task_context: &RuntimeTaskContext,
operation: impl FnOnce(&mut NowledgeMemReadSnapshot<'_>) -> Result<T>,
) -> Result<T> {
self.with_bounded_read_snapshot_kind(budget, false, task_context, operation)
}
fn with_bounded_read_snapshot_kind<T>(
&self,
budget: NowledgeMemReadSnapshotBudget,
pin_projection: bool,
task_context: &RuntimeTaskContext,
operation: impl FnOnce(&mut NowledgeMemReadSnapshot<'_>) -> Result<T>,
) -> Result<T> {
if budget.max_rows == 0 {
return Err(HawDBError::Execution(
"bounded read snapshot requires max_rows greater than zero".to_string(),
));
}
if budget.max_payload_bytes == 0 {
return Err(HawDBError::Execution(
"bounded read snapshot requires max_payload_bytes greater than zero".to_string(),
));
}
if !pin_projection {
let (_permit, transaction) = self.canonical_read_transaction_with_context(
budget.max_payload_bytes,
Some(budget.max_rows),
task_context,
)?;
let mut snapshot = NowledgeMemReadSnapshot::new(transaction, budget, None, None);
return self.finish_canonical_read(operation(&mut snapshot));
}
let permit = self.admit_typed_read(budget.max_payload_bytes)?;
let task_context = permit.bind_task_context(task_context.clone());
let _permit = permit;
let store = self.read_store()?;
let configured_rows = store
.graph
.database()
.config()
.max_read_result_rows
.ok_or_else(|| {
HawDBError::Execution(
"bounded read snapshot requires max_read_result_rows".to_string(),
)
})?;
if budget.max_rows > configured_rows {
return Err(HawDBError::Execution(format!(
"bounded read snapshot row budget {} exceeds configured limit {configured_rows}",
budget.max_rows
)));
}
let transaction = store
.graph
.database()
.begin_read_transaction_with_context(&task_context)?;
let mut snapshot = NowledgeMemReadSnapshot::new(
transaction,
budget,
store.search_projection.as_ref(),
store.out_of_core_search_projection.as_ref(),
);
operation(&mut snapshot)
}
pub fn hawdb_lightning_initial_import_apply_with_document_identities(
&self,
encoded_graph_stream: &str,
encoded_relational_stream: &[u8],
manifest: &HawDBLightningBootstrapManifest,
projection_freshness: Option<&SearchProjectionFreshness>,
checkpoint: Option<&HawDBLightningInitialImportCheckpoint>,
document_identities: &[HawDBLightningInitialImportDocumentIdentity],
) -> Result<HawDBLightningInitialImportApplyReport> {
let estimated_input_bytes = encoded_graph_stream
.len()
.saturating_add(encoded_relational_stream.len())
.saturating_add(
document_identities
.iter()
.map(|identity| identity.document_id.len())
.sum::<usize>(),
);
let _permit = self.admit_typed_maintenance(estimated_input_bytes, 1)?;
self.write_store()?
.graph_mut()
.database_mut()
.hawdb_lightning_initial_import_apply_with_document_identities(
encoded_graph_stream,
encoded_relational_stream,
manifest,
projection_freshness,
checkpoint,
document_identities,
)
}
pub fn apply_search_projection_delta_and_checkpoint(
&self,
delta: SearchProjectionDelta,
) -> Result<SearchProjectionDeltaReport> {
let permit =
self.admit_search_projection_maintenance(search_projection_delta_bytes(&delta))?;
let mut store = self.write_store()?;
if store.incremental_search_enabled() {
store.apply_incremental_search_delta(
delta,
permit.bind_task_context(RuntimeTaskContext::default()),
)
} else {
store.apply_search_projection_delta_and_checkpoint(delta)
}
}
pub fn apply_initial_import_projection_delta_and_checkpoint(
&self,
delta: SearchProjectionDelta,
import_source_graph_commit_epoch: u64,
) -> Result<SearchProjectionDeltaReport> {
let _permit = self.admit_typed_maintenance(search_projection_delta_bytes(&delta), 1)?;
self.write_store()?
.apply_initial_import_projection_delta_and_checkpoint(
delta,
import_source_graph_commit_epoch,
)
}
pub fn query_with_params_with_report_options(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
self.write_store()?
.query_with_params_with_report_options(cypher, parameters, options)
}
pub fn query_with_params_with_report_options_context(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
task_context: &RuntimeTaskContext,
) -> Result<NowledgeMemQueryOutput> {
self.write_store()?
.query_with_params_with_report_options_context(
cypher,
parameters,
options,
task_context,
)
}
pub fn read_query(
&self,
cypher: &str,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.read_store()?.read_query_with_options(cypher, options)
}
pub fn read_query_with_params(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.read_store()?
.read_query_with_params(cypher, parameters, options)
}
pub fn read_query_with_params_streaming(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.read_store()?
.read_query_with_params_streaming(cypher, parameters, options, consumer)
}
pub fn read_query_with_params_streaming_context(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
task_context: &RuntimeTaskContext,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.read_store()?.read_query_with_params_streaming_context(
cypher,
parameters,
options,
task_context,
consumer,
)
}
pub fn read_query_with_params_streaming_collect(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.read_store()?
.read_query_with_params_streaming_collect(cypher, parameters, options)
}
pub fn graph_rag_schema_context(
&self,
options: GraphRagSchemaContextOptions,
) -> Result<GraphRagSchemaContext> {
self.read_store()?.graph_rag_schema_context(options)
}
pub fn read_generated_graph_rag(
&self,
query: &GraphRagGeneratedQuery,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.read_store()?
.read_generated_graph_rag(query, parameters, options)
}
}
impl NowledgeMemEmbeddedStoreHandle {
pub fn search_candidates(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<SearchResultSet> {
let _permit = self.admit_typed_search()?;
Ok(self.read_store()?.search_candidates(request)?.result)
}
pub fn search_candidates_with_report(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<NowledgeMemSearchCandidateOutput> {
let _permit = self.admit_typed_search()?;
self.read_store()?.search_candidates(request)
}
pub fn validate_sampled_vector_recall(
&self,
options: VectorRecallValidationOptions,
) -> Result<VectorRecallValidationReport> {
self.read_store()?.validate_sampled_vector_recall(options)
}
pub fn qualify_sampled_vector_recall_for_production(
&self,
options: VectorRecallValidationOptions,
evidence_binding: crate::ProductionEvidenceBinding,
expected_identity: crate::ProductionQualificationIdentity,
) -> Result<VectorRecallProductionQualificationReport> {
self.read_store()?
.qualify_sampled_vector_recall_for_production(
options,
evidence_binding,
expected_identity,
)
}
pub fn retrieve_knowledge(
&self,
request: &KnowledgeRetrievalRequest,
) -> Result<KnowledgeRetrievalOutput> {
let _permit = self.admit_typed_search()?;
self.read_store()?.retrieve_knowledge(request)
}
pub fn retrieve_knowledge_with_report(
&self,
request: &KnowledgeRetrievalRequest,
) -> Result<NowledgeMemRetrievalOutput> {
let _permit = self.admit_typed_search()?;
self.read_store()?.retrieve_knowledge_with_report(request)
}
pub fn search_candidate_readiness(
&self,
request: &NowledgeMemSearchCandidateRequest,
options: &NowledgeMemSearchCandidateReadinessOptions,
) -> Result<NowledgeMemSearchCandidateReadinessReport> {
let _permit = self.admit_typed_search()?;
self.read_store()?
.search_candidate_readiness(request, options)
}
pub fn search_candidate_shadow_evidence_json<I, S>(
&self,
request: &NowledgeMemSearchCandidateRequest,
primary_candidate_ids: I,
) -> Result<serde_json::Value>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let _permit = self.admit_typed_search()?;
self.read_store()?
.search_candidate_shadow_evidence_json(request, primary_candidate_ids)
}
pub fn slow_query_report(&self) -> Result<NowledgeMemSlowQueryReport> {
Ok(self.read_store()?.slow_query_report())
}
pub fn slow_query_report_json(&self) -> Result<serde_json::Value> {
Ok(self.read_store()?.slow_query_report_json())
}
pub fn runtime_status(&self) -> Result<NowledgeMemRuntimeStatus> {
self.read_store()?.runtime_status()
}
pub fn production_resource_profile(
&self,
statement: &NowledgeGraphStatement,
limits: StorageResourceProfileLimits,
evidence_binding: crate::ProductionEvidenceBinding,
expected_identity: crate::ProductionQualificationIdentity,
) -> Result<StorageResourceProfileReport> {
let store = self.read_store()?;
let permit = store.graph.admit_streaming_query(
&statement.cypher,
&statement.parameters,
limits.max_output_payload_bytes,
)?;
let task_context = permit.bind_task_context(RuntimeTaskContext::default());
store
.graph
.database()
.storage_resource_profile_for_production_with_context(
&statement.cypher,
&statement.parameters,
limits,
evidence_binding,
expected_identity,
&task_context,
)
}
pub fn production_status(
&self,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<NowledgeMemProductionStatus> {
self.read_store()?.production_status(route_ownership)
}
pub fn production_status_json(
&self,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<serde_json::Value> {
self.read_store()?.production_status_json(route_ownership)
}
pub fn cutover_controls_report(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
self.read_store()?
.cutover_controls_report(controls, route_ownership)
}
pub fn cutover_controls_report_with_initial_import_cutover_catch_up(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_cutover_catch_up: Option<&HawDBLightningInitialImportCutoverCatchUpReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
self.read_store()?
.cutover_controls_report_with_initial_import_cutover_catch_up(
controls,
route_ownership,
initial_import_cutover_catch_up,
)
}
pub fn cutover_controls_report_with_initial_import_recovery(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_recovery: Option<&HawDBLightningInitialImportRecoveryReadinessReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
self.read_store()?
.cutover_controls_report_with_initial_import_recovery(
controls,
route_ownership,
initial_import_recovery,
)
}
pub fn cutover_controls_report_json(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<serde_json::Value> {
self.read_store()?
.cutover_controls_report_json(controls, route_ownership)
}
pub fn cutover_controls_report_json_with_initial_import_cutover_catch_up(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_cutover_catch_up: Option<&HawDBLightningInitialImportCutoverCatchUpReport>,
) -> Result<serde_json::Value> {
self.read_store()?
.cutover_controls_report_json_with_initial_import_cutover_catch_up(
controls,
route_ownership,
initial_import_cutover_catch_up,
)
}
pub fn library_readiness(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemLibraryReadinessReport> {
self.read_store()?.library_readiness(options)
}
pub fn library_readiness_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
self.read_store()?.library_readiness_json(options)
}
pub fn readiness_dashboard(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemReadinessDashboard> {
self.read_store()?.readiness_dashboard(options)
}
pub fn readiness_dashboard_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
self.read_store()?.readiness_dashboard_json(options)
}
pub fn operations_readiness(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemOperationsReadinessReport> {
self.read_store()?.operations_readiness(options)
}
pub fn operations_readiness_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
self.read_store()?.operations_readiness_json(options)
}
pub fn query_runtime_preflight(
&self,
probes: &[NowledgeQueryRuntimePreflightProbe],
) -> Result<NowledgeQueryRuntimePreflightReport> {
Ok(self.write_store()?.query_runtime_preflight(probes))
}
pub fn query_runtime_preflight_json(
&self,
probes: &[NowledgeQueryRuntimePreflightProbe],
) -> Result<serde_json::Value> {
Ok(self.write_store()?.query_runtime_preflight_json(probes))
}
pub fn catch_up_search_projection(
&self,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<SearchProjectionCatchUpReport> {
let estimated_operations = max_operations_per_batch.saturating_mul(max_batches);
let estimated_input_bytes =
estimated_operations.saturating_mul(SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES);
let _permit = self.admit_typed_maintenance(estimated_input_bytes, 1)?;
self.write_store()?
.catch_up_search_projection(max_operations_per_batch, max_batches)
}
pub fn catch_up_search_projection_with_relational<F>(
&self,
max_operations_per_batch: usize,
max_batches: usize,
relational_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
let estimated_operations = max_operations_per_batch.saturating_mul(max_batches);
let estimated_input_bytes =
estimated_operations.saturating_mul(SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES);
let _permit = self.admit_typed_maintenance(estimated_input_bytes, 1)?;
self.write_store()?
.catch_up_search_projection_with_relational(
max_operations_per_batch,
max_batches,
relational_hydrator,
)
}
pub fn catch_up_search_projection_with_batch_hydrator<F>(
&self,
max_change_operations_per_batch: usize,
max_projection_operations_per_batch: usize,
max_batches: usize,
batch_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
let estimated_operations = max_change_operations_per_batch
.max(max_projection_operations_per_batch)
.saturating_mul(max_batches);
let estimated_input_bytes =
estimated_operations.saturating_mul(SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES);
let permit = self.admit_search_projection_maintenance(estimated_input_bytes)?;
let mut store = self.write_store()?;
if store.incremental_search_enabled() {
store.catch_up_incremental_search_with_batch_hydrator(
max_change_operations_per_batch,
max_projection_operations_per_batch,
max_batches,
batch_hydrator,
permit.bind_task_context(RuntimeTaskContext::default()),
)
} else {
store.catch_up_search_projection_with_batch_hydrator(
max_change_operations_per_batch,
max_projection_operations_per_batch,
max_batches,
batch_hydrator,
)
}
}
pub fn search_projection_changefeed_readiness(
&self,
require_restart_recoverable: bool,
max_operations: Option<usize>,
) -> Result<SearchProjectionChangefeedReadiness> {
self.read_store()?
.search_projection_changefeed_readiness(require_restart_recoverable, max_operations)
}
pub fn catch_up_search_projection_scheduled(
&self,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<ScheduledSearchProjectionCatchUpReport> {
let estimated_operations = max_operations_per_batch.saturating_mul(max_batches);
let estimated_input_bytes =
estimated_operations.saturating_mul(SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES);
let _permit = self.admit_typed_maintenance(estimated_input_bytes, 1)?;
self.write_store()?
.catch_up_search_projection_scheduled(max_operations_per_batch, max_batches)
}
#[cfg(test)]
fn admit_typed_mutation(&self) -> Result<RuntimePermit> {
let store = self.read_store()?;
let config = store.graph.database().config();
let estimated_memory_bytes = crate::executor::estimated_mutation_memory_bytes(
config.mutation_limits,
config.max_wal_record_bytes,
);
store
.graph
.try_admit_runtime(RuntimeWorkRequest::foreground_mutation(
estimated_memory_bytes,
))
}
fn admit_typed_transaction(&self) -> Result<RuntimePermit> {
let store = self.read_store()?;
let config = store.graph.database().config();
let memory_bytes = crate::executor::estimated_mutation_memory_bytes(
config.mutation_limits,
config.max_wal_record_bytes,
)
.max(u64::try_from(config.execution_memory.query_memory_bytes.get()).unwrap_or(u64::MAX));
let result_bytes = u64::try_from(
config
.max_read_result_payload_bytes
.unwrap_or(config.mutation_limits.max_result_payload_bytes.get()),
)
.unwrap_or(u64::MAX);
store.graph.try_admit_runtime(
RuntimeWorkRequest::new(RuntimeWorkPriority::Foreground, RuntimeWorkKind::Mutation)
.with_cpu_slots(1)
.with_memory_bytes(memory_bytes)
.with_result_bytes(result_bytes)
.with_blocking(true),
)
}
fn admit_typed_read(&self, max_estimated_payload_bytes: usize) -> Result<RuntimePermit> {
let store = self.read_store()?;
let request =
typed_read_request(store.graph.database().config(), max_estimated_payload_bytes)?;
store.graph.try_admit_runtime(request)
}
fn lock_published_read(&self) -> Result<MutexGuard<'_, Option<PublishedCanonicalRead>>> {
if self.read_publication_failed.load(Ordering::Acquire) {
return Err(HawDBError::Execution(
"nowledge mem embedded store read lock poisoned".to_string(),
));
}
self.published_read.lock().map_err(|_| {
HawDBError::Execution("embedded snapshot publication lock poisoned".to_string())
})
}
fn canonical_read_transaction(
&self,
max_estimated_payload_bytes: usize,
max_rows: Option<usize>,
) -> Result<(RuntimePermit, crate::DatabaseReadTransaction)> {
self.canonical_read_transaction_with_context(
max_estimated_payload_bytes,
max_rows,
&RuntimeTaskContext::default(),
)
}
fn canonical_read_transaction_with_context(
&self,
max_estimated_payload_bytes: usize,
max_rows: Option<usize>,
task_context: &RuntimeTaskContext,
) -> Result<(RuntimePermit, crate::DatabaseReadTransaction)> {
let (governor, request) = {
let published = self.lock_published_read()?;
let published = published.as_ref().ok_or_else(|| {
HawDBError::Execution("nowledge mem embedded store read lock poisoned".to_string())
})?;
published.snapshot.ensure_usable()?;
(
published.governor.clone(),
typed_read_request(published.snapshot.config(), max_estimated_payload_bytes)?,
)
};
let permit = governor.try_admit(request).map_err(|error| {
if error.is_retryable() {
governor.record_admission_wait(request, error.code, 0);
}
HawDBError::Execution(error.to_string())
})?;
let published = self.lock_published_read()?;
let published = published.as_ref().ok_or_else(|| {
HawDBError::Execution("nowledge mem embedded store read lock poisoned".to_string())
})?;
published.snapshot.ensure_usable()?;
let config = published.snapshot.config();
if let Some(max_rows) = max_rows {
let configured_rows = config.max_read_result_rows.ok_or_else(|| {
HawDBError::Execution(
"bounded read snapshot requires max_read_result_rows".to_string(),
)
})?;
if max_rows > configured_rows {
return Err(HawDBError::Execution(format!(
"bounded read snapshot row budget {max_rows} exceeds configured limit {configured_rows}"
)));
}
}
let task_context = permit.bind_task_context(task_context.clone());
let transaction = published.snapshot.begin_read_transaction(&task_context)?;
Ok((permit, transaction))
}
fn finish_canonical_read<T>(&self, result: Result<T>) -> Result<T> {
if result.is_ok() {
let published = self.lock_published_read()?;
let published = published.as_ref().ok_or_else(|| {
HawDBError::Execution("nowledge mem embedded store read lock poisoned".to_string())
})?;
published.snapshot.ensure_usable()?;
}
result
}
fn admit_typed_maintenance(
&self,
estimated_input_bytes: usize,
io_slots: usize,
) -> Result<RuntimePermit> {
let store = self.read_store()?;
let config = store.graph.database().config();
let working_memory_bytes =
u64::try_from(config.execution_memory.blocking_operator_bytes.get())
.unwrap_or(u64::MAX);
let estimated_input_bytes = u64::try_from(estimated_input_bytes).unwrap_or(u64::MAX);
store.graph.try_admit_runtime(
RuntimeWorkRequest::background_maintenance(
working_memory_bytes.saturating_add(estimated_input_bytes),
)
.with_io_slots(io_slots)
.with_blocking(true),
)
}
fn admit_typed_search(&self) -> Result<RuntimePermit> {
let store = self.read_store()?;
let config = store.graph.database().config();
let result_bytes = config
.max_read_result_payload_bytes
.ok_or_else(|| {
HawDBError::Execution(
"admitted typed search requires max_read_result_payload_bytes".to_string(),
)
})
.and_then(|bytes| {
u64::try_from(bytes).map_err(|_| {
HawDBError::Execution(
"admitted typed search result budget exceeds u64".to_string(),
)
})
})?;
let working_memory_bytes =
u64::try_from(config.execution_memory.blocking_operator_bytes.get())
.unwrap_or(u64::MAX);
let memory_bytes = store.search_memory_bytes(working_memory_bytes, result_bytes)?;
let io_slots = store.search_io_slots()?;
store.graph.try_admit_runtime(
RuntimeWorkRequest::foreground_query(memory_bytes, result_bytes)
.with_io_slots(io_slots)
.with_blocking(true),
)
}
fn admit_transaction(&self, statements: &[NowledgeGraphStatement]) -> Result<RuntimePermit> {
let store = self.read_store()?;
let db = store.graph.database();
let config = db.config();
let mut priority = RuntimeWorkPriority::Background;
let mut kind = RuntimeWorkKind::Query;
let mut estimated_memory_bytes = TYPED_CONTROL_STATEMENT_MEMORY_BYTES;
let mut result_bytes = 0u64;
let mut io_slots = 0usize;
for statement in statements {
let admission = db.runtime_admission_plan(&statement.cypher, &statement.parameters)?;
if admission.work_request.priority == WorkPriority::Foreground {
priority = RuntimeWorkPriority::Foreground;
}
if admission.is_mutation {
kind = RuntimeWorkKind::Mutation;
}
estimated_memory_bytes = estimated_memory_bytes.max(admission.estimated_memory_bytes);
io_slots = io_slots.max(admission.required_io_slots);
let statement_result_bytes = if admission.is_mutation {
config.mutation_limits.max_result_payload_bytes.get()
} else {
config.max_read_result_payload_bytes.ok_or_else(|| {
HawDBError::Execution(
"admitted transaction requires max_read_result_payload_bytes".to_string(),
)
})?
};
result_bytes = result_bytes
.saturating_add(u64::try_from(statement_result_bytes).unwrap_or(u64::MAX));
}
let request = RuntimeWorkRequest::new(priority, kind)
.with_cpu_slots(1)
.with_memory_bytes(estimated_memory_bytes)
.with_io_slots(io_slots)
.with_result_bytes(result_bytes)
.with_blocking(true);
store.graph.try_admit_runtime(request)
}
fn read_store(&self) -> Result<RwLockReadGuard<'_, NowledgeMemEmbeddedStore>> {
self.inner.read().map_err(|_| {
HawDBError::Execution("nowledge mem embedded store read lock poisoned".to_string())
})
}
fn write_store(&self) -> Result<EmbeddedStoreWriteGuard<'_>> {
let store = self.inner.write().map_err(|_| {
HawDBError::Execution("nowledge mem embedded store write lock poisoned".to_string())
})?;
Ok(EmbeddedStoreWriteGuard {
store,
published: &self.published_read,
read_publication_failed: &self.read_publication_failed,
})
}
}
fn typed_read_request(
config: &DatabaseConfig,
max_estimated_payload_bytes: usize,
) -> Result<RuntimeWorkRequest> {
let configured_result_bytes = config.max_read_result_payload_bytes.ok_or_else(|| {
HawDBError::Execution(
"admitted typed read requires max_read_result_payload_bytes".to_string(),
)
})?;
if max_estimated_payload_bytes > configured_result_bytes {
return Err(HawDBError::Execution(format!(
"typed read payload budget {max_estimated_payload_bytes} exceeds configured limit {configured_result_bytes}"
)));
}
let result_bytes = u64::try_from(configured_result_bytes).unwrap_or(u64::MAX);
let working_memory_bytes =
u64::try_from(config.execution_memory.query_memory_bytes.get()).unwrap_or(u64::MAX);
Ok(
RuntimeWorkRequest::foreground_query(working_memory_bytes, result_bytes)
.with_io_slots(1)
.with_blocking(true),
)
}
fn search_projection_delta_bytes(delta: &SearchProjectionDelta) -> usize {
let upsert_bytes = delta.upserts.iter().fold(0usize, |total, row| {
let embedding_bytes = row.embedding.as_ref().map_or(0, |embedding| {
embedding.len().saturating_mul(size_of::<f32>())
});
let metadata_bytes = row
.metadata
.iter()
.fold(0usize, |metadata_total, (key, value)| {
metadata_total
.saturating_add(key.len())
.saturating_add(value.len())
});
total
.saturating_add(size_of::<crate::SearchProjectionRow>())
.saturating_add(row.external_id.len())
.saturating_add(row.title.len())
.saturating_add(row.body.len())
.saturating_add(row.source_id.as_ref().map_or(0, String::len))
.saturating_add(embedding_bytes)
.saturating_add(metadata_bytes)
});
delta.deletes.iter().fold(upsert_bytes, |total, id| {
total
.saturating_add(size_of::<String>())
.saturating_add(id.len())
})
}
fn search_document_payload_bytes(document: &SearchDocument) -> usize {
let embedding_bytes = document.embedding.as_ref().map_or(0, |embedding| {
embedding.len().saturating_mul(size_of::<f32>())
});
let metadata_bytes = document
.metadata
.iter()
.fold(0usize, |total, (key, value)| {
total.saturating_add(key.len()).saturating_add(value.len())
});
size_of::<SearchDocument>()
.saturating_add(document.id.len())
.saturating_add(document.title.len())
.saturating_add(document.content.len())
.saturating_add(embedding_bytes)
.saturating_add(metadata_bytes)
}
impl NowledgeMemEmbeddedStore {
pub fn new(
graph: NowledgeMemGraph,
mut search_projection: Option<NowledgeMemSearchProjection>,
) -> Self {
if let Some(projection) = search_projection.as_mut() {
let mut range_read_config = projection.index().range_read_config();
range_read_config.io_depth = range_read_config
.io_depth
.min(graph.runtime_governor_snapshot().limits.foreground_io_depth);
projection.set_range_read_config(range_read_config);
}
Self {
graph,
search_projection,
out_of_core_search_projection: None,
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor::default(),
}
}
fn new_with_out_of_core_search(
graph: NowledgeMemGraph,
out_of_core_search_projection: NowledgeMemOutOfCoreSearchProjection,
retrieval_projection_advisor: NowledgeMemRetrievalProjectionAdvisor,
) -> Self {
Self {
graph,
search_projection: None,
out_of_core_search_projection: Some(out_of_core_search_projection),
retrieval_projection_advisor,
}
}
pub fn open_with_options(
options: NowledgeMemOpenOptions,
) -> Result<(Self, NowledgeMemOpenReport)> {
let graph_config = options.effective_database_config();
let runtime_governor =
default_nowledge_mem_runtime_governor(&options.graph_path, &graph_config);
Self::open_with_options_and_runtime_governor(options, runtime_governor)
}
pub fn open_with_options_and_runtime_governor(
options: NowledgeMemOpenOptions,
runtime_governor: RuntimeGovernor,
) -> Result<(Self, NowledgeMemOpenReport)> {
options.validate()?;
let mut report = options.sanitized_report();
let graph_config = options.effective_database_config();
let mut graph = NowledgeMemGraph::open_with_config_and_runtime_governor(
&options.graph_path,
graph_config,
runtime_governor,
)?;
for registry in &options.system_schema_registries {
report.system_schema_upgrades.push(
graph
.database_mut()
.apply_system_schema_registry(registry)?,
);
}
report.graph_opened = true;
let default_search_range_read_config = SearchRangeReadConfig {
io_depth: graph.runtime_governor_snapshot().limits.foreground_io_depth,
..SearchRangeReadConfig::default()
};
let Some(path) = options.search_projection_path.as_ref() else {
return Ok((Self::new(graph, None), report));
};
match &options.search_projection_open_mode {
NowledgeMemSearchProjectionOpenMode::FullResidencyMaintenance => {
let mut projection = NowledgeMemSearchProjection::open(path)?;
projection.set_range_read_config(
options
.search_range_read_config
.unwrap_or(default_search_range_read_config),
);
report.search_projection_opened = true;
Ok((Self::new(graph, Some(projection)), report))
}
NowledgeMemSearchProjectionOpenMode::IncrementalMaintenance(maintenance) => {
let projection = NowledgeMemOutOfCoreSearchProjection::open_incremental(
path,
(**maintenance).clone(),
)?;
report.search_projection_opened = true;
Ok((
Self::new_with_out_of_core_search(
graph,
projection,
options.retrieval_projection_advisor.clone(),
),
report,
))
}
NowledgeMemSearchProjectionOpenMode::QualifiedOutOfCore(qualified) => {
let graph_commit_epoch = graph.database().commit_epoch()?;
if graph_commit_epoch != qualified.expected_identity.canonical_graph_commit_epoch {
return Err(HawDBError::Storage(format!(
"qualified out-of-core search expected canonical graph commit epoch {}, opened graph is at {graph_commit_epoch}",
qualified.expected_identity.canonical_graph_commit_epoch
)));
}
let projection = NowledgeMemOutOfCoreSearchProjection::open_production_with_config(
path,
qualified.config.clone(),
&qualified.qualification,
&qualified.expected_identity,
)?;
report.search_projection_opened = true;
report.search_production_qualification_bound = true;
Ok((
Self::new_with_out_of_core_search(
graph,
projection,
options.retrieval_projection_advisor.clone(),
),
report,
))
}
}
}
pub fn graph(&self) -> &NowledgeMemGraph {
&self.graph
}
pub fn graph_mut(&mut self) -> &mut NowledgeMemGraph {
&mut self.graph
}
pub fn runtime_governor_snapshot(&self) -> RuntimeGovernorSnapshot {
self.graph.runtime_governor_snapshot()
}
pub fn refresh_runtime_resources(&self) -> bool {
self.graph.refresh_runtime_resources()
}
pub fn search_projection(&self) -> Option<&NowledgeMemSearchProjection> {
self.search_projection.as_ref()
}
pub fn search_projection_mut(&mut self) -> Option<&mut NowledgeMemSearchProjection> {
self.search_projection.as_mut()
}
pub fn out_of_core_search_projection(&self) -> Option<&NowledgeMemOutOfCoreSearchProjection> {
self.out_of_core_search_projection.as_ref()
}
pub fn apply_search_projection_delta_and_checkpoint(
&mut self,
delta: SearchProjectionDelta,
) -> Result<SearchProjectionDeltaReport> {
if self.incremental_search_enabled() {
let permit =
self.admit_incremental_search_maintenance(search_projection_delta_bytes(&delta))?;
return self.apply_incremental_search_delta(
delta,
permit.bind_task_context(RuntimeTaskContext::default()),
);
}
let projection = require_search_projection_mut(&mut self.search_projection)?;
let report = projection.index_mut().apply_projection_delta(delta)?;
projection.index().checkpoint()?;
Ok(report)
}
pub fn apply_initial_import_projection_delta_and_checkpoint(
&mut self,
delta: SearchProjectionDelta,
import_source_graph_commit_epoch: u64,
) -> Result<SearchProjectionDeltaReport> {
let local_graph_commit_epoch = self.graph.database().commit_epoch()?;
let projection = require_search_projection_mut(&mut self.search_projection)?;
projection
.index()
.validate_import_source_graph_commit_epoch(import_source_graph_commit_epoch)?;
let report = projection
.index_mut()
.apply_projection_delta(SearchProjectionDelta {
source_graph_commit_epoch: Some(local_graph_commit_epoch),
..delta
})?;
projection
.index_mut()
.record_import_source_graph_commit_epoch(import_source_graph_commit_epoch)?;
projection.index().checkpoint()?;
Ok(report)
}
pub fn set_telemetry_sink(&mut self, telemetry: Option<Arc<dyn TelemetrySink>>) {
self.graph
.database_mut()
.set_telemetry_sink(telemetry.clone());
if let Some(search_projection) = &mut self.search_projection {
search_projection.set_telemetry_sink(telemetry);
}
}
pub fn storage_recovery_report(&self) -> Result<NowledgeMemStorageRecoveryReport> {
Ok({
NowledgeMemStorageRecoveryReport::from_storage_report(
&self.graph.database().storage_recovery_report()?,
)
})
}
pub fn storage_recovery_report_json(&self) -> Result<serde_json::Value> {
Ok(self.storage_recovery_report()?.json())
}
pub fn production_resource_profile(
&self,
statement: &NowledgeGraphStatement,
limits: StorageResourceProfileLimits,
evidence_binding: crate::ProductionEvidenceBinding,
expected_identity: crate::ProductionQualificationIdentity,
) -> Result<StorageResourceProfileReport> {
self.graph
.database()
.storage_resource_profile_for_production(
&statement.cypher,
&statement.parameters,
limits,
evidence_binding,
expected_identity,
)
}
pub fn storage_lifecycle_decision(&self) -> Result<NowledgeMemStorageLifecycleDecision> {
Ok({
NowledgeMemStorageLifecycleDecision::from_storage_recovery(
self.storage_recovery_report()?,
)
})
}
pub fn storage_lifecycle_decision_json(&self) -> Result<serde_json::Value> {
Ok(self.storage_lifecycle_decision()?.json())
}
pub fn runtime_status(&self) -> Result<NowledgeMemRuntimeStatus> {
Ok({
let graph_commit_epoch = self.graph.database().commit_epoch()?;
NowledgeMemRuntimeStatus {
protocol: NOWLEDGE_MEM_RUNTIME_STATUS_PROTOCOL.to_string(),
graph_commit_epoch,
changefeed: self
.graph
.database()
.search_projection_changefeed_status()?,
projection_freshness: self.search_projection_freshness(),
file_descriptors: self.graph.database().file_descriptor_metrics(),
}
})
}
pub fn production_status(
&self,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<NowledgeMemProductionStatus> {
Ok({
NowledgeMemProductionStatus::from_runtime(
self.graph.mode(),
self.graph.database().config().read_only,
self.runtime_status()?,
route_ownership.cloned(),
)
})
}
pub fn production_status_json(
&self,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<serde_json::Value> {
Ok(self.production_status(route_ownership)?.json())
}
pub fn cutover_controls_report(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
Ok({
self.cutover_controls_report_with_initial_import_cutover_catch_up(
controls,
route_ownership,
None,
)?
})
}
pub fn cutover_controls_report_with_initial_import_cutover_catch_up(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_cutover_catch_up: Option<&HawDBLightningInitialImportCutoverCatchUpReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
Ok({
NowledgeMemCutoverControlsReport::from_production_status(
controls,
self.production_status(route_ownership)?,
initial_import_cutover_catch_up.is_some_and(|report| report.ready),
)
})
}
pub fn cutover_controls_report_with_initial_import_recovery(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_recovery: Option<&HawDBLightningInitialImportRecoveryReadinessReport>,
) -> Result<NowledgeMemCutoverControlsReport> {
Ok({
let recovery_ready = initial_import_recovery.is_some_and(|report| {
report.ready
&& report.startup.readiness.ready_for_cutover
&& report
.startup
.cutover_catch_up
.as_ref()
.is_some_and(|catch_up| catch_up.ready)
});
let mut report = NowledgeMemCutoverControlsReport::from_production_status(
controls,
self.production_status(route_ownership)?,
recovery_ready,
);
if report.initial_import_enabled && !recovery_ready {
report.ready = false;
report
.blocker_codes
.push("initial_import_recovery_not_ready".to_string());
report.blocker_codes.sort();
report.blocker_codes.dedup();
}
report
})
}
pub fn cutover_controls_report_json(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
) -> Result<serde_json::Value> {
Ok({
self.cutover_controls_report(controls, route_ownership)?
.json()
})
}
pub fn cutover_controls_report_json_with_initial_import_cutover_catch_up(
&self,
controls: NowledgeMemCutoverControls,
route_ownership: Option<&NowledgeMemRouteOwnershipReadinessReport>,
initial_import_cutover_catch_up: Option<&HawDBLightningInitialImportCutoverCatchUpReport>,
) -> Result<serde_json::Value> {
Ok({
self.cutover_controls_report_with_initial_import_cutover_catch_up(
controls,
route_ownership,
initial_import_cutover_catch_up,
)?
.json()
})
}
pub fn build_search_projection_graph_delta_request_from_freshness(
&self,
max_operations: Option<usize>,
) -> Result<Option<SearchProjectionGraphDeltaRequest>> {
let search_projection = self.require_search_projection()?;
self.graph
.database()
.build_search_projection_graph_delta_request_from_freshness(
search_projection.index(),
max_operations,
)
}
pub fn search_projection_changefeed_readiness(
&self,
require_restart_recoverable: bool,
max_operations: Option<usize>,
) -> Result<SearchProjectionChangefeedReadiness> {
if self.incremental_search_enabled() {
let freshness = self.search_projection_freshness().ok_or_else(|| {
HawDBError::Storage("incremental search reader is detached".into())
})?;
return Ok(self
.graph
.database()
.search_projection_changefeed_status()?
.readiness_after(
freshness.source_graph_commit_epoch,
freshness.durable_source_graph_commit_epoch,
require_restart_recoverable,
max_operations,
));
}
let search_projection = self.require_search_projection()?;
self.graph
.database()
.search_projection_changefeed_readiness(
search_projection.index(),
require_restart_recoverable,
max_operations,
)
}
pub fn search_projection_graph_delta_background_work_plan(
&self,
request: &SearchProjectionGraphDeltaRequest,
hint: BackgroundWorkHint,
) -> Result<Option<BackgroundWorkPlan>> {
let Some(search_projection) = self.search_projection.as_ref() else {
return Ok(None);
};
self.graph
.database()
.search_projection_graph_delta_freshness_background_work_plan(
search_projection.index(),
request,
hint,
)
}
pub fn apply_search_projection_graph_delta(
&mut self,
request: SearchProjectionGraphDeltaRequest,
) -> Result<SearchProjectionDeltaReport> {
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph
.database()
.apply_search_projection_graph_delta(search_projection.index_mut(), request)
}
pub fn catch_up_search_projection(
&mut self,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<SearchProjectionCatchUpReport> {
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph.database().catch_up_search_projection(
search_projection.index_mut(),
max_operations_per_batch,
max_batches,
)
}
pub fn catch_up_search_projection_with_relational<F>(
&mut self,
max_operations_per_batch: usize,
max_batches: usize,
relational_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph.database().catch_up_search_projection_with_relational(
search_projection.index_mut(),
max_operations_per_batch,
max_batches,
relational_hydrator,
)
}
pub fn catch_up_search_projection_with_batch_hydrator<F>(
&mut self,
max_change_operations_per_batch: usize,
max_projection_operations_per_batch: usize,
max_batches: usize,
batch_hydrator: F,
) -> Result<SearchProjectionCatchUpReport>
where
F: FnMut(
&mut DatabaseReadTransaction,
&SearchProjectionChangeBatch,
) -> Result<SearchProjectionRelationalDelta>,
{
if self.incremental_search_enabled() {
let operations = max_change_operations_per_batch
.max(max_projection_operations_per_batch)
.saturating_mul(max_batches);
let permit = self.admit_incremental_search_maintenance(
operations.saturating_mul(SEARCH_PROJECTION_CHANGEFEED_OPERATION_BYTES),
)?;
return self.catch_up_incremental_search_with_batch_hydrator(
max_change_operations_per_batch,
max_projection_operations_per_batch,
max_batches,
batch_hydrator,
permit.bind_task_context(RuntimeTaskContext::default()),
);
}
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph
.database()
.catch_up_search_projection_with_batch_hydrator(
search_projection.index_mut(),
max_change_operations_per_batch,
max_projection_operations_per_batch,
max_batches,
batch_hydrator,
)
}
pub fn catch_up_search_projection_scheduled(
&mut self,
max_operations_per_batch: usize,
max_batches: usize,
) -> Result<ScheduledSearchProjectionCatchUpReport> {
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph.database().catch_up_search_projection_scheduled(
search_projection.index_mut(),
max_operations_per_batch,
max_batches,
)
}
pub fn apply_scheduled_background_search_projection_graph_delta(
&mut self,
request: SearchProjectionGraphDeltaRequest,
) -> Result<SearchProjectionDeltaReport> {
let Self {
graph,
search_projection,
..
} = self;
let search_projection = require_search_projection_mut(search_projection)?;
graph
.database()
.apply_scheduled_background_search_projection_graph_delta(
search_projection.index_mut(),
request,
)
}
pub fn search_projection_probe_json(
&self,
options: SearchProjectionProbeOptions,
) -> Result<serde_json::Value> {
Ok(self.require_search_projection()?.probe_json(options))
}
pub fn search_projection_evidence_json(
&self,
options: SearchProjectionProbeOptions,
) -> Result<serde_json::Value> {
Ok(self.require_search_projection()?.evidence_json(options))
}
pub fn search_projection_evidence_report(
&self,
options: SearchProjectionProbeOptions,
) -> Result<NowledgeSearchProjectionEvidenceReport> {
Ok(self.require_search_projection()?.evidence_report(options))
}
pub fn validate_sampled_vector_recall(
&self,
options: VectorRecallValidationOptions,
) -> Result<VectorRecallValidationReport> {
Ok(self
.require_search_projection()?
.validate_sampled_vector_recall(options))
}
pub fn qualify_sampled_vector_recall_for_production(
&self,
options: VectorRecallValidationOptions,
evidence_binding: crate::ProductionEvidenceBinding,
expected_identity: crate::ProductionQualificationIdentity,
) -> Result<VectorRecallProductionQualificationReport> {
Ok(self
.require_search_projection()?
.qualify_sampled_vector_recall_for_production(
options,
evidence_binding,
expected_identity,
))
}
pub fn search_projection_shadow_evidence_json(
&self,
primary_probe: &serde_json::Value,
options: SearchProjectionProbeOptions,
) -> Result<serde_json::Value> {
Ok(self
.require_search_projection()?
.shadow_evidence_json(primary_probe, options))
}
pub fn search_candidates(
&self,
request: &NowledgeMemSearchCandidateRequest,
) -> Result<NowledgeMemSearchCandidateOutput> {
match (
self.search_projection.as_ref(),
self.out_of_core_search_projection.as_ref(),
) {
(Some(projection), None) => projection.try_search_candidates_with_report(request),
(None, Some(projection)) => {
Ok(projection.search_candidates_with_report(request)?.into())
}
(None, None) => Err(missing_search_projection_error()),
(Some(_), Some(_)) => Err(ambiguous_search_projection_error()),
}
}
pub fn search_candidate_readiness(
&self,
request: &NowledgeMemSearchCandidateRequest,
options: &NowledgeMemSearchCandidateReadinessOptions,
) -> Result<NowledgeMemSearchCandidateReadinessReport> {
Ok(self.search_candidates(request)?.readiness_report(options))
}
pub fn search_candidate_shadow_evidence_json<I, S>(
&self,
request: &NowledgeMemSearchCandidateRequest,
primary_candidate_ids: I,
) -> Result<serde_json::Value>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
self.require_search_projection()?
.search_candidate_shadow_evidence_json(request, primary_candidate_ids)
}
pub fn retrieve_knowledge(
&self,
request: &KnowledgeRetrievalRequest,
) -> Result<KnowledgeRetrievalOutput> {
Ok(self.retrieve_knowledge_with_report(request)?.output)
}
pub fn retrieve_knowledge_with_report(
&self,
request: &KnowledgeRetrievalRequest,
) -> Result<NowledgeMemRetrievalOutput> {
let (output, out_of_core_search_metrics) = match (
self.search_projection.as_ref(),
self.out_of_core_search_projection.as_ref(),
) {
(Some(search_projection), None) => (
self.graph
.database()
.try_retrieve_knowledge(search_projection.index(), request)?,
None,
),
(None, Some(search_projection)) => {
let search = search_projection
.search_candidates_with_report(&self.search_request_for_retrieval(request))?;
let output = self.graph.database().try_retrieve_knowledge_from_search(
search.result,
search_projection.freshness(),
request,
)?;
(output, Some(search.metrics))
}
(None, None) => return Err(missing_search_projection_error()),
(Some(_), Some(_)) => return Err(ambiguous_search_projection_error()),
};
let report = nowledge_mem_retrieval_report(
self.graph.mode(),
self.graph.database().config().compressed_vector_search_mode,
&output,
);
Ok(NowledgeMemRetrievalOutput {
output,
report,
out_of_core_search_metrics,
})
}
pub fn read_query(&self, cypher: &str) -> Result<NowledgeMemReadOutput> {
self.graph.read_query(cypher)
}
pub fn query_with_report(&mut self, cypher: &str) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options(
cypher,
&BTreeMap::new(),
NowledgeMemQueryReportOptions::default(),
)
}
pub fn query_with_report_options(
&mut self,
cypher: &str,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options(cypher, &BTreeMap::new(), options)
}
pub fn query_with_params_with_report(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
) -> Result<NowledgeMemQueryOutput> {
self.query_with_params_with_report_options(
cypher,
parameters,
NowledgeMemQueryReportOptions::default(),
)
}
pub fn query_with_params_with_report_options(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
) -> Result<NowledgeMemQueryOutput> {
let Self {
graph,
search_projection,
out_of_core_search_projection,
..
} = self;
let mut external = SearchProjectionExternalReadOperator {
projection: search_projection.as_ref(),
out_of_core_projection: out_of_core_search_projection.as_ref(),
vector_seed_execution_count: 0,
};
graph.query_with_params_with_report_options_and_external(
cypher,
parameters,
options,
&mut external,
)
}
pub fn query_with_params_with_report_options_context(
&mut self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: NowledgeMemQueryReportOptions,
task_context: &RuntimeTaskContext,
) -> Result<NowledgeMemQueryOutput> {
let Self {
graph,
search_projection,
out_of_core_search_projection,
..
} = self;
let mut external = SearchProjectionExternalReadOperator {
projection: search_projection.as_ref(),
out_of_core_projection: out_of_core_search_projection.as_ref(),
vector_seed_execution_count: 0,
};
graph.query_with_params_with_report_options_and_external_context(
cypher,
parameters,
options,
&mut external,
task_context,
)
}
pub fn query_runtime_preflight(
&mut self,
probes: &[NowledgeQueryRuntimePreflightProbe],
) -> NowledgeQueryRuntimePreflightReport {
nowledge_query_runtime_preflight_report(self.graph.database_mut(), probes)
}
pub fn query_runtime_preflight_json(
&mut self,
probes: &[NowledgeQueryRuntimePreflightProbe],
) -> serde_json::Value {
self.query_runtime_preflight(probes).json()
}
pub fn slow_query_report(&self) -> NowledgeMemSlowQueryReport {
self.graph.slow_query_report()
}
pub fn slow_query_report_json(&self) -> serde_json::Value {
self.slow_query_report().json()
}
pub fn read_query_with_options(
&self,
cypher: &str,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.graph.read_query_with_options(cypher, options)
}
pub fn read_query_with_params(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.graph
.read_query_with_params(cypher, parameters, options)
}
pub fn read_query_with_params_streaming(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.graph
.read_query_with_params_streaming(cypher, parameters, options, consumer)
}
pub fn read_query_with_params_streaming_context(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
task_context: &RuntimeTaskContext,
consumer: impl FnMut(BTreeMap<String, Value>) -> Result<()>,
) -> Result<QueryStreamReport> {
self.graph.read_query_with_params_streaming_context(
cypher,
parameters,
options,
task_context,
consumer,
)
}
pub fn read_query_with_params_streaming_collect(
&self,
cypher: &str,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.graph
.read_query_with_params_streaming_collect(cypher, parameters, options)
}
pub fn graph_rag_schema_context(
&self,
options: GraphRagSchemaContextOptions,
) -> Result<GraphRagSchemaContext> {
self.graph.graph_rag_schema_context(options)
}
pub fn read_generated_graph_rag(
&self,
query: &GraphRagGeneratedQuery,
parameters: &BTreeMap<String, Value>,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
self.graph
.read_generated_graph_rag(query, parameters, options)
}
}
impl NowledgeMemEmbeddedStore {
pub fn background_maintenance_summary(
&self,
policy: &LocalQosPolicy,
state: &LocalQosState,
options: BackgroundMaintenanceOptions,
) -> Result<BackgroundMaintenanceSummary> {
Ok({
self.graph.database().background_maintenance_summary(
self.search_projection
.as_ref()
.map(NowledgeMemSearchProjection::index),
policy,
state,
options,
)?
})
}
pub fn background_maintenance_report(
&self,
policy: &LocalQosPolicy,
state: &LocalQosState,
options: BackgroundMaintenanceOptions,
) -> Result<NowledgeMemBackgroundMaintenanceReport> {
Ok({
let summary = self.background_maintenance_summary(policy, state, options)?;
let slow_query = self.slow_query_report();
NowledgeMemBackgroundMaintenanceReport::from_summary_with_slow_query(
&summary,
Some(&slow_query),
)
})
}
pub fn background_maintenance_report_json(
&self,
policy: &LocalQosPolicy,
state: &LocalQosState,
options: BackgroundMaintenanceOptions,
) -> Result<serde_json::Value> {
Ok({
self.background_maintenance_report(policy, state, options)?
.json()
})
}
pub fn library_readiness_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
Ok(self.library_readiness(options)?.json())
}
pub fn library_readiness(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemLibraryReadinessReport> {
Ok({
let bounded_read_evidence = options
.bounded_read_evidence
.clone()
.unwrap_or_else(|| self.bounded_read_probe_evidence_json(options));
let storage_recovery = self.storage_recovery_report_json()?;
let background_maintenance = self.background_maintenance_report_json(
&options.qos_policy,
&options.qos_state,
options.background_maintenance_options.clone(),
)?;
let query_family_evidence = query_family_replacement_evidence_json(
options.replacement_readiness_by_query_family.as_ref(),
);
let graph_route_readiness =
nowledge_mem_graph_route_readiness_json(options.graph_route_readiness.as_ref());
let search_route_ownership = options
.search_route_ownership
.as_ref()
.map(NowledgeMemSearchRouteOwnershipReadinessReport::json)
.unwrap_or_else(missing_search_route_ownership_json);
let active_search_route_ownership = options
.active_search_route_ownership
.as_ref()
.map(NowledgeMemActiveSearchRouteOwnershipReadinessReport::json)
.unwrap_or_else(missing_active_search_route_ownership_json);
let active_search_route_readiness = options
.active_search_route_readiness
.as_ref()
.map(NowledgeMemActiveSearchRouteReadinessReport::json)
.unwrap_or_else(missing_active_search_route_readiness_json);
let search_projection_evidence = options
.search_projection_evidence
.clone()
.unwrap_or_else(|| {
self.search_projection_evidence_json(
options.search_projection_probe_options.clone(),
)
.unwrap_or_else(|_| missing_search_projection_evidence_json())
});
let search_projection_shadow_evidence = options
.search_projection_shadow_evidence
.clone()
.unwrap_or_else(|| {
options
.primary_search_projection_probe
.as_ref()
.map(|primary_probe| {
self.search_projection_shadow_evidence_json(
primary_probe,
options.search_projection_probe_options.clone(),
)
.unwrap_or_else(|_| missing_search_projection_shadow_evidence_json())
})
.unwrap_or_else(missing_primary_search_projection_probe_json)
});
let search_candidate_shadow_evidence = options
.search_candidate_shadow_evidence
.clone()
.unwrap_or_else(missing_search_candidate_shadow_evidence_json);
let workload_fixture_evidence = options
.workload_fixture_evidence
.as_ref()
.map(NowledgeGraphRouteWorkloadFixtureReport::json)
.unwrap_or_else(missing_workload_fixture_evidence_json);
let production_resource_profile = options
.production_resource_profile
.as_ref()
.map(StorageResourceProfileReport::json)
.unwrap_or_else(missing_production_resource_profile_json);
let assessment =
hawdb_readiness::library_readiness::assess_nowledge_mem_library_readiness(
&hawdb_readiness::library_readiness::NowledgeMemLibraryReadinessEvidence {
bounded_read_evidence: &bounded_read_evidence,
storage_recovery: &storage_recovery,
background_maintenance: &background_maintenance,
query_family_evidence: &query_family_evidence,
graph_route_readiness: &graph_route_readiness,
search_route_ownership: &search_route_ownership,
active_search_route_ownership: &active_search_route_ownership,
active_search_route_readiness: &active_search_route_readiness,
search_projection_evidence: &search_projection_evidence,
search_projection_shadow_evidence: &search_projection_shadow_evidence,
search_candidate_shadow_evidence: &search_candidate_shadow_evidence,
workload_fixture_evidence: &workload_fixture_evidence,
production_resource_profile: &production_resource_profile,
},
);
let readiness_by_area = assessment.readiness_by_area;
let areas = readiness_by_area.areas();
let ready_area_count = areas.iter().filter(|area| area.ready).count();
let blocked_area_count = areas.len().saturating_sub(ready_area_count);
let blocker_codes = assessment.blocker_codes;
let ready = blocker_codes.is_empty();
NowledgeMemLibraryReadinessReport {
protocol: NOWLEDGE_MEM_LIBRARY_READINESS_PROTOCOL.to_string(),
present: true,
ready,
mode: self.graph.mode(),
redaction: NowledgeMemReadinessRedactionSummary::default(),
production_path: NowledgeMemLibraryProductionPathSummary::default(),
blocker_codes,
readiness_by_area,
ready_area_count,
blocked_area_count,
graph_open: true,
graph_read_only: self.graph.database().config().read_only,
graph_route_readiness,
search_route_ownership,
active_search_route_ownership,
active_search_route_readiness,
bounded_read_evidence,
storage_recovery,
background_maintenance,
query_family_evidence,
search_projection_evidence,
search_projection_shadow_evidence,
search_candidate_shadow_evidence,
workload_fixture_evidence,
production_resource_profile,
}
})
}
pub fn readiness_dashboard(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemReadinessDashboard> {
Ok({
let library = self.library_readiness(options)?;
let storage_lifecycle = self.storage_lifecycle_decision()?;
let slow_query = self.slow_query_report();
NowledgeMemReadinessDashboard::from_reports(&library, &storage_lifecycle, &slow_query)
})
}
pub fn readiness_dashboard_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
Ok(self.readiness_dashboard(options)?.json())
}
pub fn operations_readiness(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<NowledgeMemOperationsReadinessReport> {
Ok({
let runtime_status = self.runtime_status()?;
let storage_recovery = self.storage_recovery_report()?;
let slow_query = self.slow_query_report();
let background_maintenance = self.background_maintenance_report(
&options.qos_policy,
&options.qos_state,
options.background_maintenance_options.clone(),
)?;
NowledgeMemOperationsReadinessReport::from_reports(
self.graph.mode(),
self.graph.database().config().read_only,
runtime_status,
storage_recovery,
slow_query,
background_maintenance,
)
})
}
pub fn operations_readiness_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> Result<serde_json::Value> {
Ok(self.operations_readiness(options)?.json())
}
fn bounded_read_probe_evidence_json(
&self,
options: &NowledgeMemReadinessOptions,
) -> serde_json::Value {
let Some(probe) = options.bounded_read_probe.as_ref() else {
return serde_json::json!({
"protocol": NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL,
"present": false,
"ready": false,
"blocker_codes": ["bounded_read_probe_missing"],
});
};
match self.read_query_with_params(&probe.cypher, &probe.parameters, &options.read_options) {
Ok(read) => nowledge_mem_bounded_read_evidence_json_with_route_readiness(
&read.report,
&options.covered_routes,
options.graph_route_readiness.as_ref(),
),
Err(_) => serde_json::json!({
"protocol": NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL,
"present": true,
"ready": false,
"blocker_codes": ["bounded_read_probe_failed"],
}),
}
}
fn search_projection_freshness(&self) -> Option<SearchProjectionFreshness> {
match (
self.search_projection.as_ref(),
self.out_of_core_search_projection.as_ref(),
) {
(Some(projection), None) => Some(projection.freshness()),
(None, Some(projection)) => Some(projection.freshness()),
_ => None,
}
}
fn search_io_slots(&self) -> Result<usize> {
match (
self.search_projection.as_ref(),
self.out_of_core_search_projection.as_ref(),
) {
(Some(projection), None) => Ok(projection.index().range_read_config().io_depth.get()),
(None, Some(_)) => Ok(1),
(None, None) => Err(missing_search_projection_error()),
(Some(_), Some(_)) => Err(ambiguous_search_projection_error()),
}
}
fn search_memory_bytes(
&self,
working_memory_bytes: u64,
result_budget_bytes: u64,
) -> Result<u64> {
match (
self.search_projection.as_ref(),
self.out_of_core_search_projection.as_ref(),
) {
(Some(_), None) => Ok(working_memory_bytes),
(None, Some(projection)) => Ok(projection
.runtime_admission_memory_bytes(working_memory_bytes, result_budget_bytes)),
(None, None) => Err(missing_search_projection_error()),
(Some(_), Some(_)) => Err(ambiguous_search_projection_error()),
}
}
fn search_request_for_retrieval(
&self,
request: &KnowledgeRetrievalRequest,
) -> NowledgeMemSearchCandidateRequest {
let config = self.graph.database().config();
NowledgeMemSearchCandidateRequest {
query_text: request.query_text.clone(),
query_embedding: request.query_embedding.clone(),
mode: request.mode,
limit: request.limit,
offset: request.offset,
rank_window: request.rank_window,
fusion_weights: request.search_fusion_weights,
metadata_filters: request.metadata_filters.clone(),
compressed_vector_search_mode: config.compressed_vector_search_mode,
adaptive_vector_backend_policy: config.adaptive_vector_backend_policy,
recall_validation_probe: false,
retrieval_projection_advisor: self.retrieval_projection_advisor.clone(),
}
}
fn require_search_projection(&self) -> Result<&NowledgeMemSearchProjection> {
self.search_projection
.as_ref()
.ok_or_else(missing_search_projection_error)
}
}
fn missing_search_projection_error() -> HawDBError {
HawDBError::Storage("nowledge mem search projection is not configured".to_string())
}
fn ambiguous_search_projection_error() -> HawDBError {
HawDBError::Storage("nowledge mem search projection ownership is ambiguous".to_string())
}
fn require_search_projection_mut(
search_projection: &mut Option<NowledgeMemSearchProjection>,
) -> Result<&mut NowledgeMemSearchProjection> {
search_projection
.as_mut()
.ok_or_else(missing_search_projection_error)
}
struct NowledgeMemQueryReportInput<'a> {
mode: NowledgeMemGraphMode,
statement: &'a cypher::Statement,
trace: Option<&'a crate::optimizer::OptimizerTrace>,
plan_cache_lookup: Option<PlanCacheLookup>,
execution_profile: Option<&'a ReadExecutionProfile>,
output: &'a QueryOutput,
options: NowledgeMemQueryReportOptions,
elapsed_micros: u128,
}
fn nowledge_mem_query_report(input: NowledgeMemQueryReportInput<'_>) -> NowledgeMemQueryReport {
let statement_kind = crate::api::statement_kind(
hawdb_cypher::read_route::query_statement_body(input.statement),
);
let decision = nowledge_mem_fast_path_classification(input.statement);
hawdb_readiness::nowledge_mem_query_report::nowledge_mem_query_report(
hawdb_readiness::nowledge_mem_query_report::NowledgeMemQueryReportInput {
mode: input.mode,
statement_kind,
statement: input.statement,
execution_path: decision.execution_path,
fast_path_reason: decision.fast_path_reason,
physical_plan_captured: input.trace.is_some(),
physical_operator_counts: input
.trace
.map(|trace| trace.selected_plan_operator_counts.clone())
.unwrap_or_default(),
optimizer_decision_count: input
.trace
.map(|trace| trace.decisions.len())
.unwrap_or_default(),
optimizer_rule_event_count: input
.trace
.map(|trace| trace.rule_events.len())
.unwrap_or_default(),
plan_cache_lookup: input.plan_cache_lookup,
execution_profile: input.execution_profile,
output: input.output,
options: input.options,
elapsed_micros: input.elapsed_micros,
},
)
}
fn nowledge_query_runtime_preflight_report(
db: &mut Database,
probes: &[NowledgeQueryRuntimePreflightProbe],
) -> NowledgeQueryRuntimePreflightReport {
let route_coverage = nowledge_query_runtime_route_coverage(probes);
let probe_reports = probes
.iter()
.map(|probe| nowledge_query_runtime_probe_report(db, probe))
.collect::<Vec<_>>();
let passed_probe_count = probe_reports.iter().filter(|probe| probe.ready).count();
let failed_probe_count = probe_reports.len().saturating_sub(passed_probe_count);
let blocker_codes =
query_runtime_preflight_blocker_codes(probes.len(), failed_probe_count, &route_coverage);
NowledgeQueryRuntimePreflightReport {
protocol: NOWLEDGE_QUERY_RUNTIME_PREFLIGHT_PROTOCOL.to_string(),
ready: blocker_codes.is_empty(),
database_opened: true,
redaction: NowledgeQueryRuntimePreflightRedactionSummary::default(),
probe_count: probes.len(),
passed_probe_count,
failed_probe_count,
required_route_count: route_coverage.required_route_count,
covered_route_count: route_coverage.covered_route_count,
covered_routes: route_coverage.covered_routes,
missing_required_routes: route_coverage.missing_required_routes,
required_routes_covered: route_coverage.required_routes_covered,
unknown_routes: route_coverage.unknown_routes,
duplicate_routes: route_coverage.duplicate_routes,
route_catalog_version: NOWLEDGE_MEM_GRAPH_READ_ROUTE_CATALOG_VERSION.to_string(),
route_catalog_digest: nowledge_mem_graph_read_route_catalog_digest(),
route_coverage_ready: route_coverage.ready,
route_coverage_blocker_codes: route_coverage.blocker_codes,
blocker_codes,
probes: probe_reports,
}
}
fn nowledge_query_runtime_probe_report(
db: &mut Database,
probe: &NowledgeQueryRuntimePreflightProbe,
) -> NowledgeQueryRuntimePreflightProbeReport {
match db.explain_analyze_query_with_params(&probe.cypher, &probe.parameters) {
Ok(output) => {
let scan_pruning_report_count = output.execution_profile.scan_pruning_reports.len();
let pruned_scan_count = output
.execution_profile
.scan_pruning_reports
.iter()
.filter(|report| report.pruned)
.count();
let output_row_count = output.output.rows.len();
let blocker_codes = query_runtime_probe_blocker_codes(
probe,
scan_pruning_report_count,
pruned_scan_count,
output_row_count,
);
let plan_cache_lookup = output.plan_cache_lookup;
let plan_cache = nowledge_mem_plan_cache_report(Some(plan_cache_lookup));
NowledgeQueryRuntimePreflightProbeReport {
name: probe.name.clone(),
route: probe.route.clone(),
query_family: probe.query_family.clone(),
ready: blocker_codes.is_empty(),
success: true,
output_row_count,
selected_plan_fingerprint: Some(output.trace.selected_plan_fingerprint),
search_mode: Some(output.trace.search_mode.as_str().to_string()),
selected_plan_operator_counts: output.trace.selected_plan_operator_counts,
selected_plan_class_counts: output.trace.selected_plan_class_counts,
optimizer_decision_count: output.trace.decisions.len(),
optimizer_rule_event_count: output.trace.rule_events.len(),
plan_cache_lookup: Some(plan_cache_lookup.as_str().to_string()),
plan_cache_bypass_reason: plan_cache_lookup
.bypass_reason()
.map(|reason| reason.as_str().to_string()),
plan_cache_cacheable: plan_cache.cacheable,
plan_cache_hit: plan_cache.hit,
plan_cache_miss: plan_cache.miss,
plan_cache_bypassed: plan_cache.bypassed,
work_priority: Some(output.work_request.priority.as_str().to_string()),
work_class: Some(output.work_request.class.as_str().to_string()),
estimated_operations: Some(output.work_request.estimated_operations),
max_rows: output.execution_profile.max_rows,
detection_row_cap: output.execution_profile.detection_row_cap,
row_limit_enforced_before_output: output
.execution_profile
.row_limit_enforced_before_output,
operator_row_cap_enabled: output.execution_profile.operator_row_cap_enabled,
blocking_operator_kinds: output.execution_profile.blocking_operator_kinds,
scan_pruning_reports: output.execution_profile.scan_pruning_reports,
pruned_scan_count,
error_class: None,
blocker_codes,
}
}
Err(error) => NowledgeQueryRuntimePreflightProbeReport {
name: probe.name.clone(),
route: probe.route.clone(),
query_family: probe.query_family.clone(),
ready: false,
success: false,
output_row_count: 0,
selected_plan_fingerprint: None,
search_mode: None,
selected_plan_operator_counts: BTreeMap::new(),
selected_plan_class_counts: BTreeMap::new(),
optimizer_decision_count: 0,
optimizer_rule_event_count: 0,
plan_cache_lookup: None,
plan_cache_bypass_reason: None,
plan_cache_cacheable: false,
plan_cache_hit: false,
plan_cache_miss: false,
plan_cache_bypassed: false,
work_priority: None,
work_class: None,
estimated_operations: None,
max_rows: None,
detection_row_cap: None,
row_limit_enforced_before_output: false,
operator_row_cap_enabled: false,
blocking_operator_kinds: Vec::new(),
scan_pruning_reports: Vec::new(),
pruned_scan_count: 0,
error_class: Some(hawdb_error_class(&error).to_string()),
blocker_codes: vec!["query_runtime_failed".to_string()],
},
}
}
fn missing_search_projection_evidence_json() -> serde_json::Value {
serde_json::json!({
"protocol": "hawdb-nowledge-search-projection-evidence",
"present": false,
"ready": false,
"blocker_codes": ["search_projection_not_configured"],
})
}
fn missing_search_projection_shadow_evidence_json() -> serde_json::Value {
serde_json::json!({
"protocol": "hawdb-nowledge-search-projection-shadow-evidence",
"present": false,
"ready": false,
"blocker_codes": ["search_projection_not_configured"],
})
}
fn missing_primary_search_projection_probe_json() -> serde_json::Value {
serde_json::json!({
"protocol": "hawdb-nowledge-search-projection-shadow-evidence",
"present": false,
"ready": false,
"blocker_codes": ["primary_search_projection_probe_missing"],
})
}
fn missing_search_candidate_shadow_evidence_json() -> serde_json::Value {
serde_json::json!({
"protocol": NOWLEDGE_MEM_SEARCH_CANDIDATE_SHADOW_EVIDENCE_PROTOCOL,
"route": NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_ROUTE,
"evidence_source": NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_SOURCE,
"present": false,
"ready": false,
"candidate_primary_engine": NOWLEDGE_MEM_SEARCH_CANDIDATE_PRIMARY_ENGINE,
"request_count": null,
"primary_candidate_count": null,
"shadow_candidate_count": null,
"matched_candidate_count": null,
"primary_only_candidate_count": null,
"candidate_identity": {
"ready": false,
"parity": false,
},
"filter_pushdown_ready": false,
"filter_pushdown": {
"ready": false,
"required_fields": NOWLEDGE_SEARCH_PROJECTION_SCAN_FILTER_FIELDS,
"missing_required_fields": NOWLEDGE_SEARCH_PROJECTION_SCAN_FILTER_FIELDS,
"field_summary_count": 0,
"field_summaries": [],
"blocker_codes": ["search_candidate_shadow_evidence_missing"],
},
"blocker_codes": ["search_candidate_shadow_evidence_missing"],
})
}
fn missing_workload_fixture_evidence_json() -> serde_json::Value {
serde_json::json!({
"protocol": NOWLEDGE_GRAPH_ROUTE_WORKLOAD_FIXTURE_PROTOCOL,
"present": false,
"ready": false,
"route_count": null,
"query_count": null,
"failed_query_count": null,
"bounded_expansion_probe_count": null,
"failed_bounded_expansion_probe_count": null,
"search_metadata_probe_count": null,
"failed_search_metadata_probe_count": null,
"blocker_codes": ["workload_fixture_evidence_missing"],
})
}
fn missing_production_resource_profile_json() -> serde_json::Value {
serde_json::json!({
"protocol": STORAGE_RESOURCE_PROFILE_PROTOCOL,
"protocol_version": 2,
"present": false,
"resource_ready": false,
"ready": false,
"blocker_codes": ["production_resource_profile_missing"],
})
}
fn query_family_replacement_evidence_json(
replacement_readiness_by_query_family: Option<&serde_json::Value>,
) -> serde_json::Value {
let Some(families) =
query_family_replacement_readiness_array(replacement_readiness_by_query_family)
else {
return serde_json::json!({
"protocol": "hawdb-nowledge-query-family-evidence-v1",
"present": false,
"ready": false,
"required_query_families": REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES,
"missing_required_query_families": REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES,
"blocker_codes": ["query_family_evidence_missing"],
});
};
let health = replacement_readiness_family_evidence_health(Some(families));
let blocker_codes = query_family_replacement_blocker_codes(&health);
serde_json::json!({
"protocol": "hawdb-nowledge-query-family-evidence-v1",
"present": health.present,
"ready": health.ready,
"min_replacement_readiness_per_million": health.min_replacement_readiness_per_million,
"invalid_family_count": health.invalid_family_count,
"blocked_query_families": health.blocked_query_families,
"required_query_families": REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES,
"missing_required_query_families": health.missing_required_query_families,
"blocker_codes": blocker_codes,
"blockers": health.blockers,
"replacement_readiness_by_query_family": families,
})
}
fn query_family_replacement_readiness_array(
value: Option<&serde_json::Value>,
) -> Option<&serde_json::Value> {
let value = value?;
if value.is_array() {
return Some(value);
}
value
.get("replacement_readiness_by_query_family")
.filter(|families| families.is_array())
}
fn query_family_replacement_blocker_codes(
health: &crate::nowledge_inventory::ReplacementReadinessFamilyEvidenceHealth,
) -> Vec<&'static str> {
let mut blockers = Vec::new();
if health.invalid_family_count > 0 {
blockers.push("invalid_family_entries");
}
if !health.blocked_query_families.is_empty() {
blockers.push("blocked_query_families");
}
if !health.missing_required_query_families.is_empty() {
blockers.push("missing_required_query_families");
}
blockers
}
fn nowledge_mem_graph_route_readiness_json(
route_readiness: Option<&NowledgeMemRouteReadinessSummary>,
) -> serde_json::Value {
let Some(summary) = route_readiness else {
return serde_json::json!({
"protocol": NMEM_GRAPH_ROUTE_READINESS_PROTOCOL,
"present": false,
"ready": false,
"required_route_count": REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES.len(),
"primary_ready_route_count": 0,
"primary_ready_routes": [],
"missing_required_routes": REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES,
"route_catalog_version": NOWLEDGE_MEM_GRAPH_READ_ROUTE_CATALOG_VERSION,
"route_catalog_digest": nowledge_mem_graph_read_route_catalog_digest(),
"route_primary_ready": null,
"route_query_plan_evidence_ready": null,
"route_query_profile_evidence_ready": null,
"route_query_api_behavior_evidence_ready": null,
"relationship_property_pruning_required_count": null,
"relationship_property_pruning_report_count": null,
"route_relationship_property_pruning_evidence_ready": null,
"blocker_codes": ["graph_route_readiness_missing"],
});
};
let missing_required_routes =
hawdb_readiness::bounded_read_evidence::missing_nowledge_mem_bounded_read_routes(
&summary.primary_ready_routes,
);
let relationship_property_pruning_count_matches = summary
.relationship_property_pruning_required_count
== summary.relationship_property_pruning_report_count;
let mut blocker_codes = Vec::new();
if !summary.route_primary_ready || !missing_required_routes.is_empty() {
blocker_codes.push("route_primary_not_ready");
}
if !summary.route_query_plan_evidence_ready {
blocker_codes.push("route_query_plan_evidence_not_ready");
}
if !summary.route_query_profile_evidence_ready {
blocker_codes.push("route_query_profile_evidence_not_ready");
}
if !summary.route_query_api_behavior_evidence_ready {
blocker_codes.push("route_query_api_behavior_evidence_not_ready");
}
if !summary.route_relationship_property_pruning_evidence_ready
|| !relationship_property_pruning_count_matches
{
blocker_codes.push("route_relationship_property_pruning_not_ready");
}
serde_json::json!({
"protocol": NMEM_GRAPH_ROUTE_READINESS_PROTOCOL,
"present": true,
"ready": blocker_codes.is_empty(),
"required_route_count": REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES.len(),
"primary_ready_route_count": summary.primary_ready_routes.len(),
"primary_ready_routes": summary.primary_ready_routes,
"missing_required_routes": missing_required_routes,
"route_catalog_version": NOWLEDGE_MEM_GRAPH_READ_ROUTE_CATALOG_VERSION,
"route_catalog_digest": nowledge_mem_graph_read_route_catalog_digest(),
"route_primary_ready": summary.route_primary_ready,
"route_query_plan_evidence_ready": summary.route_query_plan_evidence_ready,
"route_query_profile_evidence_ready": summary.route_query_profile_evidence_ready,
"route_query_api_behavior_evidence_ready": summary.route_query_api_behavior_evidence_ready,
"relationship_property_pruning_required_count": summary.relationship_property_pruning_required_count,
"relationship_property_pruning_report_count": summary.relationship_property_pruning_report_count,
"route_relationship_property_pruning_evidence_ready": summary.route_relationship_property_pruning_evidence_ready,
"blocker_codes": blocker_codes,
})
}
fn missing_search_route_ownership_json() -> serde_json::Value {
serde_json::json!({
"protocol": NOWLEDGE_MEM_SEARCH_ROUTE_OWNERSHIP_PROTOCOL,
"ready": false,
"production_cutover_ready": false,
"require_all_hawdb": true,
"required_route_count": REQUIRED_NOWLEDGE_MEM_SEARCH_ROUTES.len(),
"explicit_route_count": 0,
"hawdb_route_count": 0,
"lancedb_route_count": 0,
"routes": [],
"hawdb_routes": [],
"lancedb_routes": [],
"missing_required_routes": REQUIRED_NOWLEDGE_MEM_SEARCH_ROUTES,
"unknown_routes": [],
"duplicate_routes": [],
"conflicting_routes": [],
"blocker_codes": ["search_route_ownership_missing"],
})
}
fn missing_active_search_route_ownership_json() -> serde_json::Value {
serde_json::json!({
"protocol": NOWLEDGE_MEM_SEARCH_ROUTE_OWNERSHIP_PROTOCOL,
"ready": false,
"production_cutover_ready": false,
"require_all_hawdb": true,
"required_route_count": REQUIRED_NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTES.len(),
"explicit_route_count": 0,
"hawdb_route_count": 0,
"lancedb_route_count": 0,
"routes": [],
"hawdb_routes": [],
"lancedb_routes": [],
"missing_required_routes": REQUIRED_NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTES,
"unknown_routes": [],
"duplicate_routes": [],
"invalid_projection_routes": [],
"blocker_codes": ["active_search_route_ownership_missing"],
})
}
fn missing_active_search_route_readiness_json() -> serde_json::Value {
serde_json::json!({
"protocol": NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTE_READINESS_PROTOCOL,
"ready": false,
"production_cutover_ready": false,
"require_all_hawdb": true,
"required_route_count": REQUIRED_NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTES.len(),
"evidence_route_count": 0,
"ready_route_count": 0,
"hawdb_route_count": 0,
"lancedb_handle_required_route_count": 0,
"routes": [],
"ready_routes": [],
"missing_required_routes": REQUIRED_NOWLEDGE_MEM_ACTIVE_SEARCH_ROUTES,
"unknown_routes": [],
"duplicate_routes": [],
"invalid_projection_routes": [],
"non_hawdb_routes": [],
"lancedb_handle_required_routes": [],
"candidate_not_ready_routes": [],
"candidate_identity_not_ready_routes": [],
"metadata_pushdown_not_ready_routes": [],
"ranking_not_ready_routes": [],
"fail_soft_not_ready_routes": [],
"blocker_codes": ["active_search_route_readiness_missing"],
})
}
fn nowledge_mem_readiness_dashboard_areas(
library: &NowledgeMemLibraryReadinessReport,
slow_query: &NowledgeMemSlowQueryReport,
) -> Vec<NowledgeMemReadinessAreaSummary> {
let mut areas = library.areas();
areas.push(NowledgeMemReadinessAreaSummary {
name: "slow_query".to_string(),
ready: slow_query.ready,
blocker_codes: if slow_query.ready {
Vec::new()
} else {
vec!["slow_query_report_not_ready".to_string()]
},
});
areas
}
fn nowledge_mem_retrieval_report(
mode: NowledgeMemGraphMode,
compressed_vector_search_mode: CompressedVectorSearchMode,
output: &KnowledgeRetrievalOutput,
) -> NowledgeMemRetrievalReport {
let vector_backend = output
.search
.retrievers
.iter()
.find(|retriever| retriever.name == "vector")
.map(|retriever| retriever.backend.clone());
let text_backend = output
.search
.retrievers
.iter()
.find(|retriever| retriever.name == "text")
.map(|retriever| retriever.backend.clone());
let search_backend = vector_backend
.clone()
.or_else(|| text_backend.clone())
.or_else(|| {
output
.search
.retrievers
.first()
.map(|retriever| retriever.backend.clone())
});
let knowledge_fallback_reason_codes = output
.diagnostics
.graph_context_fallback_reason_codes
.iter()
.map(|code| code.as_str().to_string())
.collect::<Vec<_>>();
let retriever_fallback_reason_codes = output
.retrievers
.iter()
.flat_map(|retriever| retriever.fallback_reason_codes.iter())
.map(|code| code.as_str().to_string())
.collect::<Vec<_>>();
let truncation_reason_codes = output
.diagnostics
.search_truncation_reason_codes
.iter()
.map(|code| code.as_str().to_string())
.chain(
output
.diagnostics
.graph_seed_truncation_reason_codes
.iter()
.map(|code| code.as_str().to_string()),
)
.chain(
output
.diagnostics
.graph_context_truncation_reason_codes
.iter()
.map(|code| code.as_str().to_string()),
)
.chain(
output
.diagnostics
.candidate_truncation_reason_codes
.iter()
.map(|code| code.as_str().to_string()),
)
.collect::<Vec<_>>();
NowledgeMemRetrievalReport {
protocol: NOWLEDGE_MEM_RETRIEVAL_REPORT_PROTOCOL.to_string(),
mode,
compressed_vector_search_mode,
graph_commit_epoch: output.graph_commit_epoch,
projection_source_graph_commit_epoch: output.projection_freshness.source_graph_commit_epoch,
projection_commit_lag: output.diagnostics.projection_commit_lag,
projection_stale: output.diagnostics.projection_stale,
search_document_count: output.search.document_count,
search_filtered_document_count: output.search.filtered_document_count,
search_total_hits: output.search.total_hits,
candidate_count: output.candidates.len(),
candidate_total_count: output.diagnostics.candidate_total_count,
evidence_count: output.evidence.len(),
graph_seed_count: output.graph_seeds.len(),
graph_context_path_count: output.graph_context_paths.len(),
search_backend,
vector_backend,
text_backend,
search_fallback_reason_codes: output
.diagnostics
.search_fallback_reason_codes
.iter()
.map(|code| code.as_str().to_string())
.collect(),
retriever_fallback_reason_codes,
knowledge_fallback_reason_codes,
truncation_reason_codes,
warning_count: output.diagnostics.warnings.len(),
warnings: output.diagnostics.warnings.clone(),
}
}
fn nowledge_mem_read_report(
mode: NowledgeMemGraphMode,
output: &QueryOutput,
options: &NowledgeMemReadOptions,
execution_profile: &ReadExecutionProfile,
) -> NowledgeMemReadReport {
let estimated_payload_bytes = estimate_query_output_payload_bytes(output);
NowledgeMemReadReport {
protocol: NOWLEDGE_MEM_READ_REPORT_PROTOCOL.to_string(),
mode,
row_count: output.rows.len(),
max_rows: options.max_rows,
execution_row_cap: execution_profile.detection_row_cap,
estimated_payload_bytes,
max_estimated_payload_bytes: options.max_estimated_payload_bytes,
row_budget_exceeded: options
.max_rows
.is_some_and(|max_rows| output.rows.len() > max_rows),
payload_budget_exceeded: options
.max_estimated_payload_bytes
.is_some_and(|max_bytes| estimated_payload_bytes > max_bytes),
row_limit_enforced_before_output: execution_profile.row_limit_enforced_before_output,
operator_row_cap_enabled: execution_profile.operator_row_cap_enabled,
blocking_operator_count: execution_profile.blocking_operator_count(),
blocking_operator_kinds: execution_profile.blocking_operator_kinds.clone(),
blocking_operator_memory_reports: execution_profile
.blocking_operator_memory_reports
.clone(),
intermediate_rows: execution_profile.pipeline_memory_report.intermediate_rows,
intermediate_payload_bytes: execution_profile
.pipeline_memory_report
.intermediate_payload_bytes,
output_payload_bytes: execution_profile
.pipeline_memory_report
.output_payload_bytes,
steady_resident_bytes: execution_profile
.pipeline_memory_report
.steady_resident_bytes,
peak_resident_bytes: execution_profile.pipeline_memory_report.peak_resident_bytes,
total_page_faults: execution_profile.pipeline_memory_report.total_page_faults,
minor_page_faults: execution_profile.pipeline_memory_report.minor_page_faults,
major_page_faults: execution_profile.pipeline_memory_report.major_page_faults,
streaming: false,
}
}
fn bounded_nowledge_mem_read_output(
mode: NowledgeMemGraphMode,
bounded: BoundedReadQueryOutput,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
let report =
nowledge_mem_read_report(mode, &bounded.output, options, &bounded.execution_profile);
if report.row_budget_exceeded {
return Err(HawDBError::read_budget_exceeded(
ReadBudgetResource::Rows,
report.max_rows.unwrap_or_default(),
format!(
"nowledge mem read query returned {} rows, exceeding max_rows {}",
report.row_count,
report.max_rows.unwrap_or_default()
),
));
}
if report.payload_budget_exceeded {
return Err(HawDBError::read_budget_exceeded(ReadBudgetResource::PayloadBytes, report.max_estimated_payload_bytes.unwrap_or_default(), format!(
"nowledge mem read query estimated {} payload bytes, exceeding max_estimated_payload_bytes {}",
report.estimated_payload_bytes,
report.max_estimated_payload_bytes.unwrap_or_default()
)));
}
Ok(NowledgeMemReadOutput {
output: bounded.output,
report,
})
}
fn streamed_nowledge_mem_read_output(
mode: NowledgeMemGraphMode,
rows: Vec<BTreeMap<String, Value>>,
streamed: QueryStreamReport,
options: &NowledgeMemReadOptions,
) -> Result<NowledgeMemReadOutput> {
let output = QueryOutput { rows: rows.into() };
let mut report = nowledge_mem_read_report(mode, &output, options, &streamed.execution_profile);
report.streaming = true;
debug_assert_eq!(report.row_count, streamed.output_rows);
debug_assert_eq!(
report.estimated_payload_bytes,
streamed.output_payload_bytes
);
Ok(NowledgeMemReadOutput { output, report })
}
fn legacy_nowledge_mem_read_error(
error: HawDBError,
options: &NowledgeMemReadOptions,
) -> HawDBError {
let Some(max_payload_bytes) = options.max_estimated_payload_bytes else {
return error;
};
if matches!(
&error,
HawDBError::ReadBudgetExceeded(cause)
if cause.resource == ReadBudgetResource::PayloadBytes && cause.limit == max_payload_bytes
) {
return HawDBError::read_budget_exceeded(ReadBudgetResource::PayloadBytes, max_payload_bytes, format!(
"nowledge mem read query payload exceeding max_estimated_payload_bytes {max_payload_bytes}"
));
}
error
}
fn estimate_query_output_payload_bytes(output: &QueryOutput) -> usize {
output
.rows
.iter()
.map(|row| {
row.iter()
.map(|(key, value)| key.len() + estimate_value_payload_bytes(value))
.sum::<usize>()
})
.sum()
}
fn estimate_value_payload_bytes(value: &Value) -> usize {
match value {
Value::Null => 0,
Value::Bool(_) => 1,
Value::Int(_) | Value::Float(_) => std::mem::size_of::<i64>(),
Value::String(value) => value.len(),
Value::Binary(value) => value.len(),
Value::Uuid(_) => 16,
Value::List(values) => values.iter().map(estimate_value_payload_bytes).sum(),
Value::Map(values) => values
.iter()
.map(|(key, value)| key.len() + estimate_value_payload_bytes(value))
.sum(),
}
}
#[cfg(test)]
mod tests {
use super::legacy_nowledge_mem_read_error;
use super::{
nowledge_mem_bounded_read_evidence_json_with_route_readiness,
nowledge_mem_fast_path_classification, nowledge_mem_graph_config,
nowledge_mem_graph_config_with_search_mode,
nowledge_mem_source_mutation_dual_write_evidence_all_ready,
nowledge_mem_source_mutation_dual_write_readiness, NowledgeMemCutoverControls,
NowledgeMemEmbeddedStore, NowledgeMemEmbeddedStoreHandle, NowledgeMemGraph,
NowledgeMemGraphMode, NowledgeMemOpenDiagnosticOptions, NowledgeMemOpenOptions,
NowledgeMemOutOfCoreSearchProjection, NowledgeMemQualifiedOutOfCoreSearchOptions,
NowledgeMemQueryExecutionPath, NowledgeMemQueryReportOptions, NowledgeMemReadOptions,
NowledgeMemReadinessAreaSummary, NowledgeMemReadinessDashboard,
NowledgeMemReadinessOptions, NowledgeMemRetrievalProjectionAdvisor,
NowledgeMemRouteReadinessSummary, NowledgeMemSearchCandidateReadinessOptions,
NowledgeMemSearchCandidateRequest, NowledgeMemSearchCandidateShadowAccumulator,
NowledgeMemSearchProjection, NowledgeMemServingEntrypoint, NowledgeMemServingPathReadiness,
NowledgeMemSourceMutationDualWriteEvidence, NowledgeMemStorageLifecycleActionKind,
NowledgeMemStorageLifecycleDecision, NowledgeMemStorageRecoveryReport,
NowledgeMemWorkControl, NowledgeQueryRuntimePreflightProbe,
NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL, NOWLEDGE_MEM_CUTOVER_CONTROLS_PROTOCOL,
NOWLEDGE_MEM_LIBRARY_READINESS_PROTOCOL, NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL,
NOWLEDGE_MEM_OPERATIONS_READINESS_PROTOCOL, NOWLEDGE_MEM_PRODUCTION_STATUS_PROTOCOL,
NOWLEDGE_MEM_QUERY_REPORT_PROTOCOL, NOWLEDGE_MEM_READINESS_DASHBOARD_PROTOCOL,
NOWLEDGE_MEM_READ_REPORT_PROTOCOL, NOWLEDGE_MEM_RETRIEVAL_REPORT_PROTOCOL,
NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_SOURCE,
NOWLEDGE_MEM_SEARCH_CANDIDATE_READINESS_PROTOCOL,
NOWLEDGE_MEM_SEARCH_CANDIDATE_REPORT_PROTOCOL, NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL,
NOWLEDGE_MEM_SOURCE_MUTATION_DUAL_WRITE_READINESS_PROTOCOL,
NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_INGEST_CREATE,
NOWLEDGE_MEM_STORAGE_LIFECYCLE_DECISION_PROTOCOL,
NOWLEDGE_QUERY_RUNTIME_PREFLIGHT_PROTOCOL, NOWLEDGE_SEARCH_PROJECTION_SCAN_FILTER_FIELDS,
REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES, REQUIRED_NOWLEDGE_MEM_SOURCE_MUTATION_FAMILIES,
REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES,
};
use crate::mem_integration_readiness::nowledge_mem_final_cutover_preflight;
use crate::route_ownership::{
nowledge_mem_route_ownership_all_hawdb, nowledge_mem_route_ownership_all_legacy,
nowledge_mem_route_ownership_readiness, NowledgeMemRouteOwnershipPolicy,
};
use crate::search::{
CompressedVectorSearchMode, SearchFusionWeights, SearchLexicalFeasibilityCoverage,
SearchLexicalFeasibilityMetrics, SearchLexicalProductionQualificationReport,
SearchOutOfCoreConfig, SearchTopKScoreParity,
};
use crate::search_route_ownership::{
nowledge_mem_active_search_route_ownership_all_hawdb,
nowledge_mem_active_search_route_ownership_readiness,
nowledge_mem_active_search_route_read_evidence_all_hawdb_ready,
nowledge_mem_active_search_route_readiness, nowledge_mem_search_route_ownership_all_hawdb,
nowledge_mem_search_route_ownership_readiness,
NowledgeMemActiveSearchRouteOwnershipReadinessReport,
NowledgeMemActiveSearchRouteReadinessReport, NowledgeMemSearchRouteOwnershipPolicy,
NowledgeMemSearchRouteOwnershipReadinessReport,
};
use crate::workload_fixtures::{
nowledge_graph_route_workload_fixture_report, NowledgeGraphRouteWorkloadFixtureOptions,
};
use crate::AdaptiveVectorBackendPolicy;
use crate::ReadBudgetResource;
use crate::Value;
use crate::{
BackgroundMaintenanceKind, BackgroundMaintenanceOptions, BackgroundWorkHint, Database,
DatabaseConfig, GraphRagQueryBinding, GraphRagQueryDraft, GraphRagQueryPattern,
GraphRagQueryPredicate, GraphRagQueryPredicateOperator, GraphRagQueryProjection,
GraphRagSchemaContextOptions, HawDBError, HawDBLightningInitialImportCheckpoint,
HawDBLightningInitialImportCutoverCatchUpReport,
HawDBLightningInitialImportDocumentIdentity, HawDBLightningInitialImportReadinessInputs,
KnowledgeCandidateScoringPolicy, KnowledgeRetrievalRequest, LocalQosPolicy, LocalQosState,
NowledgeGraphStatement, ProductionEvidenceBinding, ProductionQualificationIdentity,
RecoveryMode, SearchEmbeddingManifest, SearchIndex, SearchMode, SearchProjectionDelta,
SearchProjectionFreshness, SearchProjectionKind, SearchProjectionProbeOptions,
SearchProjectionRelationalDelta, SearchProjectionRow, StorageOpenTimings,
StorageRecoveryReport, StorageResidencyMode, StorageResourceProfileLimits,
VectorRecallValidationOptions, VectorRecallValidationReport, WorkClass,
PRODUCTION_QUALIFICATION_POLICY_VERSION, VECTOR_RECALL_VALIDATION_PROTOCOL,
};
use std::any::TypeId;
use std::collections::BTreeMap;
use std::sync::mpsc;
use std::thread;
use std::time::Duration;
fn production_identity(canonical_graph_commit_epoch: u64) -> ProductionQualificationIdentity {
ProductionQualificationIdentity {
source_revision: "test-revision".to_string(),
rust_toolchain: "test-toolchain".to_string(),
target_os: std::env::consts::OS.to_string(),
target_arch: std::env::consts::ARCH.to_string(),
enabled_features: vec!["vector-search".to_string()],
durable_format_version: 1,
schema_version: 1,
configuration_digest: "test-configuration".to_string(),
deployment_profile: "test".to_string(),
dataset_fingerprint: "test-dataset".to_string(),
canonical_graph_commit_epoch,
policy_version: PRODUCTION_QUALIFICATION_POLICY_VERSION,
}
}
fn ready_vector_recall_report() -> VectorRecallValidationReport {
VectorRecallValidationReport {
protocol: VECTOR_RECALL_VALIDATION_PROTOCOL.to_string(),
ready: true,
approximate_backend: "hawdb_rabitq_candidate_projection".to_string(),
sample_candidate_count: 2,
requested_sample_count: 2,
executed_sample_count: 2,
top_k: 1,
candidate_limit: 1,
minimum_recall_per_million: 950_000,
exact_hit_count: 2,
candidate_hit_count: 2,
candidate_overlap_count: 2,
candidate_recall_at_k_per_million: 1_000_000,
approximate_hit_count: 2,
overlap_count: 2,
recall_at_k_per_million: 1_000_000,
overlap_at_k_per_million: 1_000_000,
fallback_count: 0,
index_coverage_incomplete_count: 0,
average_filter_selectivity_per_million: 1_000_000,
max_filter_selectivity_per_million: 1_000_000,
blocker_codes: Vec::new(),
}
}
#[test]
fn graph_config_tracks_shadow_vs_cutover_mode() {
assert!(nowledge_mem_graph_config(NowledgeMemGraphMode::ShadowReadOnly).read_only);
assert!(!nowledge_mem_graph_config(NowledgeMemGraphMode::WritableCutover).read_only);
assert_eq!(
nowledge_mem_graph_config(NowledgeMemGraphMode::ShadowReadOnly)
.compressed_vector_search_mode,
CompressedVectorSearchMode::Disabled
);
assert_eq!(
nowledge_mem_graph_config_with_search_mode(
NowledgeMemGraphMode::ShadowReadOnly,
CompressedVectorSearchMode::Required,
)
.compressed_vector_search_mode,
CompressedVectorSearchMode::Required
);
}
#[test]
fn serving_path_readiness_reexports_the_readiness_owner_contract() {
assert_eq!(
TypeId::of::<NowledgeMemServingPathReadiness>(),
TypeId::of::<hawdb_readiness::nowledge_mem_serving_path::NowledgeMemServingPathReadiness>(
)
);
assert_eq!(
TypeId::of::<NowledgeMemServingEntrypoint>(),
TypeId::of::<hawdb_readiness::nowledge_mem_serving_path::NowledgeMemServingEntrypoint>(
)
);
}
#[test]
fn graph_facade_executes_cypher_through_library_api() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-1', title: 'Library seam'})")
.unwrap();
let output = graph
.query("MATCH (m:Memory {id: 'mem-1'}) RETURN m.title AS title")
.unwrap();
assert_eq!(output.rows.len(), 1);
assert_eq!(graph.mode(), NowledgeMemGraphMode::WritableCutover);
}
#[test]
fn embedded_handle_generates_and_executes_bounded_schema_guided_graph_rag() {
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query(
"CREATE (:Memory {id: 'memory-1'})\
-[:MENTIONS]->(:Entity {id: 'entity-1', name: 'HawDB'})",
)
.unwrap();
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None))
.unwrap();
let context = handle
.graph_rag_schema_context(GraphRagSchemaContextOptions::default())
.unwrap();
let generated = context
.generate_query(&GraphRagQueryDraft {
schema_fingerprint: context.fingerprint,
pattern: GraphRagQueryPattern::Route {
source_label: "Memory".to_string(),
relationship_type: "MENTIONS".to_string(),
target_label: "Entity".to_string(),
},
predicates: vec![GraphRagQueryPredicate {
binding: GraphRagQueryBinding::Source,
property: "id".to_string(),
operator: GraphRagQueryPredicateOperator::Eq,
parameter: Some("memory_id".to_string()),
}],
projections: vec![GraphRagQueryProjection {
binding: GraphRagQueryBinding::Target,
property: "name".to_string(),
alias: "entity_name".to_string(),
}],
limit: 2,
})
.unwrap();
let output = handle
.read_generated_graph_rag(
&generated,
&BTreeMap::from([(
"memory_id".to_string(),
Value::String("memory-1".to_string()),
)]),
&NowledgeMemReadOptions {
max_rows: Some(2),
max_estimated_payload_bytes: Some(256),
},
)
.unwrap();
assert_eq!(
output.output.rows[0].get("entity_name"),
Some(&Value::String("HawDB".to_string()))
);
assert_eq!(output.report.max_rows, Some(2));
assert!(output.report.row_limit_enforced_before_output);
handle
.query_with_report("CREATE (:Source {id: 'source-1'})")
.unwrap();
let error = handle
.read_generated_graph_rag(
&generated,
&BTreeMap::from([(
"memory_id".to_string(),
Value::String("memory-1".to_string()),
)]),
&NowledgeMemReadOptions::default(),
)
.unwrap_err();
assert!(error
.to_string()
.contains("GraphRAG schema context is stale"));
}
#[test]
fn fast_path_classification_uses_ast_shape_not_query_text() {
let compact =
crate::cypher::parse("MATCH (m:Memory {id: $id}) RETURN m.title AS title").unwrap();
let spaced = crate::cypher::parse(
" match ( m : Memory { id : $id } ) return m.title as title ",
)
.unwrap();
let ordered = crate::cypher::parse(
"MATCH (m:Memory {id: $id}) RETURN m.title AS title ORDER BY title LIMIT 1",
)
.unwrap();
let compact_classification = nowledge_mem_fast_path_classification(&compact);
let spaced_classification = nowledge_mem_fast_path_classification(&spaced);
let ordered_classification = nowledge_mem_fast_path_classification(&ordered);
assert_eq!(
compact_classification.execution_path,
NowledgeMemQueryExecutionPath::FastPath
);
assert_eq!(
compact_classification.fast_path_reason,
Some("simple_node_lookup")
);
assert_eq!(compact_classification, spaced_classification);
assert_eq!(
ordered_classification.execution_path,
NowledgeMemQueryExecutionPath::OptimizedPath
);
assert_eq!(ordered_classification.fast_path_reason, None);
}
#[test]
fn graph_query_with_report_marks_simple_lookup_fast_path() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-fast', title: 'Fast path'})")
.unwrap();
let query = graph
.query_with_report("MATCH (m:Memory {id: 'mem-fast'}) RETURN m.title AS title")
.unwrap();
assert_eq!(query.output.rows.len(), 1);
assert_eq!(query.report.protocol, NOWLEDGE_MEM_QUERY_REPORT_PROTOCOL);
assert_eq!(query.report.statement_kind, "match_return");
assert_eq!(
query.report.execution_path,
NowledgeMemQueryExecutionPath::FastPath
);
assert_eq!(
query.report.fast_path_reason.as_deref(),
Some("simple_node_lookup")
);
assert_eq!(query.report.optimizer_decision_count, 0);
assert!(!query.report.physical_plan_captured);
assert!(query.report.physical_operator_counts.is_empty());
assert!(!query.report.slow_log_candidate);
assert_eq!(query.report.json()["execution_path"], "fast_path");
assert_eq!(query.report.json()["statement_kind"], "match_return");
assert_eq!(query.report.json()["fast_path_selected"], true);
assert_eq!(query.report.json()["physical_plan_captured"], false);
assert_eq!(query.report.optimizer_rule_event_count, 0);
assert_eq!(query.report.json()["optimizer_rule_event_count"], 0);
assert_eq!(query.report.json()["plan_cache"]["cacheable"], true);
assert_eq!(query.report.output_row_shape.row_count, 1);
assert_eq!(query.report.output_row_shape.column_count, 1);
assert_eq!(query.report.output_row_shape.columns, vec!["title"]);
assert_eq!(query.report.json()["output_row_shape"]["row_count"], 1);
assert_eq!(
query.report.json()["output_row_shape"]["columns"],
serde_json::json!(["title"])
);
assert!(
query
.report
.api_behavior
.include_metadata_false_strips_metadata
);
assert!(query.report.api_behavior.ordering_contract_recorded);
assert!(query.report.api_behavior.pagination_contract_recorded);
assert!(query.report.api_behavior.error_class_stable);
assert!(!query.report.api_behavior.statement_has_ordering);
assert!(!query.report.api_behavior.statement_has_pagination);
assert_eq!(
query.report.json()["api_behavior"]["error_class_stable"],
true
);
}
#[test]
fn graph_query_with_report_keeps_ordered_scan_on_optimized_path() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-slow-1', title: 'B'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'mem-slow-2', title: 'A'})")
.unwrap();
let query = graph
.query_with_report("MATCH (m:Memory) RETURN m.title AS title ORDER BY title LIMIT 1")
.unwrap();
assert_eq!(query.output.rows.len(), 1);
assert_eq!(
query.report.execution_path,
NowledgeMemQueryExecutionPath::OptimizedPath
);
assert_eq!(query.report.statement_kind, "match_return");
assert_eq!(query.report.fast_path_reason, None);
assert_eq!(query.report.optimizer_decision_count, 0);
assert_eq!(query.report.optimizer_rule_event_count, 0);
assert!(!query.report.physical_plan_captured);
assert!(query.report.physical_operator_counts.is_empty());
assert!(query.report.plan_cache_cacheable);
assert!(query.report.plan_cache_miss);
assert!(!query.report.plan_cache_hit);
assert!(!query.report.plan_cache_bypassed);
assert_eq!(query.report.json()["execution_path"], "optimized_path");
assert_eq!(query.report.json()["fast_path_selected"], false);
assert_eq!(query.report.json()["plan_cache"]["lookup"], "miss");
assert_eq!(query.report.json()["plan_cache"]["cacheable"], true);
assert_eq!(query.report.json()["plan_cache"]["miss"], true);
assert!(query.report.api_behavior.statement_has_ordering);
assert!(query.report.api_behavior.statement_has_pagination);
assert_eq!(
query.report.json()["api_behavior"]["statement_has_ordering"],
true
);
assert_eq!(
query.report.json()["api_behavior"]["statement_has_pagination"],
true
);
}
#[test]
fn graph_query_with_report_can_capture_physical_plan_on_demand() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-plan-1', title: 'B'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'mem-plan-2', title: 'A'})")
.unwrap();
let query = graph
.query_with_params_with_report_options(
"MATCH (m:Memory) RETURN m.title AS title ORDER BY title LIMIT 1",
&BTreeMap::new(),
NowledgeMemQueryReportOptions {
capture_physical_plan: true,
slow_log_threshold_micros: Some(0),
},
)
.unwrap();
assert_eq!(query.output.rows.len(), 1);
assert!(query.report.physical_plan_captured);
assert_eq!(query.report.statement_kind, "match_return");
assert!(query.report.optimizer_decision_count > 0);
assert!(query.report.optimizer_rule_event_count > 0);
assert!(query
.report
.physical_operator_counts
.contains_key("TopNExec"));
assert_eq!(query.report.plan_cache_lookup.as_deref(), Some("miss"));
assert_eq!(query.report.plan_cache_bypass_reason, None);
assert!(query.report.slow_log_candidate);
assert_eq!(query.report.json()["physical_plan_captured"], true);
assert!(query.report.json()["optimizer_rule_event_count"]
.as_u64()
.is_some_and(|count| count > 0));
assert_eq!(query.report.json()["plan_cache_lookup"], "miss");
assert_eq!(query.report.json()["slow_log_candidate"], true);
assert_eq!(
query.report.json()["physical_operator_counts"]["TopNExec"],
1
);
}
#[test]
fn graph_query_with_report_captures_plan_without_extra_cache_lookup() {
let db = Database::new_with_config(DatabaseConfig {
max_plan_cache_entries: Some(8),
..DatabaseConfig::default()
});
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-report-cache-1', title: 'B'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'mem-report-cache-2', title: 'A'})")
.unwrap();
let before = graph.database().plan_cache_stats().unwrap();
let query = graph
.query_with_params_with_report_options(
"MATCH (m:Memory) RETURN m.title AS title ORDER BY title LIMIT 1",
&BTreeMap::new(),
NowledgeMemQueryReportOptions {
capture_physical_plan: true,
slow_log_threshold_micros: None,
},
)
.unwrap();
let after = graph.database().plan_cache_stats().unwrap();
assert_eq!(query.output.rows.len(), 1);
assert!(query.report.physical_plan_captured);
assert_eq!(query.report.plan_cache_lookup.as_deref(), Some("miss"));
assert_eq!(query.report.plan_cache_bypass_reason, None);
assert!(query.report.plan_cache_cacheable);
assert!(query.report.plan_cache_miss);
assert!(!query.report.plan_cache_bypassed);
assert_eq!(after.entries, before.entries + 1);
assert_eq!(after.misses, before.misses + 1);
assert_eq!(after.hits, before.hits);
}
#[test]
fn graph_query_with_report_exposes_storage_scan_pruning() {
let mut db = Database::new();
db.query("CREATE INDEX ON :Memory(kind)").unwrap();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-prune-1', kind: 'note', title: 'Keep'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'mem-prune-2', kind: 'note', title: 'Also keep'})")
.unwrap();
let query = graph
.query_with_report("MATCH (m:Memory) WHERE m.kind = 'note' RETURN m.title AS title")
.unwrap();
assert_eq!(query.output.rows.len(), 2);
assert_eq!(query.report.scan_pruning_reports.len(), 1);
let scan = &query.report.scan_pruning_reports[0];
assert!(scan.pruned);
assert_eq!(scan.candidate_count_before_filter, 2);
assert_eq!(scan.output_count, 2);
assert_eq!(query.report.json()["scan_pruning_report_count"], 1);
assert_eq!(
query.report.json()["scan_pruning_reports"][0]["strategy"]["kind"],
"property_eq"
);
assert_eq!(
query.report.json()["scan_pruning_reports"][0]["strategy"]["property"],
"kind"
);
}
#[test]
fn graph_query_report_exposes_normalized_default_scan_pruning() {
let mut db = Database::new();
db.query("CREATE INDEX ON :Thread(space_id)").unwrap();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Thread {thread_id: 'thread-missing', title: 'Missing space'})")
.unwrap();
graph
.query(
"CREATE (:Thread {thread_id: 'thread-empty', space_id: '', title: 'Empty space'})",
)
.unwrap();
graph
.query("CREATE (:Thread {thread_id: 'thread-default', space_id: 'default', title: 'Default space'})")
.unwrap();
graph
.query("CREATE (:Thread {thread_id: 'thread-team', space_id: 'team', title: 'Team space'})")
.unwrap();
let source_params = BTreeMap::from([(
"source_space_id".to_string(),
Value::String("default".to_string()),
)]);
let source_query = graph
.query_with_params_with_report(
"MATCH (t:Thread) WHERE CASE WHEN t.space_id IS NULL OR t.space_id = '' THEN 'default' ELSE t.space_id END = $source_space_id RETURN t.thread_id AS thread_id",
&source_params,
)
.unwrap();
assert_eq!(source_query.output.rows.len(), 3);
assert_eq!(source_query.report.scan_pruning_reports.len(), 1);
let source_scan = &source_query.report.scan_pruning_reports[0];
assert!(source_scan.pruned);
assert_eq!(source_scan.candidate_count_before_pruning, 4);
assert_eq!(source_scan.candidate_count_before_filter, 3);
assert_eq!(source_scan.pruned_candidate_count, 1);
assert_eq!(
source_query.report.json()["scan_pruning_reports"][0]["strategy"]["kind"],
"property_default_if_null_eq"
);
assert_eq!(
source_query.report.json()["scan_pruning_reports"][0]["strategy"]["property"],
"space_id"
);
let target_params = BTreeMap::from([(
"target_space_id".to_string(),
Value::String("default".to_string()),
)]);
let target_query = graph
.query_with_params_with_report(
"MATCH (t:Thread) WHERE CASE WHEN t.space_id IS NULL OR t.space_id = '' THEN 'default' ELSE t.space_id END <> $target_space_id RETURN t.thread_id AS thread_id",
&target_params,
)
.unwrap();
assert_eq!(target_query.output.rows.len(), 1);
assert_eq!(target_query.report.scan_pruning_reports.len(), 1);
let target_scan = &target_query.report.scan_pruning_reports[0];
assert!(target_scan.pruned);
assert_eq!(target_scan.candidate_count_before_pruning, 4);
assert_eq!(target_scan.candidate_count_before_filter, 1);
assert_eq!(target_scan.pruned_candidate_count, 3);
assert_eq!(
target_query.report.json()["scan_pruning_reports"][0]["strategy"]["kind"],
"property_default_if_null_not_eq"
);
assert_eq!(
target_query.report.json()["scan_pruning_reports"][0]["strategy"]["property"],
"space_id"
);
}
#[test]
fn graph_query_with_report_keeps_system_statement_out_of_plan_cache() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let query = graph
.query_with_report("SET system.work_priority = 'background'")
.unwrap();
assert_eq!(query.output.rows.len(), 1);
assert_eq!(query.report.statement_kind, "set_system_variable");
assert_eq!(
query.report.execution_path,
NowledgeMemQueryExecutionPath::OptimizedPath
);
assert_eq!(query.report.plan_cache_lookup, None);
assert_eq!(query.report.plan_cache_bypass_reason, None);
assert!(!query.report.plan_cache_cacheable);
assert!(!query.report.plan_cache_hit);
assert!(!query.report.plan_cache_miss);
assert!(!query.report.plan_cache_bypassed);
assert_eq!(
query.report.json()["plan_cache"]["lookup"],
serde_json::Value::Null
);
assert_eq!(query.report.json()["plan_cache"]["cacheable"], false);
}
#[test]
fn embedded_store_exposes_redacted_typed_slow_query_report() {
let db = Database::new_with_config(DatabaseConfig {
slow_query_log_threshold_micros: 0,
slow_query_log_capacity: 4,
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE (:Memory {id: 'slow-secret-id', title: 'Slow Secret'})")
.unwrap();
store
.query_with_report("MATCH (m:Memory {id: 'slow-secret-id'}) RETURN m.title AS title")
.unwrap();
let report = store.slow_query_report();
let json = report.json();
let encoded = json.to_string();
assert_eq!(report.protocol, NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL);
assert_eq!(report.mode, NowledgeMemGraphMode::ShadowReadOnly);
assert!(report.present);
assert!(report.ready);
assert_eq!(report.capacity, 4);
assert_eq!(report.threshold_micros, 0);
assert_eq!(report.record_count, 2);
assert_eq!(report.latest_sequence, Some(2));
assert_eq!(report.records.len(), 2);
assert!(report.records.iter().all(|record| record.success));
assert!(report
.records
.iter()
.all(|record| record.slow_log_candidate));
assert_eq!(json["protocol"], NOWLEDGE_MEM_SLOW_QUERY_REPORT_PROTOCOL);
assert_eq!(json["record_count"], 2);
assert_eq!(json["redaction"]["query_text_copied"], false);
assert_eq!(json["redaction"]["parameters_copied"], false);
assert!(json["records"][0].get("query_digest").is_some());
assert!(!encoded.contains("Slow Secret"));
assert!(!encoded.contains("slow-secret-id"));
assert!(!encoded.contains("MATCH (m:Memory"));
}
#[test]
fn embedded_store_handle_supports_shared_read_and_observability_access() {
let db = Database::new_with_config(DatabaseConfig {
max_plan_cache_entries: Some(8),
slow_query_log_threshold_micros: 0,
slow_query_log_capacity: 8,
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE (:Memory {id: 'shared-1', title: 'Shared State'})")
.unwrap();
let handle = NowledgeMemEmbeddedStoreHandle::new(store).unwrap();
let reader = {
let handle = handle.clone();
thread::spawn(move || {
for _ in 0..4 {
let query = handle
.read_query(
"MATCH (m:Memory {id: 'shared-1'}) RETURN m.title AS title",
&NowledgeMemReadOptions::default(),
)
.unwrap();
assert_eq!(query.output.rows.len(), 1);
assert!(query.report.row_limit_enforced_before_output);
}
})
};
let observer = {
let handle = handle.clone();
thread::spawn(move || {
for _ in 0..4 {
let slow_query = handle.slow_query_report().unwrap();
assert!(slow_query.ready);
assert!(slow_query.record_count <= slow_query.capacity);
let dashboard = handle
.readiness_dashboard(&NowledgeMemReadinessOptions::default())
.unwrap();
assert!(readiness_dashboard_area(&dashboard, "slow_query").ready);
assert!(dashboard.slow_query_record_count <= slow_query.capacity);
}
})
};
reader.join().unwrap();
observer.join().unwrap();
let final_slow_query = handle.slow_query_report().unwrap();
assert!(final_slow_query.ready);
assert_eq!(final_slow_query.latest_sequence, Some(1));
assert_eq!(final_slow_query.record_count, 1);
}
#[test]
fn write_guard_panic_poisons_reads_without_discarding_last_good_snapshot() {
let db = Database::new_with_config(DatabaseConfig::default());
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE (:Memory {id: 'panic-guard', title: 'Before'})")
.unwrap();
let handle = NowledgeMemEmbeddedStoreHandle::new(store).unwrap();
handle
.with_read_transaction(4096, |_transaction| Ok(()))
.unwrap();
let panicking_handle = handle.clone();
let panicked = std::panic::catch_unwind(std::panic::AssertUnwindSafe(move || {
let _guard = panicking_handle.write_store().unwrap();
panic!("simulated write failure inside the guard");
}));
assert!(panicked.is_err());
assert!(handle.published_read.lock().unwrap().is_some());
assert!(handle
.with_read_transaction(4096, |_transaction| Ok(()))
.is_err());
}
#[test]
fn embedded_store_exposes_typed_operations_readiness_report() {
let db = Database::new_with_config(DatabaseConfig {
slow_query_log_threshold_micros: 0,
slow_query_log_capacity: 4,
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE (:Memory {id: 'ops-1', title: 'Operations'})")
.unwrap();
let report = store
.operations_readiness(&NowledgeMemReadinessOptions::default())
.unwrap();
let json = report.json();
let encoded = json.to_string();
assert_eq!(report.protocol, NOWLEDGE_MEM_OPERATIONS_READINESS_PROTOCOL);
assert!(report.present);
assert!(!report.ready);
assert!(report.graph_open);
assert!(!report.graph_read_only);
assert!(!report.search_projection_open);
assert_eq!(report.graph_commit_epoch, 1);
assert_eq!(report.projection_commit_lag, 1);
assert!(!report.projection_stale);
assert_eq!(
report.storage_lifecycle_action,
NowledgeMemStorageLifecycleActionKind::OpenReadOnlyInspect
);
assert!(!report.storage_lifecycle_ready);
assert!(!report.storage_recovery_ready);
assert!(report.slow_query_ready);
assert!(report.background_maintenance_ready);
assert!(report
.blocker_codes
.contains(&"storage_recovery_not_ready".to_string()));
assert!(report
.blocker_codes
.contains(&"storage_lifecycle.storage_not_durable".to_string()));
assert_eq!(json["protocol"], NOWLEDGE_MEM_OPERATIONS_READINESS_PROTOCOL);
assert_eq!(
json["storage_lifecycle"]["action"],
"open_read_only_inspect"
);
assert_eq!(json["readiness"]["storage_lifecycle_ready"], false);
assert_eq!(
json["storage_lifecycle_decision"]["protocol"],
NOWLEDGE_MEM_STORAGE_LIFECYCLE_DECISION_PROTOCOL
);
assert_eq!(json["redaction"]["query_text_copied"], false);
assert!(!encoded.contains("Operations"));
assert!(!encoded.contains("ops-1"));
}
#[test]
fn embedded_store_operations_readiness_reports_projection_staleness() {
let root = unique_nowledge_mem_test_dir("operations_readiness_projection_stale");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'ops-stale', title: 'Projection stale'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let stale = store
.operations_readiness(&NowledgeMemReadinessOptions::default())
.unwrap();
assert!(stale.search_projection_open);
assert_eq!(stale.projection_commit_lag, 1);
assert!(stale.projection_stale);
assert!(stale
.blocker_codes
.contains(&"search_projection_stale".to_string()));
store.catch_up_search_projection(16, 1).unwrap();
let caught_up = store
.operations_readiness(&NowledgeMemReadinessOptions::default())
.unwrap();
assert!(caught_up.search_projection_open);
assert_eq!(caught_up.projection_commit_lag, 0);
assert!(!caught_up.projection_stale);
assert!(!caught_up
.blocker_codes
.contains(&"search_projection_stale".to_string()));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_supports_operations_readiness_with_shared_reads() {
let db = Database::new_with_config(DatabaseConfig {
slow_query_log_threshold_micros: 0,
slow_query_log_capacity: 8,
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE (:Memory {id: 'ops-shared', title: 'Shared Operations'})")
.unwrap();
let handle = NowledgeMemEmbeddedStoreHandle::new(store);
let reader = {
let handle = handle.clone().unwrap();
thread::spawn(move || {
let read = handle
.read_query(
"MATCH (m:Memory {id: 'ops-shared'}) RETURN m.title AS title",
&NowledgeMemReadOptions::default(),
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
})
};
let observer = {
let handle = handle.clone().unwrap();
thread::spawn(move || {
let report = handle
.operations_readiness(&NowledgeMemReadinessOptions::default())
.unwrap();
assert!(report.slow_query_ready);
assert!(report.background_maintenance_ready);
})
};
reader.join().unwrap();
observer.join().unwrap();
}
#[test]
fn embedded_store_handle_allows_overlapping_read_guards() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None));
let (acquired_tx, acquired_rx) = mpsc::channel();
let (release_tx, release_rx) = mpsc::channel();
let first = {
let handle = handle.clone().unwrap();
let acquired_tx = acquired_tx.clone();
thread::spawn(move || {
let _guard = handle.read_store().unwrap();
acquired_tx.send(()).unwrap();
release_rx.recv().unwrap();
})
};
acquired_rx.recv_timeout(Duration::from_secs(1)).unwrap();
let second = {
let handle = handle.clone().unwrap();
thread::spawn(move || {
let _guard = handle.read_store().unwrap();
acquired_tx.send(()).unwrap();
})
};
acquired_rx.recv_timeout(Duration::from_secs(1)).unwrap();
release_tx.send(()).unwrap();
first.join().unwrap();
second.join().unwrap();
}
#[test]
fn embedded_store_exposes_typed_query_runtime_preflight() {
let db = Database::new_with_config(DatabaseConfig {
max_plan_cache_entries: Some(8),
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report("CREATE INDEX ON :Memory(id)")
.unwrap();
store
.query_with_report("CREATE (:Memory {id: 'preflight-1', title: 'Preflight'})")
.unwrap();
for id in 0..8 {
store
.query_with_report(&format!(
"CREATE (:Memory {{id: 'preflight-filler-{id}', title: 'Filler {id}'}})"
))
.unwrap();
}
let probes = REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES
.iter()
.map(|route| {
NowledgeQueryRuntimePreflightProbe::new(
format!("probe:{route}"),
"MATCH (m:Memory {id: 'preflight-1'}) RETURN m.title AS title",
)
.with_route(*route)
.with_query_family(super::nowledge_mem_required_query_families_for_route(route)[0])
.require_scan_pruning(1)
.require_pruned()
.with_max_output_rows(1)
})
.collect::<Vec<_>>();
let report = store.query_runtime_preflight(&probes);
let json = report.json();
assert_eq!(report.protocol, NOWLEDGE_QUERY_RUNTIME_PREFLIGHT_PROTOCOL);
assert!(report.ready);
assert!(report.database_opened);
assert!(report.redaction.ready());
assert!(!report.redaction.rows_copied);
assert!(!report.redaction.parameters_copied);
assert!(!report.redaction.local_paths_copied);
assert!(!report.redaction.raw_errors_copied);
assert_eq!(
report.probe_count,
REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES.len()
);
assert_eq!(report.passed_probe_count, report.probe_count);
assert_eq!(report.failed_probe_count, 0);
assert!(report.required_routes_covered);
assert!(report.route_coverage_ready);
assert!(report.blocker_codes.is_empty());
assert!(report.probes.iter().all(|probe| probe.ready));
assert!(report
.probes
.iter()
.all(|probe| probe.selected_plan_fingerprint.is_some()));
assert!(report
.probes
.iter()
.all(|probe| !probe.scan_pruning_reports.is_empty()));
assert_eq!(json["protocol"], NOWLEDGE_QUERY_RUNTIME_PREFLIGHT_PROTOCOL);
assert_eq!(json["ready"], true);
assert_eq!(json["redaction"]["ready"], true);
assert_eq!(json["redaction"]["rows_copied"], false);
assert_eq!(json["redaction"]["parameters_copied"], false);
assert_eq!(json["redaction"]["local_paths_copied"], false);
assert_eq!(json["redaction"]["raw_errors_copied"], false);
assert_eq!(json["probe_count"], probes.len());
assert_eq!(json["probes"][0]["output_row_count"], 1);
assert_eq!(
json["probes"][0]["execution_profile"]["scan_pruning_report_count"],
1
);
assert!(json["probes"][0]["selected_plan_fingerprint"]
.as_str()
.is_some_and(|fingerprint| fingerprint.contains("IndexNodeSeek")));
}
#[test]
fn embedded_store_query_runtime_preflight_redacts_failed_probe_values() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
let probe = NowledgeQueryRuntimePreflightProbe::new(
"probe:/graph/node-details/{node_id}",
"MATCH (m:Memory {id: 'secret-preflight-id'}) RETURN missing(",
)
.with_route("/graph/node-details/{node_id}")
.with_query_family("memory_lookup");
let report = store.query_runtime_preflight(&[probe]);
let json = report.json();
let encoded = json.to_string();
assert!(!report.ready);
assert!(report.redaction.ready());
assert_eq!(report.failed_probe_count, 1);
assert_eq!(
report.probes[0].blocker_codes,
vec!["query_runtime_failed".to_string()]
);
assert_eq!(json["probes"][0]["success"], false);
assert_eq!(json["probes"][0]["error_class"], "parse");
assert_eq!(json["redaction"]["ready"], true);
assert_eq!(json["redaction"]["parameters_copied"], false);
assert_eq!(json["redaction"]["raw_errors_copied"], false);
assert!(!encoded.contains("secret-preflight-id"));
assert!(!encoded.contains("RETURN missing"));
}
#[test]
fn graph_read_query_reports_bounded_payload() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-read', title: 'Bounded read'})")
.unwrap();
let read = graph
.read_query_with_options(
"MATCH (m:Memory {id: 'mem-read'}) RETURN m.title AS title",
&NowledgeMemReadOptions {
max_rows: Some(4),
max_estimated_payload_bytes: Some(512),
},
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
assert_eq!(read.report.protocol, NOWLEDGE_MEM_READ_REPORT_PROTOCOL);
assert_eq!(read.report.mode, NowledgeMemGraphMode::ShadowReadOnly);
assert_eq!(read.report.row_count, 1);
assert_eq!(read.report.max_rows, Some(4));
assert_eq!(read.report.execution_row_cap, Some(5));
assert!(read.report.estimated_payload_bytes <= 512);
assert!(!read.report.row_budget_exceeded);
assert!(!read.report.payload_budget_exceeded);
assert!(read.report.row_limit_enforced_before_output);
assert!(read.report.operator_row_cap_enabled);
assert_eq!(read.report.blocking_operator_count, 0);
assert!(read.report.blocking_operator_kinds.is_empty());
assert!(read.report.intermediate_rows >= read.report.row_count);
assert!(read.report.intermediate_payload_bytes >= read.report.output_payload_bytes);
assert!(read.report.output_payload_bytes > 0);
assert!(read.report.steady_resident_bytes.is_some());
assert!(read.report.peak_resident_bytes.is_some());
assert!(read.report.total_page_faults.is_some());
assert_eq!(read.report.minor_page_faults.is_some(), cfg!(unix));
assert_eq!(read.report.major_page_faults.is_some(), cfg!(unix));
assert!(!read.report.streaming);
assert_eq!(read.report.json()["execution_row_cap"], 5);
assert_eq!(read.report.json()["row_limit_enforced_before_output"], true);
assert_eq!(read.report.json()["operator_row_cap_enabled"], true);
assert_eq!(read.report.json()["blocking_operator_count"], 0);
assert!(read.report.json()["intermediate_rows"].as_u64().unwrap() >= 1);
assert!(read.report.json()["output_payload_bytes"].as_u64().unwrap() > 0);
assert!(
read.report.json()["steady_resident_bytes"]
.as_u64()
.unwrap()
> 0
);
assert!(read.report.json()["peak_resident_bytes"].as_u64().unwrap() > 0);
assert_eq!(read.report.json()["streaming"], false);
assert_eq!(
read.report.bounded_read_evidence_json()["protocol"],
NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL
);
assert_eq!(
read.report.bounded_read_evidence_json()["mode"],
"shadow_read_only"
);
assert_eq!(read.report.bounded_read_evidence_json()["ready"], false);
assert_eq!(
read.report.bounded_read_evidence_json()["blocker_codes"],
serde_json::json!(["missing_covered_routes", "graph_route_readiness_missing"])
);
let covered_routes = full_bounded_read_routes();
let route_readiness = ready_route_readiness_summary();
let evidence = nowledge_mem_bounded_read_evidence_json_with_route_readiness(
&read.report,
&covered_routes,
Some(&route_readiness),
);
assert_eq!(evidence["ready"], true);
assert_eq!(
evidence["covered_routes"],
serde_json::json!(covered_routes)
);
assert_eq!(evidence["missing_covered_routes"], serde_json::json!([]));
}
#[test]
fn graph_queries_hold_and_release_runtime_governor_permits() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'admitted-mem'})")
.unwrap();
let read = graph
.read_query_with_options(
"MATCH (m:Memory {id: 'admitted-mem'}) RETURN m.id AS id",
&NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(1024),
},
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
let snapshot = graph.runtime_governor_snapshot();
assert_eq!(snapshot.admissions, 2);
assert_eq!(snapshot.completions, 2);
assert_eq!(snapshot.active_foreground_tasks, 0);
assert_eq!(snapshot.active_cpu_slots, 0);
assert_eq!(snapshot.admitted_memory_bytes, 0);
}
#[test]
fn graph_query_rejects_before_mutation_when_runtime_memory_is_unavailable() {
let governor = hawdb_qos::RuntimeGovernor::detect(
hawdb_qos::RuntimeGovernorConfig {
memory_budget_bytes: Some(1),
..hawdb_qos::RuntimeGovernorConfig::default()
},
hawdb_qos::IoConcurrencyBudget::new(1, 1),
);
let mut graph = NowledgeMemGraph::from_database_with_runtime_governor(
Database::new(),
NowledgeMemGraphMode::WritableCutover,
governor,
);
let error = graph
.query("CREATE (:Memory {id: 'rejected-mem'})")
.unwrap_err();
assert!(error
.to_string()
.contains("runtime admission memory_saturated"));
assert!(graph
.database_mut()
.query("MATCH (m:Memory) RETURN m.id AS id")
.unwrap()
.rows
.is_empty());
let snapshot = graph.runtime_governor_snapshot();
assert_eq!(snapshot.admissions, 0);
assert_eq!(snapshot.completions, 0);
assert_eq!(snapshot.admission_rejections, 1);
}
#[test]
fn embedded_handle_transaction_rejects_before_mutation_without_runtime_memory() {
let governor = hawdb_qos::RuntimeGovernor::detect(
hawdb_qos::RuntimeGovernorConfig {
memory_budget_bytes: Some(1),
..hawdb_qos::RuntimeGovernorConfig::default()
},
hawdb_qos::IoConcurrencyBudget::new(2, 1),
);
let graph = NowledgeMemGraph::from_database_with_runtime_governor(
Database::new(),
NowledgeMemGraphMode::WritableCutover,
governor,
);
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None))
.unwrap();
let error = handle
.transaction(&[NowledgeGraphStatement {
cypher: "CREATE (:Memory {id: 'transaction-rejected'})".to_string(),
parameters: BTreeMap::new(),
}])
.unwrap_err();
assert!(error.to_string().contains("runtime admission"));
assert_eq!(handle.runtime_governor_snapshot().unwrap().admissions, 0);
let mut store = handle.write_store().unwrap();
let output = store
.graph_mut()
.database_mut()
.query("MATCH (m:Memory) RETURN m.id AS id")
.unwrap();
assert!(output.rows.is_empty());
}
#[test]
fn embedded_handle_query_transaction_commits_graph_and_relational_state_once() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None))
.unwrap();
handle
.with_transaction(|transaction| {
transaction.query("CREATE (:Memory {id: 'memory-1'})")?;
let graph_rows = transaction
.query("MATCH (m:Memory {id: 'memory-1'}) RETURN m.id AS id")?
.rows;
assert_eq!(graph_rows.len(), 1);
transaction.query_sql(
"CREATE TABLE anchors (\
anchor_id TEXT PRIMARY KEY,\
memory_id TEXT NOT NULL\
)",
)?;
transaction.query_sql_with_params(
"INSERT INTO anchors (anchor_id, memory_id) VALUES ($1, $2)",
&[
Value::String("anchor-1".to_string()),
Value::String("memory-1".to_string()),
],
)?;
Ok(())
})
.unwrap();
assert_eq!(handle.runtime_status().unwrap().graph_commit_epoch, 1);
let mut store = handle.write_store().unwrap();
assert_eq!(
store
.graph_mut()
.database_mut()
.query("MATCH (m:Memory) RETURN m.id AS id")
.unwrap()
.rows
.len(),
1
);
assert_eq!(
store
.graph_mut()
.database_mut()
.query_sql("SELECT anchor_id, memory_id FROM anchors")
.unwrap()
.rows
.len(),
1
);
}
#[test]
fn embedded_handle_query_transaction_rolls_back_callback_errors() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None))
.unwrap();
let error = handle
.with_transaction(|transaction| {
transaction.query("CREATE (:Memory {id: 'rolled-back'})")?;
Err::<(), _>(crate::HawDBError::Execution(
"injected callback failure".to_string(),
))
})
.unwrap_err();
assert!(error.to_string().contains("injected callback failure"));
assert_eq!(handle.runtime_status().unwrap().graph_commit_epoch, 0);
let mut store = handle.write_store().unwrap();
assert!(store
.graph_mut()
.database_mut()
.query("MATCH (m:Memory) RETURN m.id AS id")
.unwrap()
.rows
.is_empty());
}
#[test]
fn embedded_handle_checkpoints_through_admitted_maintenance() {
let root = unique_nowledge_mem_test_dir("embedded_handle_checkpoint");
let graph_path = root.join("graph");
let options =
NowledgeMemOpenOptions::graph_only(&graph_path, NowledgeMemGraphMode::WritableCutover);
let (handle, _) = NowledgeMemEmbeddedStoreHandle::open_with_options(options).unwrap();
handle
.query_with_report("CREATE (:Memory {id: 'checkpointed'})")
.unwrap();
let before = handle.runtime_governor_snapshot().unwrap();
handle.checkpoint().unwrap();
let after = handle.runtime_governor_snapshot().unwrap();
assert_eq!(after.admissions, before.admissions + 1);
assert_eq!(after.completions, before.completions + 1);
assert_eq!(after.active_background_tasks, 0);
assert_eq!(after.active_blocking_tasks, 0);
assert_eq!(after.active_cpu_slots, 0);
assert_eq!(after.active_background_io_slots, 0);
assert_eq!(after.admitted_memory_bytes, 0);
drop(handle);
let options =
NowledgeMemOpenOptions::graph_only(&graph_path, NowledgeMemGraphMode::ShadowReadOnly);
let (handle, _) = NowledgeMemEmbeddedStoreHandle::open_with_options(options).unwrap();
let output = handle
.query_with_report("MATCH (m:Memory {id: 'checkpointed'}) RETURN m.id AS id")
.unwrap();
assert_eq!(output.output.rows.len(), 1);
drop(handle);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_handle_search_holds_governed_io_and_result_budget() {
let governor = hawdb_qos::RuntimeGovernor::detect(
hawdb_qos::RuntimeGovernorConfig {
memory_budget_bytes: Some(256 * 1024 * 1024),
result_budget_bytes: 64 * 1024 * 1024,
..hawdb_qos::RuntimeGovernorConfig::default()
},
hawdb_qos::IoConcurrencyBudget::new(1, 1),
);
let graph = NowledgeMemGraph::from_database_with_runtime_governor(
Database::new(),
NowledgeMemGraphMode::WritableCutover,
governor,
);
let mut projection = NowledgeMemSearchProjection::from_index(SearchIndex::default());
projection.set_range_read_config(crate::SearchRangeReadConfig::new(
std::num::NonZeroUsize::MIN,
std::num::NonZeroU64::new(1024).unwrap(),
));
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let output = handle
.search_candidates_with_report(&NowledgeMemSearchCandidateRequest::text("empty", 4))
.unwrap();
assert!(output.result.hits.is_empty());
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.admissions, 1);
assert_eq!(snapshot.completions, 1);
assert_eq!(snapshot.active_foreground_io_slots, 0);
assert_eq!(snapshot.admitted_memory_bytes, 0);
}
#[test]
fn embedded_handle_charges_out_of_core_buffers_to_runtime_admission() {
let root = unique_nowledge_mem_test_dir("out_of_core_runtime_admission");
let search_path = root.join("search");
{
let mut index = SearchIndex::open(&search_path).unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "admission".to_string(),
title: "Out of core admission".to_string(),
body: "Bounded buffers must be admitted".to_string(),
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
})
.unwrap();
index.checkpoint().unwrap();
}
let governor = hawdb_qos::RuntimeGovernor::detect(
hawdb_qos::RuntimeGovernorConfig {
memory_budget_bytes: Some(256 * 1024 * 1024),
result_budget_bytes: 64 * 1024 * 1024,
..hawdb_qos::RuntimeGovernorConfig::default()
},
hawdb_qos::IoConcurrencyBudget::new(1, 1),
);
let graph = NowledgeMemGraph::from_database_with_runtime_governor(
Database::new(),
NowledgeMemGraphMode::ShadowReadOnly,
governor,
);
let projection = NowledgeMemOutOfCoreSearchProjection::open(&search_path).unwrap();
let handle = NowledgeMemEmbeddedStoreHandle::new(
NowledgeMemEmbeddedStore::new_with_out_of_core_search(
graph,
projection,
NowledgeMemRetrievalProjectionAdvisor::default(),
),
)
.unwrap();
let error = handle
.search_candidates(&NowledgeMemSearchCandidateRequest::text("admission", 1))
.unwrap_err();
assert!(error
.to_string()
.contains("runtime admission memory_saturated"));
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.admissions, 0);
assert_eq!(snapshot.admission_rejections, 1);
drop(handle);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_clamps_search_io_depth_to_runtime_governor() {
let governor = hawdb_qos::RuntimeGovernor::detect(
hawdb_qos::RuntimeGovernorConfig {
memory_budget_bytes: Some(256 * 1024 * 1024),
result_budget_bytes: 64 * 1024 * 1024,
..hawdb_qos::RuntimeGovernorConfig::default()
},
hawdb_qos::IoConcurrencyBudget::new(2, 1),
);
let graph = NowledgeMemGraph::from_database_with_runtime_governor(
Database::new(),
NowledgeMemGraphMode::WritableCutover,
governor,
);
let mut projection = NowledgeMemSearchProjection::from_index(SearchIndex::default());
projection.set_range_read_config(crate::SearchRangeReadConfig::new(
std::num::NonZeroUsize::new(4).unwrap(),
std::num::NonZeroU64::new(1024).unwrap(),
));
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
assert_eq!(
store
.search_projection()
.unwrap()
.index()
.range_read_config()
.io_depth
.get(),
2
);
}
#[test]
fn embedded_handle_read_transaction_runs_against_one_admitted_snapshot() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let handle =
NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(graph, None))
.unwrap();
handle
.query_with_report("CREATE (:Memory {id: 'memory-1'})")
.unwrap();
let (epoch, rows, memory_budget) = handle
.with_read_transaction(64 * 1024, |transaction| {
let epoch = transaction.commit_epoch();
let output = transaction.query_with_params_bounded_profile(
"MATCH (m:Memory) RETURN m.id AS id LIMIT 2",
&BTreeMap::new(),
Some(2),
)?;
let memory_budget = output
.execution_profile
.pipeline_memory_report
.query_memory_budget_bytes;
Ok((epoch, output.output.rows, memory_budget))
})
.unwrap();
assert_eq!(epoch, 1);
assert_eq!(rows.len(), 1);
assert_eq!(memory_budget, 256 * 1024 * 1024);
assert_eq!(
rows[0].get("id"),
Some(&Value::String("memory-1".to_string()))
);
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.active_foreground_tasks, 0);
assert_eq!(snapshot.admitted_memory_bytes, 0);
}
#[test]
fn graph_streaming_query_reports_pre_execution_cancellation() {
let mut db = Database::new();
db.query("CREATE (:Memory {id: 'cancelled-mem'})").unwrap();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let cancellation = hawdb_core::RuntimeCancellationToken::new();
cancellation.cancel();
let context = hawdb_core::RuntimeTaskContext::without_deadline(cancellation);
let error = graph
.read_query_with_params_streaming_context(
"MATCH (m:Memory) RETURN m.id AS id",
&BTreeMap::new(),
&NowledgeMemReadOptions::default(),
&context,
|_| Ok(()),
)
.unwrap_err();
assert!(error.to_string().contains("runtime task cancelled"));
let snapshot = graph.runtime_governor_snapshot();
assert_eq!(snapshot.admissions, 0);
assert_eq!(snapshot.cancellations, 1);
}
#[test]
fn graph_read_query_streaming_collect_enforces_payload_before_host_ownership() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-stream', title: 'streamed payload'})")
.unwrap();
let query = "MATCH (m:Memory {id: 'mem-stream'}) RETURN m.title AS title";
let read = graph
.read_query_with_params_streaming_collect(
query,
&BTreeMap::new(),
&NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(1024),
},
)
.unwrap();
assert!(read.report.streaming);
assert_eq!(read.report.row_count, 1);
assert_eq!(
read.output.rows[0].get("title"),
Some(&Value::String("streamed payload".to_string()))
);
let error = graph
.read_query_with_params_streaming_collect(
query,
&BTreeMap::new(),
&NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(1),
},
)
.unwrap_err();
assert!(error.to_string().contains("max_payload_bytes 1"));
}
#[test]
fn graph_read_query_rejects_payload_budget_excess() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-large', title: 'Large read payload'})")
.unwrap();
let error = graph
.read_query_with_options(
"MATCH (m:Memory {id: 'mem-large'}) RETURN m.title AS title",
&NowledgeMemReadOptions {
max_rows: Some(4),
max_estimated_payload_bytes: Some(4),
},
)
.unwrap_err();
assert!(error
.to_string()
.contains("exceeding max_estimated_payload_bytes 4"));
assert!(matches!(error, HawDBError::ReadBudgetExceeded(cause)
if cause.resource == ReadBudgetResource::PayloadBytes && cause.limit == 4));
}
#[test]
fn legacy_read_error_context_preserves_typed_budget_and_unknown_errors() {
let options = NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(4),
};
let typed = legacy_nowledge_mem_read_error(
HawDBError::read_budget_exceeded(
ReadBudgetResource::PayloadBytes,
4,
"diagnostic without budget keywords",
),
&options,
);
assert!(matches!(&typed, HawDBError::ReadBudgetExceeded(cause)
if cause.resource == ReadBudgetResource::PayloadBytes && cause.limit == 4));
assert!(typed.to_string().contains("max_estimated_payload_bytes 4"));
let unknown = HawDBError::Execution("unrelated max_payload_bytes 4 diagnostic".into());
assert_eq!(
legacy_nowledge_mem_read_error(unknown.clone(), &options),
unknown
);
let rows = HawDBError::read_budget_exceeded(ReadBudgetResource::Rows, 1, "rows");
assert_eq!(legacy_nowledge_mem_read_error(rows.clone(), &options), rows);
}
#[test]
fn read_transaction_rejects_rows_above_configured_limit() {
let mut db = Database::new_with_config(DatabaseConfig {
max_read_result_rows: Some(1),
..DatabaseConfig::default()
});
db.query("CREATE (:Memory {id: 'mem-limit-1', title: 'Limit one'})")
.unwrap();
db.query("CREATE (:Memory {id: 'mem-limit-2', title: 'Limit two'})")
.unwrap();
let error = db
.begin_read_transaction()
.unwrap()
.query("MATCH (m:Memory) RETURN m.id AS id")
.unwrap_err();
assert!(error.to_string().contains("more than 1 rows"));
}
#[test]
fn graph_read_query_rejects_rows_before_returning_oversized_output() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-read-limit-1', title: 'Limit one'})")
.unwrap();
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-read-limit-2', title: 'Limit two'})")
.unwrap();
let error = graph
.read_query_with_options(
"MATCH (m:Memory) RETURN m.id AS id",
&NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(4096),
},
)
.unwrap_err();
assert!(error.to_string().contains("more than 1 rows"));
}
#[test]
fn graph_read_query_allows_cypher_limit_within_row_budget() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-read-limit-pass-1', title: 'Limit one'})")
.unwrap();
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-read-limit-pass-2', title: 'Limit two'})")
.unwrap();
let read = graph
.read_query_with_options(
"MATCH (m:Memory) RETURN m.id AS id LIMIT 1",
&NowledgeMemReadOptions {
max_rows: Some(1),
max_estimated_payload_bytes: Some(4096),
},
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
assert_eq!(read.report.row_count, 1);
assert!(!read.report.row_budget_exceeded);
}
#[test]
fn graph_read_query_reports_blocking_operators() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-sort-profile-1', title: 'B'})")
.unwrap();
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-sort-profile-2', title: 'A'})")
.unwrap();
let read = graph
.read_query_with_options(
"MATCH (m:Memory) RETURN m.title AS title ORDER BY title LIMIT 1",
&NowledgeMemReadOptions {
max_rows: Some(4),
max_estimated_payload_bytes: Some(4096),
},
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
assert_eq!(read.report.blocking_operator_kinds, vec!["TopNExec"]);
assert_eq!(read.report.blocking_operator_count, 1);
assert_eq!(read.report.json()["blocking_operator_kinds"][0], "TopNExec");
}
#[test]
fn embedded_store_read_query_does_not_require_search_projection() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-store-read', title: 'Store read'})")
.unwrap();
let store = NowledgeMemEmbeddedStore::new(graph, None);
let read = store
.read_query_with_options(
"MATCH (m:Memory {id: 'mem-store-read'}) RETURN m.title AS title",
&NowledgeMemReadOptions::default(),
)
.unwrap();
assert_eq!(read.output.rows.len(), 1);
assert_eq!(read.report.row_count, 1);
assert!(!read.report.row_budget_exceeded);
assert!(!read.report.payload_budget_exceeded);
}
#[test]
fn embedded_store_library_readiness_fails_closed_without_required_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions::default())
.unwrap();
assert_eq!(
readiness["protocol"],
NOWLEDGE_MEM_LIBRARY_READINESS_PROTOCOL
);
assert_eq!(readiness["present"], true);
assert_eq!(readiness["ready"], false);
assert_eq!(readiness["mode"], "shadow_read_only");
assert_eq!(readiness["production_path"]["ready"], true);
assert_eq!(readiness["production_path"]["in_process"], true);
assert_eq!(readiness["production_path"]["cli_required"], false);
assert_eq!(
readiness["production_path"]["env_control_plane_required"],
false
);
assert_eq!(
readiness["production_path"]["spawned_helper_required"],
false
);
assert_eq!(readiness["bounded_read_evidence"]["present"], false);
assert_eq!(
readiness["bounded_read_evidence"]["blocker_codes"],
serde_json::json!(["bounded_read_probe_missing"])
);
assert_eq!(
readiness["search_projection_evidence"]["blocker_codes"],
serde_json::json!(["search_projection_not_configured"])
);
assert_eq!(
readiness["search_projection_shadow_evidence"]["blocker_codes"],
serde_json::json!(["primary_search_projection_probe_missing"])
);
assert_eq!(
readiness["search_candidate_shadow_evidence"]["blocker_codes"],
serde_json::json!(["search_candidate_shadow_evidence_missing"])
);
assert_eq!(
readiness["query_family_evidence"]["blocker_codes"],
serde_json::json!(["query_family_evidence_missing"])
);
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "bounded_read_evidence_not_ready"));
assert_eq!(readiness["readiness_by_area"]["graph"]["ready"], true);
assert_eq!(readiness["readiness_by_area"]["query"]["ready"], false);
assert_eq!(
readiness["readiness_by_area"]["query"]["blocker_codes"],
serde_json::json!(["bounded_read_probe_missing"])
);
assert_eq!(readiness["readiness_by_area"]["storage"]["ready"], false);
assert_eq!(readiness["readiness_by_area"]["background"]["ready"], false);
assert_eq!(readiness["production_resource_profile"]["present"], false);
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "production_resource_profile_not_ready"));
assert_eq!(
readiness["readiness_by_area"]["background"]["blocker_codes"],
serde_json::json!(["no_candidates", "no_ranked_work"])
);
assert_eq!(
readiness["readiness_by_area"]["query_family"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["query_family"]["blocker_codes"],
serde_json::json!(["query_family_evidence_missing"])
);
assert_eq!(
readiness["graph_route_readiness"]["blocker_codes"],
serde_json::json!(["graph_route_readiness_missing"])
);
assert_eq!(
readiness["readiness_by_area"]["graph_route"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["graph_route"]["blocker_codes"],
serde_json::json!(["graph_route_readiness_missing"])
);
assert_eq!(
readiness["search_route_ownership"]["blocker_codes"],
serde_json::json!(["search_route_ownership_missing"])
);
assert_eq!(
readiness["active_search_route_ownership"]["blocker_codes"],
serde_json::json!(["active_search_route_ownership_missing"])
);
assert_eq!(
readiness["readiness_by_area"]["search_route_ownership"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["search_route_ownership"]["blocker_codes"],
serde_json::json!([
"search_route_ownership_missing",
"active_search_route_ownership_missing",
"active_search_route_readiness_missing"
])
);
assert_eq!(
readiness["readiness_by_area"]["search_projection"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["search_projection_shadow"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["search_candidate_shadow"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["search_candidate_shadow"]["blocker_codes"],
serde_json::json!(["search_candidate_shadow_evidence_missing"])
);
assert_eq!(
readiness["workload_fixture_evidence"]["blocker_codes"],
serde_json::json!(["workload_fixture_evidence_missing"])
);
assert_eq!(
readiness["readiness_by_area"]["workload_fixture"]["ready"],
false
);
assert_eq!(
readiness["readiness_by_area"]["workload_fixture"]["blocker_codes"],
serde_json::json!(["workload_fixture_evidence_missing"])
);
assert_eq!(readiness["ready_area_count"], 1);
assert_eq!(readiness["blocked_area_count"], 10);
assert!(!readiness.to_string().contains("redacted"));
}
#[test]
fn embedded_store_library_readiness_consumes_typed_production_resource_profile() {
let path = unique_nowledge_mem_test_dir("production_resource_profile");
let cache_capacity = 1024;
let config = DatabaseConfig {
storage_residency_mode: StorageResidencyMode::OutOfCore,
segment_cache_capacity_bytes: cache_capacity,
..DatabaseConfig::default()
};
let mut db = Database::open_with_config(&path, config.clone()).unwrap();
let mut transaction = db.begin_transaction().unwrap();
for id in 0..32 {
transaction
.query_with_params(
"CREATE (:Memory {id: $id, body: $body})",
&BTreeMap::from([
("id".to_string(), Value::Int(id)),
("body".to_string(), Value::String("x".repeat(1024))),
]),
)
.unwrap();
}
transaction.commit().unwrap();
db.checkpoint().unwrap();
drop(db);
let db = Database::open_with_config(&path, config).unwrap();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let statement = NowledgeGraphStatement {
cypher: "MATCH (m:Memory) RETURN m.id AS memory_id".to_string(),
parameters: BTreeMap::new(),
};
let identity = crate::ProductionQualificationIdentity {
source_revision: "test-revision".to_string(),
rust_toolchain: "test-toolchain".to_string(),
target_os: std::env::consts::OS.to_string(),
target_arch: std::env::consts::ARCH.to_string(),
enabled_features: Vec::new(),
durable_format_version: 1,
schema_version: 1,
configuration_digest: "test-config".to_string(),
deployment_profile: "test-production-replica".to_string(),
dataset_fingerprint: "test-dataset".to_string(),
canonical_graph_commit_epoch: store.graph.database().commit_epoch().unwrap(),
policy_version: crate::PRODUCTION_QUALIFICATION_POLICY_VERSION,
};
let profile = store
.production_resource_profile(
&statement,
StorageResourceProfileLimits {
min_canonical_artifact_bytes: 4096,
max_steady_resident_bytes: u64::MAX,
max_peak_resident_bytes: u64::MAX,
max_total_page_faults: Some(u64::MAX),
max_minor_page_faults: cfg!(unix).then_some(u64::MAX),
max_major_page_faults: cfg!(unix).then_some(u64::MAX),
max_intermediate_rows: 1024,
max_intermediate_payload_bytes: 1024 * 1024,
max_output_rows: 64,
max_output_payload_bytes: 1024 * 1024,
require_fully_streamed: true,
},
crate::ProductionEvidenceBinding {
identity: identity.clone(),
generated_at_unix_seconds: 1,
},
identity,
)
.unwrap();
assert!(
profile.resource_ready,
"unexpected blockers: {:?}",
profile.blocker_codes
);
assert!(profile.production_ready());
assert!(profile.after.canonical_artifact_bytes > cache_capacity);
let readiness = store
.library_readiness(&NowledgeMemReadinessOptions {
production_resource_profile: Some(profile),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(readiness.production_resource_profile["ready"], true);
assert!(
readiness.readiness_by_area.storage.ready,
"unexpected storage blockers: {:?}",
readiness.readiness_by_area.storage.blocker_codes
);
assert!(!readiness
.blocker_codes
.iter()
.any(|code| code == "production_resource_profile_not_ready"));
drop(store);
std::fs::remove_dir_all(path).unwrap();
}
#[test]
fn embedded_store_exposes_typed_library_readiness_report() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let report = store
.library_readiness(&NowledgeMemReadinessOptions::default())
.unwrap();
let json = report.json();
assert_eq!(report.protocol, NOWLEDGE_MEM_LIBRARY_READINESS_PROTOCOL);
assert!(report.present);
assert!(!report.ready);
assert_eq!(report.mode, NowledgeMemGraphMode::ShadowReadOnly);
assert!(report.redaction.ready());
assert!(!report.redaction.query_text_copied);
assert!(!report.redaction.parameters_copied);
assert!(!report.redaction.local_paths_copied);
assert!(report.graph_open);
assert!(!report.graph_read_only);
let areas = report.areas();
assert_eq!(areas.len(), 11);
assert_eq!(report.ready_area_count, 1);
assert_eq!(report.blocked_area_count, 10);
assert!(report.readiness_by_area.graph.ready);
assert!(!report.readiness_by_area.query.ready);
assert_eq!(
report.readiness_by_area.query.blocker_codes,
vec!["bounded_read_probe_missing".to_string()]
);
assert!(!report.readiness_by_area.graph_route.ready);
assert_eq!(
report.readiness_by_area.graph_route.blocker_codes,
vec!["graph_route_readiness_missing".to_string()]
);
assert!(!report.readiness_by_area.search_route_ownership.ready);
assert_eq!(
report
.readiness_by_area
.search_route_ownership
.blocker_codes,
vec![
"search_route_ownership_missing".to_string(),
"active_search_route_ownership_missing".to_string(),
"active_search_route_readiness_missing".to_string()
]
);
assert!(!report.readiness_by_area.workload_fixture.ready);
assert_eq!(
report.readiness_by_area.workload_fixture.blocker_codes,
vec!["workload_fixture_evidence_missing".to_string()]
);
let graph_area = areas
.iter()
.find(|area| area.name == "graph")
.expect("graph readiness area");
let query_area = areas
.iter()
.find(|area| area.name == "query")
.expect("query readiness area");
assert!(graph_area.ready);
assert!(!query_area.ready);
assert_eq!(
query_area.blocker_codes,
vec!["bounded_read_probe_missing".to_string()]
);
assert!(report
.blocker_codes
.iter()
.any(|code| code == "bounded_read_evidence_not_ready"));
assert!(report
.blocker_codes
.iter()
.any(|code| code == "graph_route_readiness_not_ready"));
assert!(report
.blocker_codes
.iter()
.any(|code| code == "search_route_ownership_not_ready"));
assert!(report
.blocker_codes
.iter()
.any(|code| code == "active_search_route_ownership_not_ready"));
assert!(report
.blocker_codes
.iter()
.any(|code| code == "active_search_route_readiness_not_ready"));
assert_eq!(json["ready"], false);
assert_eq!(json["redaction"]["ready"], true);
assert_eq!(json["redaction"]["query_text_copied"], false);
assert_eq!(json["redaction"]["parameters_copied"], false);
assert_eq!(json["redaction"]["local_paths_copied"], false);
assert_eq!(
json["blocker_codes"],
serde_json::json!(report.blocker_codes)
);
assert_eq!(json["areas"].as_array().unwrap().len(), areas.len());
assert_eq!(
json["areas"][0],
serde_json::json!({
"name": "graph",
"ready": true,
"blocker_codes": [],
})
);
assert_eq!(json["graph"]["read_only"], false);
assert_eq!(
json["bounded_read_evidence"]["blocker_codes"],
serde_json::json!(["bounded_read_probe_missing"])
);
assert_eq!(
json["search_route_ownership"]["blocker_codes"],
serde_json::json!(["search_route_ownership_missing"])
);
assert_eq!(
json["active_search_route_ownership"]["blocker_codes"],
serde_json::json!(["active_search_route_ownership_missing"])
);
assert_eq!(
json["active_search_route_readiness"]["blocker_codes"],
serde_json::json!(["active_search_route_readiness_missing"])
);
}
#[test]
fn embedded_store_library_readiness_requires_active_search_route_read_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
search_route_ownership: Some(ready_search_route_ownership()),
active_search_route_ownership: Some(ready_active_search_route_ownership()),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(
readiness["search_route_ownership"]["ready"],
serde_json::json!(true)
);
assert_eq!(
readiness["active_search_route_ownership"]["ready"],
serde_json::json!(true)
);
assert_eq!(
readiness["active_search_route_readiness"]["blocker_codes"],
serde_json::json!(["active_search_route_readiness_missing"])
);
assert_eq!(
readiness["readiness_by_area"]["search_route_ownership"]["ready"],
serde_json::json!(false)
);
assert_eq!(
readiness["readiness_by_area"]["search_route_ownership"]["blocker_codes"],
serde_json::json!(["active_search_route_readiness_missing"])
);
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "active_search_route_readiness_not_ready"));
}
#[test]
fn embedded_store_library_readiness_accepts_typed_workload_fixture_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let workload_fixture = nowledge_graph_route_workload_fixture_report(
NowledgeGraphRouteWorkloadFixtureOptions {
capture_physical_plan: true,
include_bounded_expansion_probes: true,
..NowledgeGraphRouteWorkloadFixtureOptions::default()
},
)
.unwrap();
let report = store
.library_readiness(&NowledgeMemReadinessOptions {
workload_fixture_evidence: Some(workload_fixture.clone()),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
let json = report.json();
assert!(workload_fixture.ready);
assert!(report.readiness_by_area.workload_fixture.ready);
assert_eq!(
report.readiness_by_area.workload_fixture.blocker_codes,
Vec::<String>::new()
);
assert!(report
.blocker_codes
.iter()
.all(|code| code != "workload_fixture_evidence_not_ready"));
assert_eq!(json["workload_fixture_evidence"]["ready"], true);
assert_eq!(json["readiness_by_area"]["workload_fixture"]["ready"], true);
assert_eq!(
json["workload_fixture_evidence"]["failed_query_count"],
serde_json::json!(0)
);
assert_eq!(
json["workload_fixture_evidence"]["failed_bounded_expansion_probe_count"],
serde_json::json!(0)
);
assert_eq!(
json["workload_fixture_evidence"]["failed_search_metadata_probe_count"],
serde_json::json!(0)
);
assert_eq!(
json["workload_fixture_evidence"]["failed_graph_rag_probe_count"],
serde_json::json!(0)
);
assert_eq!(
json["workload_fixture_evidence"]["failed_source_projection_probe_count"],
serde_json::json!(0)
);
assert_eq!(
json["workload_fixture_evidence"]["graph_rag_reports"][0]["ready"],
true
);
assert_eq!(
json["workload_fixture_evidence"]["graph_rag_reports"][0]
["parameter_requirement_count"],
serde_json::json!(1)
);
assert_eq!(
json["workload_fixture_evidence"]["source_projection_reports"][0]["ready"],
true
);
assert_eq!(
json["workload_fixture_evidence"]["source_projection_reports"][0]
["too_small_batch_failed_closed"],
true
);
assert_eq!(
json["workload_fixture_evidence"]["source_projection_reports"][0]
["indexed_source_document_ready"],
true
);
}
#[test]
fn library_readiness_rejects_workload_fixture_without_graph_rag_probe() {
let workload_fixture = nowledge_graph_route_workload_fixture_report(
NowledgeGraphRouteWorkloadFixtureOptions::default(),
)
.unwrap();
let mut evidence = workload_fixture.json();
evidence
.as_object_mut()
.unwrap()
.remove("graph_rag_probe_count");
evidence
.as_object_mut()
.unwrap()
.remove("failed_graph_rag_probe_count");
evidence
.as_object_mut()
.unwrap()
.remove("graph_rag_reports");
let blockers =
hawdb_readiness::library_readiness::workload_fixture_readiness_blocker_codes(&evidence);
assert!(blockers.contains(&"workload_fixture_graph_rag_not_ready".to_string()));
assert!(blockers.contains(&"workload_fixture_graph_rag_probe_missing".to_string()));
}
#[test]
fn library_readiness_rejects_workload_fixture_without_source_projection_probe() {
let workload_fixture = nowledge_graph_route_workload_fixture_report(
NowledgeGraphRouteWorkloadFixtureOptions::default(),
)
.unwrap();
let mut evidence = workload_fixture.json();
evidence
.as_object_mut()
.unwrap()
.remove("source_projection_probe_count");
evidence
.as_object_mut()
.unwrap()
.remove("failed_source_projection_probe_count");
evidence
.as_object_mut()
.unwrap()
.remove("source_projection_reports");
let blockers =
hawdb_readiness::library_readiness::workload_fixture_readiness_blocker_codes(&evidence);
assert!(blockers.contains(&"workload_fixture_source_projection_not_ready".to_string()));
assert!(blockers.contains(&"workload_fixture_source_projection_probe_missing".to_string()));
}
#[test]
fn embedded_store_library_readiness_recomputes_search_candidate_shadow_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
search_candidate_shadow_evidence: Some(serde_json::json!({
"ready": true,
"blocker_codes": []
})),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(
readiness["readiness_by_area"]["search_candidate_shadow"]["ready"],
false
);
let blocker_codes = readiness["readiness_by_area"]["search_candidate_shadow"]
["blocker_codes"]
.as_array()
.unwrap();
assert!(blocker_codes
.iter()
.any(|code| code == "search_candidate_shadow_protocol_mismatch"));
assert!(blocker_codes
.iter()
.any(|code| code == "search_candidate_counts_not_ready"));
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "search_candidate_shadow_evidence_not_ready"));
}
#[test]
fn embedded_store_library_readiness_recomputes_search_projection_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
search_projection_evidence: Some(serde_json::json!({
"ready": true,
"blocker_codes": []
})),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(
readiness["readiness_by_area"]["search_projection"]["ready"],
false
);
let blocker_codes = readiness["readiness_by_area"]["search_projection"]["blocker_codes"]
.as_array()
.unwrap();
assert!(blocker_codes
.iter()
.any(|code| code == "search_projection_protocol_mismatch"));
assert!(blocker_codes
.iter()
.any(|code| code == "search_projection_tables_not_ready"));
assert!(blocker_codes
.iter()
.any(|code| code == "search_projection_document_identity_not_ready"));
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "search_projection_evidence_not_ready"));
}
#[test]
fn embedded_store_library_readiness_recomputes_search_projection_shadow_evidence() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
search_projection_shadow_evidence: Some(serde_json::json!({
"ready": true,
"blocker_codes": []
})),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(
readiness["readiness_by_area"]["search_projection_shadow"]["ready"],
false
);
let blocker_codes = readiness["readiness_by_area"]["search_projection_shadow"]
["blocker_codes"]
.as_array()
.unwrap();
assert!(blocker_codes
.iter()
.any(|code| code == "search_projection_shadow_protocol_mismatch"));
assert!(blocker_codes
.iter()
.any(|code| code == "search_projection_shadow_document_identity_not_ready"));
assert!(blocker_codes.iter().any(|code| {
code == hawdb_readiness::library_readiness::SEARCH_PROJECTION_SHADOW_PUSHDOWN_NOT_READY
}));
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "search_projection_shadow_evidence_not_ready"));
}
#[test]
fn embedded_store_exposes_compact_readiness_dashboard() {
let db = Database::new_with_config(DatabaseConfig {
slow_query_log_threshold_micros: 0,
slow_query_log_capacity: 4,
..DatabaseConfig::default()
});
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
store
.query_with_report(
"CREATE (:Memory {id: 'dashboard-secret', title: 'Dashboard Secret'})",
)
.unwrap();
let dashboard = store
.readiness_dashboard(&NowledgeMemReadinessOptions::default())
.unwrap();
let json = dashboard.json();
let encoded = json.to_string();
assert_eq!(
dashboard.protocol,
NOWLEDGE_MEM_READINESS_DASHBOARD_PROTOCOL
);
assert_eq!(dashboard.mode, NowledgeMemGraphMode::ShadowReadOnly);
assert!(!dashboard.ready);
assert_eq!(dashboard.area_count, 12);
assert_eq!(dashboard.ready_area_count, 3);
assert_eq!(dashboard.blocked_area_count, 9);
assert_eq!(
dashboard.storage_lifecycle_action,
NowledgeMemStorageLifecycleActionKind::OpenReadOnlyInspect
);
assert!(!dashboard.storage_lifecycle_ready);
assert!(dashboard.slow_query_ready);
assert_eq!(dashboard.slow_query_record_count, 1);
assert!(readiness_dashboard_area(&dashboard, "graph").ready);
assert!(readiness_dashboard_area(&dashboard, "background").ready);
assert_eq!(
readiness_dashboard_area(&dashboard, "query").blocker_codes,
vec!["bounded_read_probe_missing".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "query_family").blocker_codes,
vec!["query_family_evidence_missing".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "graph_route").blocker_codes,
vec!["graph_route_readiness_missing".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "storage").blocker_codes,
vec![
"storage_recovery_not_ready".to_string(),
"production_resource_profile_has_blockers".to_string(),
"production_resource_profile_identity_invalid".to_string(),
"production_resource_profile_intermediate_payload_bytes_invalid".to_string(),
"production_resource_profile_intermediate_rows_invalid".to_string(),
"production_resource_profile_metric_capabilities_invalid".to_string(),
"production_resource_profile_missing".to_string(),
"production_resource_profile_not_ready".to_string(),
"production_resource_profile_output_payload_bytes_invalid".to_string(),
"production_resource_profile_output_rows_invalid".to_string(),
"production_resource_profile_peak_resident_bytes_invalid".to_string(),
"production_resource_profile_resident_growth_missing".to_string(),
"production_resource_profile_resource_not_ready".to_string(),
"production_resource_profile_steady_resident_bytes_invalid".to_string(),
"production_resource_profile_storage_budget_invalid".to_string(),
"production_resource_profile_streaming_invalid".to_string(),
"production_resource_profile_total_page_faults_invalid".to_string()
]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "search_projection").blocker_codes,
vec!["search_projection_not_configured".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "search_projection_shadow").blocker_codes,
vec!["primary_search_projection_probe_missing".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "search_candidate_shadow").blocker_codes,
vec!["search_candidate_shadow_evidence_missing".to_string()]
);
assert_eq!(
readiness_dashboard_area(&dashboard, "workload_fixture").blocker_codes,
vec!["workload_fixture_evidence_missing".to_string()]
);
assert!(readiness_dashboard_area(&dashboard, "slow_query").ready);
assert_eq!(json["protocol"], NOWLEDGE_MEM_READINESS_DASHBOARD_PROTOCOL);
assert_eq!(
json["storage_lifecycle"]["action"],
"open_read_only_inspect"
);
assert_eq!(json["storage_lifecycle"]["ready"], false);
assert_eq!(json["redaction"]["query_text_copied"], false);
assert_eq!(json["redaction"]["parameters_copied"], false);
assert_eq!(json["redaction"]["local_paths_copied"], false);
assert!(!encoded.contains("Dashboard Secret"));
assert!(!encoded.contains("dashboard-secret"));
}
#[test]
fn storage_recovery_report_exposes_typed_readiness_summary() {
let report =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: Some(11),
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(7),
replayed_wal_entries: 3,
torn_tail_ignored: false,
torn_tail_reason: None,
recovered_commit_epoch: 14,
..StorageRecoveryReport::default()
});
let json = report.json();
assert_eq!(report.protocol, "hawdb-storage-recovery-report");
assert!(report.present);
assert!(report.ready);
assert!(report.durable_recovery_observed);
assert!(report.checkpoint_boundary_present);
assert!(report.wal_replay_bounded);
assert!(report.replay_boundary_consistent);
assert!(report.torn_tail_clean);
assert!(report.blocker_codes.is_empty());
assert_eq!(json["ready"], true);
assert_eq!(json["readiness"]["wal_replay_bounded"], true);
assert_eq!(json["readiness"]["replay_boundary_consistent"], true);
assert_eq!(json["max_wal_replay_entries"], 16);
}
#[test]
fn storage_recovery_report_rejects_inconsistent_open_timings() {
let report =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: StorageOpenTimings {
durable_manifest_open_micros: 2,
total_open_micros: 1,
..StorageOpenTimings::default()
},
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: Some(11),
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(7),
replayed_wal_entries: 3,
torn_tail_ignored: false,
torn_tail_reason: None,
recovered_commit_epoch: 14,
..StorageRecoveryReport::default()
});
assert!(!report.open_timing_consistent);
assert!(!report.ready);
assert_eq!(
report.blocker_codes,
vec!["storage_open_timing_inconsistent".to_string()]
);
assert_eq!(report.json()["readiness"]["open_timing_consistent"], false);
}
#[test]
fn storage_lifecycle_decision_reports_ready_for_clean_recovery() {
let recovery =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: Some(11),
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(7),
replayed_wal_entries: 3,
torn_tail_ignored: false,
torn_tail_reason: None,
recovered_commit_epoch: 14,
..StorageRecoveryReport::default()
});
let decision = NowledgeMemStorageLifecycleDecision::from_storage_recovery(recovery);
let json = decision.json();
assert_eq!(
decision.protocol,
NOWLEDGE_MEM_STORAGE_LIFECYCLE_DECISION_PROTOCOL
);
assert_eq!(
decision.action,
NowledgeMemStorageLifecycleActionKind::Ready
);
assert!(decision.ready_for_mem_lifecycle);
assert!(decision.storage_recovery_ready);
assert!(!decision.checkpoint_required);
assert!(!decision.repair_required);
assert!(!decision.quarantine_required);
assert!(!decision.read_only_inspection_required);
assert!(decision.blocker_codes.is_empty());
assert_eq!(json["action"], "ready");
assert_eq!(json["ready_for_mem_lifecycle"], true);
}
#[test]
fn storage_lifecycle_decision_recommends_wal_tail_repair() {
let recovery =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::DoctorRepairTornTail,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: Some(11),
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(7),
replayed_wal_entries: 3,
torn_tail_ignored: true,
torn_tail_reason: Some("partial wal entry".to_string()),
recovered_commit_epoch: 14,
..StorageRecoveryReport::default()
});
let decision = NowledgeMemStorageLifecycleDecision::from_storage_recovery(recovery);
assert_eq!(
decision.action,
NowledgeMemStorageLifecycleActionKind::RepairWalTail
);
assert!(!decision.ready_for_mem_lifecycle);
assert!(decision.repair_required);
assert_eq!(
decision.blocker_codes,
vec![
"torn_tail_observed".to_string(),
"wal_tail_repair_required".to_string()
]
);
assert_eq!(decision.json()["action"], "repair_wal_tail");
}
#[test]
fn storage_lifecycle_decision_fails_closed_for_in_memory_storage() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let decision = store.storage_lifecycle_decision().unwrap();
let json = store.storage_lifecycle_decision_json().unwrap();
assert_eq!(
decision.action,
NowledgeMemStorageLifecycleActionKind::OpenReadOnlyInspect
);
assert!(!decision.ready_for_mem_lifecycle);
assert!(decision.read_only_inspection_required);
assert!(decision
.blocker_codes
.contains(&"durable_recovery_not_observed".to_string()));
assert!(decision
.blocker_codes
.contains(&"storage_not_durable".to_string()));
assert_eq!(json["action"], "open_read_only_inspect");
}
#[test]
fn storage_lifecycle_decision_recommends_checkpoint_for_missing_boundary() {
let recovery =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: None,
checkpoint_commit_epoch: None,
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(4),
replayed_wal_entries: 0,
torn_tail_ignored: false,
torn_tail_reason: None,
recovered_commit_epoch: 4,
..StorageRecoveryReport::default()
});
let decision = NowledgeMemStorageLifecycleDecision::from_storage_recovery(recovery);
assert_eq!(
decision.action,
NowledgeMemStorageLifecycleActionKind::RunCheckpoint
);
assert!(!decision.ready_for_mem_lifecycle);
assert!(decision.checkpoint_required);
assert_eq!(
decision.blocker_codes,
vec![
"checkpoint_boundary_missing".to_string(),
"replay_boundary_inconsistent".to_string(),
"checkpoint_required".to_string()
]
);
}
#[test]
fn storage_recovery_report_recomputes_typed_readiness_from_raw_fields() {
let report =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(2),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: None,
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(8),
replayed_wal_entries: 3,
torn_tail_ignored: false,
torn_tail_reason: Some("partial wal entry".to_string()),
recovered_commit_epoch: 13,
..StorageRecoveryReport::default()
});
let json = report.json();
assert!(report.durable_recovery_observed);
assert!(!report.ready);
assert!(!report.checkpoint_boundary_present);
assert!(!report.wal_replay_bounded);
assert!(!report.replay_boundary_consistent);
assert!(!report.torn_tail_clean);
assert_eq!(
report.blocker_codes,
vec![
"checkpoint_boundary_missing".to_string(),
"wal_replay_unbounded".to_string(),
"replay_boundary_inconsistent".to_string(),
"torn_tail_observed".to_string()
]
);
assert_eq!(json["readiness"]["checkpoint_boundary_present"], false);
assert_eq!(json["readiness"]["wal_replay_bounded"], false);
assert_eq!(json["readiness"]["replay_boundary_consistent"], false);
assert_eq!(json["readiness"]["torn_tail_clean"], false);
}
#[test]
fn storage_recovery_report_rejects_inconsistent_replay_boundary() {
let report =
NowledgeMemStorageRecoveryReport::from_storage_report(&StorageRecoveryReport {
open_timings: Default::default(),
durable: true,
recovery_mode: RecoveryMode::Strict,
max_wal_replay_entries: Some(16),
max_wal_replay_bytes: Some(4096),
max_wal_record_bytes: Some(1024),
checkpoint_epoch: Some(3),
checkpoint_commit_epoch: Some(11),
wal_present: true,
wal_replay_start_lsn: Some(4),
next_lsn_after_replay: Some(7),
replayed_wal_entries: 3,
torn_tail_ignored: false,
torn_tail_reason: None,
recovered_commit_epoch: 13,
..StorageRecoveryReport::default()
});
let json = report.json();
assert!(report.durable_recovery_observed);
assert!(report.checkpoint_boundary_present);
assert!(report.wal_replay_bounded);
assert!(!report.replay_boundary_consistent);
assert!(report.torn_tail_clean);
assert!(!report.ready);
assert_eq!(
report.blocker_codes,
vec!["replay_boundary_inconsistent".to_string()]
);
assert_eq!(json["readiness"]["replay_boundary_consistent"], false);
}
#[test]
fn embedded_store_exposes_storage_recovery_report_through_library_api() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let report = store.storage_recovery_report().unwrap();
let json = store.storage_recovery_report_json().unwrap();
assert_eq!(report.protocol, "hawdb-storage-recovery-report");
assert!(report.present);
assert!(!report.ready);
assert_eq!(
report.blocker_codes,
vec![
"durable_recovery_not_observed".to_string(),
"checkpoint_boundary_missing".to_string(),
"wal_replay_unbounded".to_string(),
"replay_boundary_inconsistent".to_string()
]
);
assert_eq!(json["ready"], false);
assert_eq!(
json["blocker_codes"],
serde_json::json!(report.blocker_codes)
);
}
#[test]
fn embedded_store_library_readiness_runs_bounded_probe() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-readiness', title: 'Readiness'})")
.unwrap();
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
bounded_read_probe: Some(NowledgeGraphStatement {
cypher: "MATCH (m:Memory {id: 'mem-readiness'}) RETURN m.title AS title"
.to_string(),
parameters: BTreeMap::new(),
}),
covered_routes: full_bounded_read_routes(),
graph_route_readiness: Some(ready_route_readiness_summary()),
search_route_ownership: Some(ready_search_route_ownership()),
active_search_route_ownership: Some(ready_active_search_route_ownership()),
active_search_route_readiness: Some(ready_active_search_route_readiness()),
replacement_readiness_by_query_family: Some(ready_query_family_replacement()),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(readiness["bounded_read_evidence"]["present"], true);
assert_eq!(readiness["bounded_read_evidence"]["ready"], true);
assert_eq!(
readiness["bounded_read_evidence"]["mode"],
"shadow_read_only"
);
assert_eq!(readiness["bounded_read_evidence"]["execution_row_cap"], 513);
assert!(
readiness["bounded_read_evidence"]["estimated_payload_bytes"]
.as_u64()
.is_some()
);
assert_eq!(
readiness["bounded_read_evidence"]["max_estimated_payload_bytes"],
serde_json::json!(4 * 1024 * 1024)
);
assert_eq!(
readiness["bounded_read_evidence"]["payload_budget_exceeded"],
false
);
assert_eq!(
readiness["bounded_read_evidence"]["missing_covered_routes"],
serde_json::json!([])
);
assert_eq!(
readiness["background_maintenance"]["total_candidates"]
.as_u64()
.unwrap_or_default(),
readiness["background_maintenance"]["ranked"]
.as_array()
.unwrap()
.len() as u64
);
assert_eq!(readiness["readiness_by_area"]["query"]["ready"], true);
assert_eq!(readiness["readiness_by_area"]["background"]["ready"], true);
assert_eq!(
readiness["readiness_by_area"]["background"]["blocker_codes"],
serde_json::json!([])
);
assert_eq!(
readiness["readiness_by_area"]["query_family"]["ready"],
true
);
assert_eq!(readiness["readiness_by_area"]["graph_route"]["ready"], true);
assert_eq!(
readiness["readiness_by_area"]["search_route_ownership"]["ready"],
true
);
assert_eq!(
readiness["search_route_ownership"]["lancedb_route_count"],
0
);
assert_eq!(
readiness["active_search_route_ownership"]["lancedb_route_count"],
0
);
assert_eq!(
readiness["active_search_route_readiness"]["lancedb_handle_required_route_count"],
0
);
assert_eq!(
readiness["graph_route_readiness"]["primary_ready_route_count"],
REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES.len()
);
assert_eq!(
readiness["query_family_evidence"]["missing_required_query_families"],
serde_json::json!([])
);
assert_eq!(
readiness["query_family_evidence"]["min_replacement_readiness_per_million"],
1_000_000
);
}
#[test]
fn embedded_store_library_readiness_feeds_final_preflight_without_cli() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
graph
.database_mut()
.query("CREATE (:Memory {id: 'mem-preflight', title: 'Preflight'})")
.unwrap();
let store = NowledgeMemEmbeddedStore::new(graph, None);
let library = store
.library_readiness(&NowledgeMemReadinessOptions {
bounded_read_probe: Some(NowledgeGraphStatement {
cypher: "MATCH (m:Memory {id: 'mem-preflight'}) RETURN m.title AS title"
.to_string(),
parameters: BTreeMap::new(),
}),
covered_routes: full_bounded_read_routes(),
graph_route_readiness: Some(ready_route_readiness_summary()),
search_route_ownership: Some(ready_search_route_ownership()),
active_search_route_ownership: Some(ready_active_search_route_ownership()),
active_search_route_readiness: Some(ready_active_search_route_readiness()),
replacement_readiness_by_query_family: Some(ready_query_family_replacement()),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
let mut library_json = library.json();
library_json["open_report"] = serde_json::json!({
"protocol": NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL,
"mode": "shadow_read_only",
"graph_configured": true,
"search_projection_configured": false,
"compressed_vector_search_mode": "disabled",
"graph_opened": true,
"search_projection_opened": false,
});
let bundle = serde_json::json!({
"library_readiness": library_json,
});
let preflight = nowledge_mem_final_cutover_preflight(&bundle);
assert!(library.readiness_by_area.query.ready);
assert!(library.readiness_by_area.graph_route.ready);
assert!(library.readiness_by_area.search_route_ownership.ready);
assert!(library.readiness_by_area.query_family.ready);
assert!(library.readiness_by_area.background.ready);
assert!(!library.readiness_by_area.storage.ready);
assert!(!library.readiness_by_area.search_projection.ready);
assert!(!preflight.production_cutover_ready);
assert!(!preflight.library_only_ready);
assert!(preflight
.failed_checks
.iter()
.any(|check| check == "library_readiness"));
assert!(preflight
.failed_evidence_fields
.iter()
.any(|field| field == "library_readiness.open_report.search_projection_opened"));
assert!(preflight
.next_action_names
.iter()
.any(|action| action == "attach_library_readiness_evidence"));
}
#[test]
fn embedded_store_library_readiness_recomputes_bounded_read_payload_budget() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let readiness = store
.library_readiness_json(&NowledgeMemReadinessOptions {
bounded_read_evidence: Some(serde_json::json!({
"protocol": NOWLEDGE_MEM_BOUNDED_READ_EVIDENCE_PROTOCOL,
"present": true,
"ready": true,
"mode": "shadow_read_only",
"max_rows": 512,
"execution_row_cap": 513,
"row_limit_enforced_before_output": true,
"operator_row_cap_enabled": true,
"streaming": false,
"blocking_operator_count": 0,
"row_budget_exceeded": false,
"payload_budget_exceeded": false,
"missing_covered_routes": [],
"route_primary_ready": true,
"route_query_plan_evidence_ready": true,
"route_query_profile_evidence_ready": true,
"route_query_api_behavior_evidence_ready": true,
"relationship_property_pruning_required_count": 0,
"relationship_property_pruning_report_count": 0,
"route_relationship_property_pruning_evidence_ready": true,
"blocker_codes": [],
})),
..NowledgeMemReadinessOptions::default()
})
.unwrap();
assert_eq!(readiness["bounded_read_evidence"]["ready"], true);
assert_eq!(readiness["readiness_by_area"]["query"]["ready"], false);
assert!(readiness["blocker_codes"]
.as_array()
.unwrap()
.iter()
.any(|code| code == "bounded_read_evidence_not_ready"));
let query_blockers = readiness["readiness_by_area"]["query"]["blocker_codes"]
.as_array()
.unwrap();
assert!(query_blockers
.iter()
.any(|code| code == "bounded_read_estimated_payload_bytes_missing"));
assert!(query_blockers
.iter()
.any(|code| code == "bounded_read_max_estimated_payload_bytes_missing"));
}
#[test]
fn embedded_store_exposes_search_projection_probe() {
let index = SearchIndex::default();
let projection = NowledgeMemSearchProjection::from_index(index);
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let probe = store
.search_projection()
.unwrap()
.probe_json(SearchProjectionProbeOptions::default());
assert_eq!(probe["protocol"], "hawdb-nowledge-search-projection-probe");
}
#[test]
fn embedded_store_handle_exposes_sampled_vector_recall_validation() {
let index = persisted_nowledge_projection_evidence_index("handle_recall_validation");
let projection = NowledgeMemSearchProjection::from_index(index);
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let report = handle
.validate_sampled_vector_recall(VectorRecallValidationOptions {
max_samples: 2,
top_k: 1,
candidate_limit: 1,
minimum_recall_per_million: 0,
metadata_filters: BTreeMap::new(),
})
.unwrap();
assert!(report.ready, "{:?}", report.blocker_codes);
assert_eq!(report.protocol, VECTOR_RECALL_VALIDATION_PROTOCOL);
assert_eq!(report.executed_sample_count, 2);
assert_eq!(
report.approximate_backend,
"hawdb_rabitq_candidate_projection"
);
assert!(report.validates_required_approximate_backend());
}
#[test]
fn embedded_store_recall_validation_requires_search_projection() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let error = store
.validate_sampled_vector_recall(VectorRecallValidationOptions::default())
.unwrap_err();
assert_eq!(
error.to_string(),
"storage error: nowledge mem search projection is not configured"
);
}
#[test]
fn embedded_store_exposes_search_projection_replacement_evidence() {
let index = persisted_nowledge_projection_evidence_index("replacement_evidence");
let projection = NowledgeMemSearchProjection::from_index(index);
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let evidence = store
.search_projection_evidence_json(SearchProjectionProbeOptions {
active_embedding_model: Some("bge-m3".to_string()),
active_embedding_dimension: Some(8),
})
.unwrap();
assert_eq!(
evidence["protocol"],
"hawdb-nowledge-search-projection-evidence"
);
assert_eq!(evidence["ready"], true, "evidence={evidence:#}");
assert_eq!(evidence["covered_table_count"], 6);
assert_eq!(evidence["required_table_count"], 6);
assert_eq!(evidence["source_chunk_ready"], true);
assert_eq!(evidence["incremental_update_ready"], true);
assert_eq!(evidence["blocker_codes"], serde_json::json!([]));
}
#[test]
fn embedded_store_exposes_typed_search_projection_replacement_evidence() {
let index = persisted_nowledge_projection_evidence_index("typed_replacement_evidence");
let projection = NowledgeMemSearchProjection::from_index(index);
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let report = store
.search_projection_evidence_report(SearchProjectionProbeOptions {
active_embedding_model: Some("bge-m3".to_string()),
active_embedding_dimension: Some(8),
})
.unwrap();
assert_eq!(report.protocol, "hawdb-nowledge-search-projection-evidence");
assert!(report.ready);
assert!(report.compressed_vector_projection_ready);
assert!(report.derived_projection);
assert!(report.all_tables_covered);
assert_eq!(report.covered_table_count, 6);
assert_eq!(report.required_table_count, 6);
assert!(report.source_chunk_ready);
assert!(report.incremental_update_ready);
assert_eq!(report.json()["covered_table_count"], 6);
}
#[test]
fn embedded_store_exposes_search_projection_shadow_evidence() {
let index = persisted_nowledge_projection_evidence_index("shadow_evidence");
let projection = NowledgeMemSearchProjection::from_index(index);
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let probe_options = SearchProjectionProbeOptions {
active_embedding_model: Some("bge-m3".to_string()),
active_embedding_dimension: Some(8),
};
let primary_probe = store
.search_projection_probe_json(probe_options.clone())
.unwrap();
let evidence = store
.search_projection_shadow_evidence_json(&primary_probe, probe_options)
.unwrap();
assert_eq!(
evidence["protocol"],
"hawdb-nowledge-search-projection-shadow-evidence"
);
assert_eq!(evidence["ready"], true, "evidence={evidence:#}");
assert_eq!(evidence["primary_ready"], true);
assert_eq!(evidence["shadow_ready"], true);
assert_eq!(evidence["document_count_parity"], true);
assert_eq!(evidence["table_parity"]["ready"], true);
assert_eq!(evidence["embedding_identity_parity"], true);
assert_eq!(evidence["incremental_watermark_parity"], true);
assert_eq!(
evidence["pushdown_evidence"]["shadow_segment_document_pruning_ready"],
true
);
assert_eq!(
evidence["pushdown_evidence"]["shadow_segment_pruning_candidate_document_count"],
130
);
assert_eq!(
evidence["pushdown_evidence"]["shadow_segment_pruned_document_count"],
2
);
assert_eq!(
evidence["pushdown_evidence"]["shadow_segment_scanned_document_count"],
128
);
assert_eq!(evidence["blocker_codes"], serde_json::json!([]));
}
#[test]
fn embedded_store_search_projection_evidence_requires_projection() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let error = store
.search_projection_evidence_json(SearchProjectionProbeOptions::default())
.unwrap_err();
assert_eq!(
error.to_string(),
"storage error: nowledge mem search projection is not configured"
);
}
#[test]
fn embedded_store_search_projection_shadow_evidence_requires_projection() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let error = store
.search_projection_shadow_evidence_json(
&serde_json::json!({ "engine": "lancedb" }),
SearchProjectionProbeOptions::default(),
)
.unwrap_err();
assert_eq!(
error.to_string(),
"storage error: nowledge mem search projection is not configured"
);
}
#[test]
fn open_options_report_is_sanitized() {
let options = NowledgeMemOpenOptions::with_search_projection(
"redacted_graph_path",
"redacted_search_path",
NowledgeMemGraphMode::ShadowReadOnly,
);
let report = options.sanitized_report().json();
assert_eq!(report["protocol"], NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL);
assert_eq!(report["mode"], "shadow_read_only");
assert_eq!(report["graph_configured"], true);
assert_eq!(report["search_projection_configured"], true);
assert_eq!(report["compressed_vector_search_mode"], "disabled");
assert!(report.get("graph_path").is_none());
assert!(report.get("search_projection_path").is_none());
assert!(!report.to_string().contains("redacted_graph_path"));
assert!(!report.to_string().contains("redacted_search_path"));
}
#[test]
fn open_options_diagnostics_include_local_paths_only_with_debug_flag() {
let options = NowledgeMemOpenOptions::with_search_projection(
"debug_graph_path",
"debug_search_path",
NowledgeMemGraphMode::ShadowReadOnly,
);
let redacted = options.diagnostic_report_json(NowledgeMemOpenDiagnosticOptions::default());
assert_eq!(redacted["debug_local_paths_included"], false);
assert_eq!(redacted["local_paths_redacted"], true);
assert!(redacted.get("graph_path").is_none());
assert!(redacted.get("search_projection_path").is_none());
assert!(!redacted.to_string().contains("debug_graph_path"));
assert!(!redacted.to_string().contains("debug_search_path"));
let debug = options.diagnostic_report_json(NowledgeMemOpenDiagnosticOptions {
include_local_paths: true,
});
assert_eq!(debug["debug_local_paths_included"], true);
assert_eq!(debug["local_paths_redacted"], false);
assert_eq!(debug["graph_path"], "debug_graph_path");
assert_eq!(debug["search_projection_path"], "debug_search_path");
}
#[test]
fn open_options_report_gates_advanced_compressed_vector_search_mode() {
let options = NowledgeMemOpenOptions::with_search_projection(
"redacted_graph_path",
"redacted_search_path",
NowledgeMemGraphMode::ShadowReadOnly,
)
.with_compressed_vector_search_mode(CompressedVectorSearchMode::Preferred);
let report = options.sanitized_report().json();
assert_eq!(report["compressed_vector_search_mode"], "disabled");
assert_eq!(
report["requested_compressed_vector_search_mode"],
"preferred"
);
assert_eq!(report["retrieval_projection_advisor"]["ready"], false);
assert_eq!(
report["retrieval_projection_advisor_blocker_codes"],
serde_json::json!([
"retrieval_projection_recall_evidence_missing",
"retrieval_projection_parity_evidence_missing",
"retrieval_projection_segment_not_advised"
])
);
assert!(!report.to_string().contains("redacted_graph_path"));
assert!(!report.to_string().contains("redacted_search_path"));
}
#[test]
fn open_options_report_allows_compressed_vector_search_with_advisor_evidence() {
let recall_report = ready_vector_recall_report();
let options = NowledgeMemOpenOptions::with_search_projection(
"redacted_graph_path",
"redacted_search_path",
NowledgeMemGraphMode::ShadowReadOnly,
)
.with_compressed_vector_search_mode(CompressedVectorSearchMode::Preferred)
.with_retrieval_projection_advisor(
NowledgeMemRetrievalProjectionAdvisor::cold_local_with_recall_parity(&recall_report),
);
let report = options.sanitized_report().json();
assert_eq!(report["compressed_vector_search_mode"], "preferred");
assert_eq!(
report["requested_compressed_vector_search_mode"],
"preferred"
);
assert_eq!(
report["adaptive_vector_backend_policy"]["flat_scan_memory_budget_bytes"],
16 * 1024 * 1024
);
assert_eq!(report["retrieval_projection_advisor"]["ready"], true);
assert_eq!(
report["retrieval_projection_advisor_blocker_codes"],
serde_json::json!([])
);
}
#[test]
fn open_options_report_rejects_inconsistent_recall_evidence() {
let mut recall_report = ready_vector_recall_report();
recall_report.recall_at_k_per_million = 0;
let options = NowledgeMemOpenOptions::with_search_projection(
"redacted_graph_path",
"redacted_search_path",
NowledgeMemGraphMode::ShadowReadOnly,
)
.with_compressed_vector_search_mode(CompressedVectorSearchMode::Required)
.with_retrieval_projection_advisor(
NowledgeMemRetrievalProjectionAdvisor::cold_local_with_recall_parity(&recall_report),
);
let report = options.sanitized_report().json();
assert_eq!(report["compressed_vector_search_mode"], "disabled");
assert_eq!(
report["retrieval_projection_advisor_blocker_codes"],
serde_json::json!(["retrieval_projection_recall_evidence_not_ready"])
);
}
#[test]
fn qualified_out_of_core_open_recomputes_production_evidence() {
let root = unique_nowledge_mem_test_dir("qualified_out_of_core_open");
let graph_path = root.join("graph");
let search_path = root.join("search");
let graph_commit_epoch = {
let mut db = Database::open(&graph_path).unwrap();
db.query("CREATE (:Memory {id: 'qualified-open'})").unwrap();
db.commit_epoch().unwrap()
};
{
let mut index = SearchIndex::open(&search_path).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "test-embedding".to_string(),
version: Some("v1".to_string()),
dimension: 2,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "qualified-open".to_string(),
title: "Qualified open".to_string(),
body: "Production evidence is recomputed".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: None,
metadata: BTreeMap::new(),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(graph_commit_epoch),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemOutOfCoreSearchProjection::open(&search_path).unwrap();
let projection_identity = projection.reader().production_qualification_identity();
drop(projection);
let expected_identity = production_identity(graph_commit_epoch);
let qualification = SearchLexicalProductionQualificationReport::evaluate_for_production(
projection_identity,
ProductionEvidenceBinding {
identity: expected_identity.clone(),
generated_at_unix_seconds: 1,
},
expected_identity.clone(),
SearchTopKScoreParity {
text: true,
vector: true,
hybrid: true,
},
SearchLexicalFeasibilityCoverage::default(),
SearchLexicalFeasibilityMetrics::default(),
);
let out_of_core_config = SearchOutOfCoreConfig {
spill_directory: root.join("spill"),
..SearchOutOfCoreConfig::default()
};
let options = NowledgeMemOpenOptions::with_qualified_out_of_core_search_projection(
&graph_path,
&search_path,
NowledgeMemGraphMode::ShadowReadOnly,
NowledgeMemQualifiedOutOfCoreSearchOptions {
config: out_of_core_config,
qualification,
expected_identity,
},
);
let report = options.sanitized_report().json();
assert_eq!(report["search_projection_role"], "qualified_out_of_core");
assert_eq!(report["search_production_qualification_bound"], false);
let error = NowledgeMemEmbeddedStore::open_with_options(options).unwrap_err();
assert!(error.to_string().contains("dataset_too_small"));
assert!(error.to_string().contains("workload_coverage_incomplete"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_opens_from_options_with_sanitized_report() {
let root = unique_nowledge_mem_test_dir("open_options");
let graph_path = root.join("graph");
let search_path = root.join("search");
let options = NowledgeMemOpenOptions::with_search_projection(
graph_path,
search_path,
NowledgeMemGraphMode::WritableCutover,
);
let (mut store, report) = NowledgeMemEmbeddedStore::open_with_options(options).unwrap();
store
.graph_mut()
.query("CREATE (:Memory {id: 'mem-open', title: 'Open options'})")
.unwrap();
assert_eq!(report.protocol, NOWLEDGE_MEM_OPEN_REPORT_PROTOCOL);
assert_eq!(report.mode, NowledgeMemGraphMode::WritableCutover);
assert_eq!(
report.compressed_vector_search_mode,
CompressedVectorSearchMode::Disabled
);
assert!(report.graph_opened);
assert!(report.search_projection_opened);
assert!(store.search_projection().is_some());
assert_eq!(
store
.graph_mut()
.query("MATCH (m:Memory {id: 'mem-open'}) RETURN m.title AS title")
.unwrap()
.rows
.len(),
1
);
}
#[test]
fn embedded_store_applies_incremental_graph_search_projection_delta() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'new', title: 'Incremental facade', content: 'Graph changes feed search projection'})")
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(SearchIndex::in_memory());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = store
.build_search_projection_graph_delta_request_from_freshness(Some(4))
.unwrap()
.expect("expected graph delta request");
let plan = store
.search_projection_graph_delta_background_work_plan(
&request,
BackgroundWorkHint::default(),
)
.expect("expected background work plan")
.unwrap();
let report = store.apply_search_projection_graph_delta(request).unwrap();
assert_eq!(plan.request.class, WorkClass::Projection);
assert_eq!(report.upserted_documents, 1);
assert_eq!(
store
.search_projection()
.unwrap()
.index()
.document("memory:new")
.unwrap()
.title,
"Incremental facade"
);
}
#[test]
fn initial_import_projection_uses_local_cursor_and_preserves_legacy_provenance() {
let root = unique_nowledge_mem_test_dir("initial_import_projection_local_cursor");
let graph_path = root.join("graph");
let search_path = root.join("search");
let options = NowledgeMemOpenOptions::with_search_projection(
graph_path,
search_path,
NowledgeMemGraphMode::WritableCutover,
);
let (mut store, _) = NowledgeMemEmbeddedStore::open_with_options(options).unwrap();
store
.graph_mut()
.query("CREATE (:Memory {id: 'm1', title: 'Imported'})")
.unwrap();
let local_graph_commit_epoch = store.graph().database().commit_epoch().unwrap();
let report = store
.apply_initial_import_projection_delta_and_checkpoint(
SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "m1".to_string(),
title: "Imported".to_string(),
body: "legacy import".to_string(),
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
}],
deletes: Vec::new(),
max_operations: Some(1),
source_graph_commit_epoch: Some(77),
},
77,
)
.unwrap();
assert_eq!(
report.source_graph_commit_epoch_after,
Some(local_graph_commit_epoch)
);
let freshness = store
.search_projection()
.unwrap()
.index()
.projection_freshness();
assert_eq!(freshness.import_source_graph_commit_epoch, Some(77));
assert_eq!(
freshness.source_graph_commit_epoch,
Some(local_graph_commit_epoch)
);
assert_eq!(
freshness.durable_source_graph_commit_epoch,
Some(local_graph_commit_epoch)
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_runs_durable_projection_catch_up() {
let root = unique_nowledge_mem_test_dir("embedded_projection_catch_up");
let search_path = root.join("search");
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'm1', title: 'First'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'm2', title: 'Second'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let report = handle.catch_up_search_projection(1, 4).unwrap();
assert!(report.complete);
assert_eq!(report.end_applied_epoch, report.end_durable_epoch);
drop(handle);
let reopened = SearchIndex::open(&search_path).unwrap();
assert!(reopened.document("memory:m1").is_some());
assert!(reopened.document("memory:m2").is_some());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_runs_unified_relational_projection_catch_up() {
let root = unique_nowledge_mem_test_dir("embedded_relational_projection_catch_up");
let search_path = root.join("search");
let mut db = Database::new();
db.query_sql("CREATE TABLE thread_messages (id BIGINT PRIMARY KEY, body TEXT NOT NULL)")
.unwrap();
db.query_sql(
"INSERT INTO thread_messages (id, body) VALUES (1, 'Embedded relational document')",
)
.unwrap();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let report = handle
.catch_up_search_projection_with_relational(1, 1, |snapshot, batch| {
assert_eq!(batch.relational_primary_key_changes().len(), 1);
let output = snapshot.query_sql_with_params(
"SELECT body FROM thread_messages WHERE id = $1",
&[Value::Int(1)],
)?;
let body = match output.rows.as_slice() {
[row] => match row.get("body") {
Some(Value::String(body)) => body.clone(),
value => {
return Err(HawDBError::Execution(format!(
"thread_messages body expected STRING, got {value:?}"
)));
}
},
rows => {
return Err(HawDBError::Execution(format!(
"thread_messages hydration returned {} rows",
rows.len()
)));
}
};
Ok(SearchProjectionRelationalDelta {
delta: SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Message,
external_id: "1".to_string(),
title: String::new(),
body,
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
}],
..SearchProjectionDelta::default()
},
processed_primary_key_count: 1,
})
})
.unwrap();
assert!(report.complete);
assert_eq!(report.applied_operation_count, 1);
assert_eq!(report.end_applied_epoch, report.end_durable_epoch);
drop(handle);
let reopened = SearchIndex::open(&search_path).unwrap();
assert_eq!(
reopened
.document("message:1")
.map(|document| document.content.as_str()),
Some("Embedded relational document")
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_runs_unified_graph_batch_hydrator() {
let root = unique_nowledge_mem_test_dir("embedded_graph_batch_projection_catch_up");
let search_path = root.join("search");
let mut db = Database::new();
db.query("CREATE (:Thread {id: 'thread-a', title: 'Updated title'})")
.unwrap();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
handle.catch_up_search_projection(1, 1).unwrap();
handle
.with_transaction(|transaction| {
transaction
.query("MATCH (t:Thread {id: 'thread-a'}) SET t.title = 'Current title'")?;
Ok(())
})
.unwrap();
let mut observed_graph_only_batch = false;
let report = handle
.catch_up_search_projection_with_batch_hydrator(1, 2, 1, |_snapshot, batch| {
observed_graph_only_batch = !batch.has_relational_changes()
&& batch.graph_delta().upsert_node_ids.len() == 1;
Ok(SearchProjectionRelationalDelta {
delta: SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Message,
external_id: "derived".to_string(),
title: String::new(),
body: "Graph-dependent document".to_string(),
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
}],
..SearchProjectionDelta::default()
},
processed_primary_key_count: 0,
})
})
.unwrap();
assert!(observed_graph_only_batch);
assert!(report.complete);
drop(handle);
let reopened = SearchIndex::open(&search_path).unwrap();
assert!(reopened.document("thread:thread-a").is_none());
assert!(reopened.document("message:derived").is_some());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_exposes_live_graph_and_projection_watermarks() {
let root = unique_nowledge_mem_test_dir("embedded_runtime_watermarks");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'm1', title: 'Runtime status'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let status_before = handle.runtime_status().unwrap();
assert_eq!(status_before.graph_commit_epoch, 1);
assert_eq!(status_before.changefeed.retained_mutation_count, 1);
assert_eq!(
status_before
.projection_freshness
.as_ref()
.unwrap()
.durable_source_graph_commit_epoch,
None
);
assert_eq!(status_before.projection_commit_lag(), 1);
assert!(status_before.projection_stale());
handle.catch_up_search_projection(16, 1).unwrap();
let status_after = handle.runtime_status().unwrap();
let after = status_after.projection_freshness.as_ref().unwrap();
assert_eq!(after.source_graph_commit_epoch, Some(1));
assert_eq!(after.durable_source_graph_commit_epoch, Some(1));
assert!(!after.has_uncheckpointed_changes);
assert_eq!(status_after.projection_commit_lag(), 0);
assert!(!status_after.projection_stale());
assert_eq!(
status_after.json()["projection"]["durable_source_graph_commit_epoch"],
1
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn production_status_does_not_claim_graph_cutover_without_route_ownership() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let status = store.production_status(None).unwrap();
let json = status.json();
assert_eq!(status.protocol, NOWLEDGE_MEM_PRODUCTION_STATUS_PROTOCOL);
assert!(status.graph_open);
assert!(!status.graph_read_only);
assert!(!status.graph_route_ownership_present);
assert!(!status.graph_hawdb_cutover_effective);
assert!(!status.search_projection_open);
assert!(!status.search_hawdb_cutover_effective);
assert!(status
.blocker_codes
.contains(&"graph_route_ownership_missing".to_string()));
assert!(status
.blocker_codes
.contains(&"search_projection_not_open".to_string()));
assert_eq!(json["graph"]["hawdb_cutover_effective"], false);
assert_eq!(json["search"]["hawdb_cutover_effective"], false);
assert_eq!(json["redaction"]["local_paths_copied"], false);
}
#[test]
fn production_status_reports_partial_graph_ownership_without_cutover_claim() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let route_ownership = nowledge_mem_route_ownership_readiness(
&nowledge_mem_route_ownership_all_legacy(),
Some(&ready_route_readiness_summary()),
NowledgeMemRouteOwnershipPolicy::migration(),
);
let status = store.production_status(Some(&route_ownership)).unwrap();
assert!(status.graph_route_ownership_present);
assert!(status.graph_route_ownership_ready);
assert_eq!(status.graph_hawdb_route_count, 0);
assert_eq!(
status.graph_legacy_route_count,
REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES.len()
);
assert!(!status.graph_hawdb_cutover_effective);
assert!(status
.blocker_codes
.contains(&"graph_legacy_routes_remaining".to_string()));
}
#[test]
fn production_status_separates_graph_cutover_from_search_freshness() {
let root = unique_nowledge_mem_test_dir("production_status_split_cutover");
let graph_path = root.join("graph");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
graph
.query("CREATE (:Memory {id: 'status-m1', title: 'Production status'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let route_ownership = nowledge_mem_route_ownership_readiness(
&nowledge_mem_route_ownership_all_hawdb(),
Some(&ready_route_readiness_summary()),
NowledgeMemRouteOwnershipPolicy::production_cutover(),
);
let stale = store.production_status(Some(&route_ownership)).unwrap();
assert!(stale.graph_hawdb_cutover_effective);
assert!(stale.search_projection_open);
assert_eq!(stale.search_projection_commit_lag, 2);
assert!(stale.search_projection_stale);
assert!(!stale.search_hawdb_cutover_effective);
assert!(stale
.blocker_codes
.contains(&"search_projection_stale".to_string()));
store.catch_up_search_projection(16, 1).unwrap();
let ready = store.production_status(Some(&route_ownership)).unwrap();
assert!(ready.graph_hawdb_cutover_effective);
assert!(ready.search_projection_open);
assert_eq!(ready.search_projection_commit_lag, 0);
assert!(!ready.search_projection_stale);
assert!(ready.search_hawdb_cutover_effective);
assert!(!ready
.blocker_codes
.contains(&"search_projection_stale".to_string()));
assert_eq!(
ready.json()["route_ownership"]["production_cutover_ready"],
true
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn cutover_controls_keep_legacy_reads_ready_without_hawdb_ownership() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let report = store
.cutover_controls_report(NowledgeMemCutoverControls::legacy(), None)
.unwrap();
let json = report.json();
assert_eq!(report.protocol, NOWLEDGE_MEM_CUTOVER_CONTROLS_PROTOCOL);
assert!(report.ready);
assert!(!report.graph_read_selected_hawdb);
assert!(report.graph_read_effective);
assert!(!report.search_read_selected_hawdb);
assert!(report.search_read_effective);
assert!(!report.dual_writes_enabled);
assert_eq!(json["controls"]["graph_reads"], "legacy");
assert_eq!(json["controls"]["search_reads"], "legacy");
assert_eq!(json["redaction"]["local_paths_copied"], false);
}
#[test]
fn source_mutation_owner_preserves_public_type_identity_and_json() {
use hawdb_readiness::source_mutation as owner;
let evidence: Vec<NowledgeMemSourceMutationDualWriteEvidence> =
owner::nowledge_mem_source_mutation_dual_write_evidence_all_ready();
let same_evidence: &[owner::NowledgeMemSourceMutationDualWriteEvidence] = &evidence;
let report: crate::NowledgeMemSourceMutationDualWriteReadinessReport =
owner::nowledge_mem_source_mutation_dual_write_readiness(same_evidence);
let requirements: Vec<crate::NowledgeMemSourceMutationFamilyRequirement> =
owner::nowledge_mem_source_mutation_family_requirements();
assert_eq!(requirements, report.requirements);
assert_eq!(
evidence,
nowledge_mem_source_mutation_dual_write_evidence_all_ready()
);
assert_eq!(
report,
nowledge_mem_source_mutation_dual_write_readiness(&evidence)
);
assert_eq!(
report,
crate::nowledge_mem_source_mutation_dual_write_readiness(&evidence)
);
assert_eq!(report.json()["ready"], true);
assert_eq!(
REQUIRED_NOWLEDGE_MEM_SOURCE_MUTATION_FAMILIES,
owner::REQUIRED_NOWLEDGE_MEM_SOURCE_MUTATION_FAMILIES
);
assert_eq!(
NOWLEDGE_MEM_SOURCE_MUTATION_DUAL_WRITE_READINESS_PROTOCOL,
"hawdb-nowledge-mem-source-mutation-dual-write-readiness-v1"
);
assert_eq!(
crate::NOWLEDGE_MEM_SOURCE_MUTATION_DUAL_WRITE_READINESS_PROTOCOL,
owner::NOWLEDGE_MEM_SOURCE_MUTATION_DUAL_WRITE_READINESS_PROTOCOL
);
}
#[test]
fn source_mutation_owner_report_reaches_replacement_summary() {
let mut evidence = nowledge_mem_source_mutation_dual_write_evidence_all_ready();
for expected_ready in [true, false] {
if !expected_ready {
evidence
.iter_mut()
.find(|item| item.family == NOWLEDGE_MEM_SOURCE_MUTATION_FAMILY_INGEST_CREATE)
.unwrap()
.independent_watermarks_recorded = false;
}
let report = nowledge_mem_source_mutation_dual_write_readiness(&evidence);
for bundle in [
serde_json::json!({"source_mutation_dual_write_readiness": report.json()}),
serde_json::json!({"cutover_evidence": {"source_mutation_dual_write_readiness": report.json()}}),
] {
let summary =
crate::replacement_summary::nowledge_replacement_summary_json(&bundle);
let summary = &summary["source_mutation_dual_write_readiness"];
assert_eq!(summary["present"], true);
assert_eq!(summary["ready"], expected_ready);
assert_eq!(
summary["blocker_codes"],
serde_json::json!(report.blocker_codes)
);
assert_eq!(
summary["required_family_count"],
serde_json::json!(REQUIRED_NOWLEDGE_MEM_SOURCE_MUTATION_FAMILIES.len())
);
}
}
}
#[test]
fn cutover_controls_fail_closed_when_hawdb_reads_are_not_effective() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let report = store
.cutover_controls_report(NowledgeMemCutoverControls::hawdb_reads(), None)
.unwrap();
assert!(!report.ready);
assert!(report.graph_read_selected_hawdb);
assert!(!report.graph_read_effective);
assert!(report.search_read_selected_hawdb);
assert!(!report.search_read_effective);
assert!(report.projection_catch_up_enabled);
assert!(report
.blocker_codes
.contains(&"graph_read_selected_hawdb_but_not_effective".to_string()));
assert!(report
.blocker_codes
.contains(&"search_read_selected_hawdb_but_not_effective".to_string()));
assert_eq!(
report.json()["production_status"]["graph"]["hawdb_cutover_effective"],
false
);
}
#[test]
fn cutover_controls_require_dual_writes_for_initial_import() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let controls = NowledgeMemCutoverControls {
initial_import: NowledgeMemWorkControl::Enabled,
..NowledgeMemCutoverControls::legacy()
};
let report = store.cutover_controls_report(controls, None).unwrap();
assert!(!report.ready);
assert!(report.initial_import_enabled);
assert!(!report.dual_writes_enabled);
assert!(!report.initial_import_inactive_for_cutover);
assert!(report
.blocker_codes
.contains(&"initial_import_enabled_without_dual_writes".to_string()));
assert!(report
.blocker_codes
.contains(&"initial_import_active_blocks_read_cutover".to_string()));
}
fn ready_initial_import_cutover_catch_up_report(
) -> HawDBLightningInitialImportCutoverCatchUpReport {
HawDBLightningInitialImportCutoverCatchUpReport {
ready: true,
session_ready_for_cutover: true,
durable_state_present: true,
live_projection_present: true,
import_graph_commit_epoch: Some(42),
import_durable_search_projection_commit_epoch: Some(42),
live_graph_commit_epoch: 42,
live_search_projection_commit_epoch: Some(42),
live_durable_search_projection_commit_epoch: Some(42),
graph_watermark_caught_up: true,
search_projection_watermark_caught_up: true,
live_projection_checkpointed: true,
live_projection_healthy: true,
cutover_watermark: Some(42),
blocker_codes: Vec::new(),
}
}
fn ready_initial_import_recovery_report(
) -> crate::HawDBLightningInitialImportRecoveryReadinessReport {
let mut source = Database::new();
source
.query("CREATE (:Memory {id: 'import-root'})-[:LINKS {id: 'import-rel'}]->(:Entity {id: 'import-entity'})")
.unwrap();
let export = source.prepare_hawdb_lightning_bootstrap_export().unwrap();
let checkpoint = HawDBLightningInitialImportCheckpoint {
protocol_version: 1,
import_id: "import-controls".to_string(),
task_id: "import-controls-task".to_string(),
fencing_token: "import-controls-fence".to_string(),
object_digest: "import-controls-digest".to_string(),
schema_checksum: export.manifest.schema_checksum,
graph_stream_checksum: export.manifest.graph_stream_checksum,
graph_stream_byte_len: export.manifest.graph_stream_byte_len,
relational_stream_checksum: export.manifest.relational_stream_checksum,
relational_stream_byte_len: export.manifest.relational_stream_byte_len,
manifest_database_commit_epoch: export.manifest.database_commit_epoch,
manifest_graph_commit_epoch: export.manifest.graph_commit_epoch,
applied_graph_commit_epoch: export.manifest.graph_commit_epoch,
applied_search_projection_commit_epoch: Some(export.manifest.graph_commit_epoch),
durable_search_projection_commit_epoch: Some(export.manifest.graph_commit_epoch),
completed_batches: 1,
total_batches: 1,
document_identity_count: 6,
};
let identities = [
SearchProjectionKind::Memory,
SearchProjectionKind::Message,
SearchProjectionKind::Entity,
SearchProjectionKind::Source,
SearchProjectionKind::SourceChunk,
SearchProjectionKind::Community,
]
.into_iter()
.map(|kind| HawDBLightningInitialImportDocumentIdentity {
kind,
document_id: format!("{}:import", kind.as_str()),
})
.collect::<Vec<_>>();
let durable_state = crate::hawdb_lightning_initial_import_durable_state_report(
&export.manifest,
&checkpoint,
&identities,
)
.state
.expect("expected persistable durable state");
let durable_state_payload =
crate::hawdb_lightning_initial_import_encode_durable_state(&durable_state).unwrap();
let projection_rows = identities
.iter()
.map(|identity| SearchProjectionRow {
kind: identity.kind,
external_id: identity.document_id.clone(),
title: "initial import".to_string(),
body: "initial import".to_string(),
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
})
.collect::<Vec<_>>();
let projection_delta = SearchProjectionDelta {
upserts: projection_rows,
deletes: Vec::new(),
max_operations: Some(6),
source_graph_commit_epoch: Some(export.manifest.graph_commit_epoch),
};
let freshness = SearchProjectionFreshness {
document_count: 6,
import_source_graph_commit_epoch: Some(export.manifest.graph_commit_epoch),
source_graph_commit_epoch: Some(export.manifest.graph_commit_epoch),
durable_source_graph_commit_epoch: Some(export.manifest.graph_commit_epoch),
has_uncheckpointed_changes: false,
full_reindex_needed: false,
full_reindex_reasons: Vec::new(),
metadata_repair_needed: false,
metadata_repair_reasons: Vec::new(),
embedding_model: None,
embedding_version: None,
embedding_dimension: None,
};
let projection_batches = [projection_delta];
let recovery = source
.hawdb_lightning_initial_import_recovery_readiness(
HawDBLightningInitialImportReadinessInputs {
encoded_graph_stream: &export.graph_stream.encoded,
encoded_relational_stream: &export.relational_stream.encoded,
manifest: &export.manifest,
projection_batches: &projection_batches,
target_projection_freshness: Some(&freshness),
live_projection_freshness: Some(&freshness),
},
Some(&durable_state_payload),
)
.unwrap();
assert!(recovery.ready);
recovery
}
#[test]
fn cutover_controls_accept_active_initial_import_with_cutover_catch_up_proof() {
let root = unique_nowledge_mem_test_dir("cutover_controls_initial_import_catch_up");
let graph_path = root.join("graph");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
graph
.query("CREATE (:Memory {id: 'controls-import-m1', title: 'Import cutover'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
store.catch_up_search_projection(16, 1).unwrap();
let route_ownership = nowledge_mem_route_ownership_readiness(
&nowledge_mem_route_ownership_all_hawdb(),
Some(&ready_route_readiness_summary()),
NowledgeMemRouteOwnershipPolicy::production_cutover(),
);
let controls = NowledgeMemCutoverControls {
dual_writes: NowledgeMemWorkControl::Enabled,
initial_import: NowledgeMemWorkControl::Enabled,
projection_catch_up: NowledgeMemWorkControl::Enabled,
graph_reads: super::NowledgeMemReadControl::HawDB,
search_reads: super::NowledgeMemReadControl::HawDB,
};
let catch_up = ready_initial_import_cutover_catch_up_report();
let report = store
.cutover_controls_report_with_initial_import_cutover_catch_up(
controls,
Some(&route_ownership),
Some(&catch_up),
)
.unwrap();
assert!(report.ready);
assert!(report.initial_import_enabled);
assert!(!report.initial_import_inactive_for_cutover);
assert!(report.initial_import_cutover_catch_up_ready);
assert!(report.initial_import_safe_for_read_cutover);
assert!(report.blocker_codes.is_empty());
assert_eq!(
report.json()["work"]["initial_import_safe_for_read_cutover"],
true
);
}
#[test]
fn cutover_controls_require_recovery_proof_for_active_initial_import() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let controls = NowledgeMemCutoverControls {
dual_writes: NowledgeMemWorkControl::Enabled,
initial_import: NowledgeMemWorkControl::Enabled,
..NowledgeMemCutoverControls::legacy()
};
let report = store
.cutover_controls_report_with_initial_import_recovery(controls, None, None)
.unwrap();
assert!(!report.ready);
assert!(report
.blocker_codes
.contains(&"initial_import_recovery_not_ready".to_string()));
assert!(report
.blocker_codes
.contains(&"initial_import_active_blocks_read_cutover".to_string()));
}
#[test]
fn cutover_controls_accept_active_initial_import_with_recovery_proof() {
let root = unique_nowledge_mem_test_dir("cutover_controls_initial_import_recovery");
let graph_path = root.join("graph");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
graph
.query("CREATE (:Memory {id: 'controls-recovery-m1', title: 'Import recovery'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
store.catch_up_search_projection(16, 1).unwrap();
let route_ownership = nowledge_mem_route_ownership_readiness(
&nowledge_mem_route_ownership_all_hawdb(),
Some(&ready_route_readiness_summary()),
NowledgeMemRouteOwnershipPolicy::production_cutover(),
);
let controls = NowledgeMemCutoverControls {
dual_writes: NowledgeMemWorkControl::Enabled,
initial_import: NowledgeMemWorkControl::Enabled,
projection_catch_up: NowledgeMemWorkControl::Enabled,
graph_reads: super::NowledgeMemReadControl::HawDB,
search_reads: super::NowledgeMemReadControl::HawDB,
};
let recovery = ready_initial_import_recovery_report();
let report = store
.cutover_controls_report_with_initial_import_recovery(
controls,
Some(&route_ownership),
Some(&recovery),
)
.unwrap();
assert!(report.ready);
assert!(report.initial_import_cutover_catch_up_ready);
assert!(report.initial_import_safe_for_read_cutover);
assert!(report.blocker_codes.is_empty());
}
#[test]
fn cutover_controls_block_read_cutover_while_initial_import_is_active() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let controls = NowledgeMemCutoverControls {
dual_writes: NowledgeMemWorkControl::Enabled,
initial_import: NowledgeMemWorkControl::Enabled,
..NowledgeMemCutoverControls::legacy()
};
let report = store.cutover_controls_report(controls, None).unwrap();
assert!(!report.ready);
assert!(report.dual_writes_enabled);
assert!(report.initial_import_enabled);
assert!(!report.initial_import_inactive_for_cutover);
assert_eq!(
report.json()["work"]["initial_import_inactive_for_cutover"],
false
);
assert_eq!(
report.blocker_codes,
vec!["initial_import_active_blocks_read_cutover".to_string()]
);
}
#[test]
fn cutover_controls_accept_independent_hawdb_reads_after_status_is_ready() {
let root = unique_nowledge_mem_test_dir("cutover_controls_ready_reads");
let graph_path = root.join("graph");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
graph
.query("CREATE (:Memory {id: 'controls-m1', title: 'Cutover controls'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
store.catch_up_search_projection(16, 1).unwrap();
let route_ownership = nowledge_mem_route_ownership_readiness(
&nowledge_mem_route_ownership_all_hawdb(),
Some(&ready_route_readiness_summary()),
NowledgeMemRouteOwnershipPolicy::production_cutover(),
);
let report = store
.cutover_controls_report(
NowledgeMemCutoverControls::hawdb_reads(),
Some(&route_ownership),
)
.unwrap();
assert!(report.ready);
assert!(report.graph_read_selected_hawdb);
assert!(report.graph_read_effective);
assert!(report.search_read_selected_hawdb);
assert!(report.search_read_effective);
assert!(report.dual_writes_enabled);
assert!(report.projection_catch_up_enabled);
assert!(report.blocker_codes.is_empty());
assert_eq!(
report.json()["production_status"]["search"]["hawdb_cutover_effective"],
true
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_catches_up_delete_after_graph_checkpoint_and_restart() {
let root = unique_nowledge_mem_test_dir("embedded_projection_checkpoint_restart");
let graph_path = root.join("graph");
let search_path = root.join("search");
{
let graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
store
.graph_mut()
.query("CREATE (:Memory {id: 'm1', title: 'Removed after checkpoint'})")
.unwrap();
let initial_report = store.catch_up_search_projection(16, 1).unwrap();
assert!(initial_report.complete);
assert!(store
.search_projection()
.unwrap()
.index()
.document("memory:m1")
.is_some());
store
.graph_mut()
.query("MATCH (m:Memory {id: 'm1'}) DETACH DELETE m")
.unwrap();
store.graph_mut().database_mut().checkpoint().unwrap();
}
{
let graph =
NowledgeMemGraph::open(&graph_path, NowledgeMemGraphMode::WritableCutover).unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let report = store.catch_up_search_projection(16, 1).unwrap();
assert!(report.complete);
assert_eq!(report.start_durable_epoch, Some(2));
assert_eq!(report.end_durable_epoch, Some(3));
assert!(store
.search_projection()
.unwrap()
.index()
.document("memory:m1")
.is_none());
}
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_does_not_split_graph_commit_across_projection_batches() {
let root = unique_nowledge_mem_test_dir("embedded_projection_commit_boundary");
let search_path = root.join("search");
let mut db = Database::new();
{
let mut transaction = db.begin_transaction().unwrap();
transaction
.query("CREATE (:Memory {id: 'm1', title: 'First'})")
.unwrap();
transaction
.query("CREATE (:Memory {id: 'm2', title: 'Second'})")
.unwrap();
transaction.commit().unwrap();
}
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let error = store.catch_up_search_projection(1, 1).unwrap_err();
assert!(error.to_string().contains(
"search projection change at commit epoch 1 requires 2 operations, exceeding configured per-batch limit 1"
));
let projection = store.search_projection().unwrap();
assert_eq!(
projection
.index()
.projection_freshness()
.durable_source_graph_commit_epoch,
None
);
assert!(projection.index().document("memory:m1").is_none());
assert!(projection.index().document("memory:m2").is_none());
let report = store.catch_up_search_projection(2, 1).unwrap();
assert!(report.complete);
assert_eq!(report.end_durable_epoch, Some(1));
assert!(store
.search_projection()
.unwrap()
.index()
.document("memory:m1")
.is_some());
assert!(store
.search_projection()
.unwrap()
.index()
.document("memory:m2")
.is_some());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_reuses_open_store_for_knowledge_retrieval() {
let root = unique_nowledge_mem_test_dir("embedded_handle_retrieval");
let search_path = root.join("search");
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'm1', title: 'Reusable handle', content: 'embedded retrieval'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
handle.catch_up_search_projection(16, 1).unwrap();
let output = handle
.retrieve_knowledge(&KnowledgeRetrievalRequest {
query_text: "reusable handle".to_string(),
query_embedding: None,
mode: SearchMode::Text,
limit: 10,
offset: 0,
rank_window: None,
search_fusion_weights: SearchFusionWeights::default(),
metadata_filters: BTreeMap::new(),
candidate_limit: None,
candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
graph_seed_limit: 4,
graph_context_limit: 4,
graph_context_max_hops: 1,
})
.unwrap();
assert_eq!(output.search.total_hits, 1);
assert_eq!(output.search.hits[0].external_id.as_deref(), Some("m1"));
drop(handle);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_background_delta_uses_scheduler_qos() {
let db = Database::new_with_config(DatabaseConfig {
local_qos_policy: LocalQosPolicy {
max_total_background_operations: Some(0),
..LocalQosPolicy::default()
},
..DatabaseConfig::default()
});
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'new', title: 'Scheduled facade'})")
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(SearchIndex::in_memory());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = store
.build_search_projection_graph_delta_request_from_freshness(Some(4))
.unwrap()
.expect("expected graph delta request");
let error = store
.apply_scheduled_background_search_projection_graph_delta(request)
.unwrap_err();
assert!(error
.to_string()
.contains("background search projection graph delta"));
assert!(store
.search_projection()
.unwrap()
.index()
.document("memory:new")
.is_none());
}
#[test]
fn embedded_store_scheduled_catch_up_reports_qos_deferral_without_applying() {
let root = unique_nowledge_mem_test_dir("embedded_scheduled_catch_up_deferred");
let search_path = root.join("search");
let database = Database::new_with_config(DatabaseConfig {
local_qos_policy: LocalQosPolicy {
max_total_background_operations: Some(0),
..LocalQosPolicy::default()
},
..DatabaseConfig::default()
});
let mut graph =
NowledgeMemGraph::from_database(database, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'new', title: 'Scheduled facade'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let report = store.catch_up_search_projection_scheduled(4, 1).unwrap();
assert_eq!(
report.stop_reason,
crate::SearchProjectionCatchUpStopReason::Deferred(
crate::QosAdmissionCode::TotalBackgroundLimitExceeded
)
);
assert_eq!(report.catch_up.applied_batch_count, 0);
assert!(!report.catch_up.complete);
let projection = store.search_projection().unwrap();
assert_eq!(
projection
.index()
.projection_freshness()
.durable_source_graph_commit_epoch,
None
);
assert!(projection.index().document("memory:new").is_none());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_handle_scheduled_catch_up_checkpoints_before_returning() {
let root = unique_nowledge_mem_test_dir("embedded_handle_scheduled_catch_up");
let search_path = root.join("search");
let database = Database::new();
let scheduler = database.local_qos_scheduler();
let mut graph =
NowledgeMemGraph::from_database(database, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'new', title: 'Scheduled facade'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let report = handle.catch_up_search_projection_scheduled(4, 1).unwrap();
assert_eq!(
report.stop_reason,
crate::SearchProjectionCatchUpStopReason::CaughtUp
);
assert!(report.catch_up.complete);
assert_eq!(report.catch_up.applied_batch_count, 1);
assert_eq!(
report.catch_up.end_applied_epoch,
report.catch_up.end_durable_epoch
);
assert_eq!(scheduler.state().running_background_operations, 0);
drop(handle);
let reopened = SearchIndex::open(&search_path).unwrap();
assert!(reopened.document("memory:new").is_some());
assert_eq!(
reopened
.projection_freshness()
.durable_source_graph_commit_epoch,
report.catch_up.end_durable_epoch
);
drop(reopened);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_scheduled_catch_up_reports_batch_budget_exhaustion() {
let root = unique_nowledge_mem_test_dir("embedded_scheduled_catch_up_budget");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'first', title: 'First'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'second', title: 'Second'})")
.unwrap();
let projection =
NowledgeMemSearchProjection::from_index(SearchIndex::open(&search_path).unwrap());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let report = store.catch_up_search_projection_scheduled(1, 1).unwrap();
assert_eq!(
report.stop_reason,
crate::SearchProjectionCatchUpStopReason::BatchBudgetExhausted
);
assert!(!report.catch_up.complete);
assert_eq!(report.catch_up.applied_batch_count, 1);
assert_eq!(report.catch_up.applied_operation_count, 1);
assert_eq!(report.catch_up.end_durable_epoch, Some(1));
let projection = store.search_projection().unwrap();
assert!(projection.index().document("memory:first").is_some());
assert!(projection.index().document("memory:second").is_none());
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_retrieves_knowledge_through_search_projection() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-search', title: 'Facade retrieval', content: 'HawDB replaces LanceDB retrieval'})")
.unwrap();
graph
.query("CREATE (:Entity {id: 'entity-hawdb', name: 'HawDB'})")
.unwrap();
graph
.query("MATCH (m:Memory {id: 'mem-search'}), (e:Entity {id: 'entity-hawdb'}) CREATE (m)-[:MENTIONS]->(e)")
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(SearchIndex::in_memory());
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let delta = store
.build_search_projection_graph_delta_request_from_freshness(Some(8))
.unwrap()
.expect("expected search projection delta");
store.apply_search_projection_graph_delta(delta).unwrap();
let retrieval = store
.retrieve_knowledge_with_report(&KnowledgeRetrievalRequest {
query_text: "facade retrieval".to_string(),
query_embedding: None,
mode: SearchMode::Text,
limit: 10,
offset: 0,
rank_window: None,
search_fusion_weights: SearchFusionWeights::default(),
metadata_filters: BTreeMap::new(),
candidate_limit: None,
candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
graph_seed_limit: 4,
graph_context_limit: 4,
graph_context_max_hops: 1,
})
.unwrap();
let output = retrieval.output;
let report = retrieval.report;
assert_eq!(output.search.total_hits, 1);
assert_eq!(output.search.hits[0].id, "memory:mem-search");
assert_eq!(output.diagnostics.projection_commit_lag, 0);
assert!(!output.evidence.is_empty());
assert_eq!(report.protocol, NOWLEDGE_MEM_RETRIEVAL_REPORT_PROTOCOL);
assert_eq!(report.mode, NowledgeMemGraphMode::WritableCutover);
assert_eq!(
report.compressed_vector_search_mode,
CompressedVectorSearchMode::Disabled
);
assert_eq!(report.search_total_hits, 1);
assert!(report.candidate_count >= 1);
assert!(report.evidence_count >= 1);
assert_eq!(report.text_backend, Some("bm25_text".to_string()));
assert_eq!(
report.json()["protocol"],
NOWLEDGE_MEM_RETRIEVAL_REPORT_PROTOCOL
);
}
#[test]
fn embedded_handle_serves_search_and_graph_context_without_document_residency() {
let root = unique_nowledge_mem_test_dir("embedded_out_of_core_serving");
let search_path = root.join("search");
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-ooc', title: 'Bounded serving', content: 'HawDB out of core retrieval'})")
.unwrap();
graph
.query("CREATE (:Entity {id: 'entity-ooc', name: 'OutOfCore'})")
.unwrap();
graph
.query("MATCH (m:Memory {id: 'mem-ooc'}), (e:Entity {id: 'entity-ooc'}) CREATE (m)-[:MENTIONS]->(e)")
.unwrap();
let graph_commit_epoch = graph.database().commit_epoch().unwrap();
{
let mut index = SearchIndex::open(&search_path).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "test-embedding".to_string(),
version: Some("v1".to_string()),
dimension: 2,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-ooc".to_string(),
title: "Bounded serving".to_string(),
body: "HawDB out of core retrieval".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-ooc".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(graph_commit_epoch),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemOutOfCoreSearchProjection::open(&search_path).unwrap();
assert_eq!(projection.reader().resident_document_count(), 0);
let handle = NowledgeMemEmbeddedStoreHandle::new(
NowledgeMemEmbeddedStore::new_with_out_of_core_search(
graph,
projection,
NowledgeMemRetrievalProjectionAdvisor::default(),
),
)
.unwrap();
let candidates = handle
.search_candidates_with_report(&NowledgeMemSearchCandidateRequest::text(
"bounded serving",
1,
))
.unwrap();
assert_eq!(candidates.result.hits[0].id, "memory:mem-ooc");
assert!(candidates.out_of_core_metrics.is_some());
let hydration = handle
.search_projection_documents_with_report(&["memory:mem-ooc".to_string()], 1)
.unwrap();
assert_eq!(hydration.documents[0].id, "memory:mem-ooc");
assert_eq!(
hydration
.out_of_core_metrics
.as_ref()
.unwrap()
.hydrated_documents,
1
);
let retrieval = handle
.retrieve_knowledge_with_report(&KnowledgeRetrievalRequest {
query_text: "bounded serving".to_string(),
query_embedding: None,
mode: SearchMode::Text,
limit: 1,
offset: 0,
rank_window: None,
search_fusion_weights: SearchFusionWeights::default(),
metadata_filters: BTreeMap::new(),
candidate_limit: Some(4),
candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
graph_seed_limit: 4,
graph_context_limit: 4,
graph_context_max_hops: 1,
})
.unwrap();
assert_eq!(retrieval.output.search.hits[0].id, "memory:mem-ooc");
assert!(!retrieval.output.graph_context_paths.is_empty());
assert!(retrieval.out_of_core_search_metrics.is_some());
assert_eq!(
retrieval
.output
.projection_freshness
.source_graph_commit_epoch,
Some(graph_commit_epoch)
);
let vector = handle
.query_with_params_with_report(
"CALL vector_search($embedding, topK := 1) RETURN id, score",
&BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]),
)
.unwrap();
assert_eq!(vector.output.rows.len(), 1);
assert_eq!(
vector.output.rows[0].get("id"),
Some(&Value::String("memory:mem-ooc".to_string()))
);
drop(handle);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_projection_candidate_api_reports_metadata_pushdown() {
let root = unique_nowledge_mem_test_dir("search_candidate_api_pushdown");
{
let mut index = SearchIndex::open(&root).unwrap();
for (external_id, lifecycle_state) in [
("deleted", "deleted"),
("forgotten", "forgotten"),
("active", "active"),
] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.to_string(),
title: format!("{external_id} candidate"),
body: "metadata filtered candidate read".to_string(),
embedding: None,
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), lifecycle_state.to_string()),
]),
})
.unwrap();
}
upsert_excluded_search_projection_rows(&mut index, "excluded", 126, None);
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = NowledgeMemSearchCandidateRequest::text("candidate read", 10)
.with_metadata_filters(BTreeMap::from([(
"lifecycle_state__not_in".to_string(),
r#"["deleted","forgotten"]"#.to_string(),
)]));
let output = store.search_candidates(&request).unwrap();
assert_eq!(output.result.total_hits, 1);
assert_eq!(output.result.hits[0].id, "memory:active");
assert_eq!(
output.report.protocol,
NOWLEDGE_MEM_SEARCH_CANDIDATE_REPORT_PROTOCOL
);
assert_eq!(output.report.metadata_filter_count, 1);
assert_eq!(output.report.pushed_predicate_count, 1);
assert_eq!(output.report.residual_predicate_count, 0);
assert_eq!(output.report.segment_count, 2);
assert_eq!(output.report.pruned_segment_count, 1);
assert_eq!(output.report.scanned_segment_count, 1);
assert!(output.report.persisted_segment_descriptor_used);
assert_eq!(output.report.physical_range_read_count, 1);
assert!(output.report.physical_bytes_read > 0);
assert_eq!(output.report.filtered_out_count, 128);
assert_eq!(
output.report.json()["candidate_set"]["metadata_predicate_pushdown"]["field_summaries"]
[0]["field"],
"lifecycle_state"
);
let mut accumulator = NowledgeMemSearchCandidateShadowAccumulator::new();
accumulator.record_search_candidate_output(["memory:active"], &output);
let evidence = accumulator.json();
assert_eq!(evidence["ready"], true);
assert_eq!(
evidence["evidence_source"],
NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_SOURCE
);
assert_eq!(evidence["request_count"], 1);
assert_eq!(evidence["candidate_primary_engine"], "hawdb");
assert_eq!(evidence["candidate_identity"]["ready"], true);
assert_eq!(evidence["filter_pushdown"]["ready"], true);
assert_eq!(
evidence["filter_pushdown"]["field_capabilities_ready"],
true
);
assert_eq!(
evidence["filter_pushdown"]["field_summary_count"],
NOWLEDGE_SEARCH_PROJECTION_SCAN_FILTER_FIELDS.len()
);
assert_eq!(
evidence["filter_pushdown"]["missing_required_fields"],
serde_json::json!([])
);
assert!(evidence["filter_pushdown"]["field_summaries"]
.as_array()
.unwrap()
.iter()
.any(|summary| summary["field"] == "lifecycle_state"
&& summary["source"] == "persisted_segment_descriptor_contract"));
assert!(evidence["filter_pushdown"]["field_summaries"]
.as_array()
.unwrap()
.iter()
.any(|summary| summary["field"] == "confidence"
&& summary["numeric_range_summary_used"] == true));
let direct_evidence = store
.search_candidate_shadow_evidence_json(&request, ["memory:active"])
.unwrap();
assert_eq!(direct_evidence["ready"], true);
assert_eq!(
direct_evidence["evidence_source"],
NOWLEDGE_MEM_SEARCH_CANDIDATE_EVIDENCE_SOURCE
);
assert_eq!(direct_evidence["candidate_identity"]["ready"], true);
assert_eq!(direct_evidence["filter_pushdown"]["ready"], true);
assert_eq!(
direct_evidence["filter_pushdown"]["field_capabilities_ready"],
true
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_handle_hydrates_only_explicitly_bounded_search_documents() {
let root = unique_nowledge_mem_test_dir("bounded_search_document_hydration");
let mut index = SearchIndex::open(&root).unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "source-1-chunk-0".to_string(),
title: "Introduction".to_string(),
body: "bounded projection payload".to_string(),
embedding: None,
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([("chunk_index".to_string(), "0".to_string())]),
})
.unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(NowledgeMemSearchProjection::from_index(index)),
))
.unwrap();
let documents = handle
.search_projection_documents(&["source_chunk:source-1-chunk-0".to_string()], 1)
.unwrap();
assert_eq!(documents.len(), 1);
assert_eq!(documents[0].content, "bounded projection payload");
assert_eq!(documents[0].metadata["chunk_index"], "0");
assert!(handle
.search_projection_documents(
&[
"source_chunk:source-1-chunk-0".to_string(),
"source_chunk:source-1-chunk-1".to_string(),
],
1,
)
.is_err());
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.admissions, 1);
assert_eq!(snapshot.completions, 1);
}
#[test]
fn embedded_handle_search_hydration_rejects_payload_over_budget() {
for out_of_core in [false, true] {
let root = unique_nowledge_mem_test_dir("search_hydration_budget");
let graph = NowledgeMemGraph::from_database(
Database::new_with_config(DatabaseConfig {
max_read_result_payload_bytes: Some(64),
..DatabaseConfig::default()
}),
NowledgeMemGraphMode::ShadowReadOnly,
);
let mut index = if out_of_core {
SearchIndex::open(&root).unwrap()
} else {
SearchIndex::in_memory()
};
index
.upsert(crate::SearchDocument {
id: "source_chunk:large".to_string(),
title: "Large".to_string(),
content: "x".repeat(256),
embedding: None,
metadata: BTreeMap::new(),
})
.unwrap();
let store = if out_of_core {
index.checkpoint().unwrap();
drop(index);
NowledgeMemEmbeddedStore::new_with_out_of_core_search(
graph,
NowledgeMemOutOfCoreSearchProjection::open(&root).unwrap(),
NowledgeMemRetrievalProjectionAdvisor::default(),
)
} else {
NowledgeMemEmbeddedStore::new(
graph,
Some(NowledgeMemSearchProjection::from_index(index)),
)
};
let handle = NowledgeMemEmbeddedStoreHandle::new(store).unwrap();
let ids = ["source_chunk:large".to_string()];
let error = handle.search_projection_documents(&ids, 1).unwrap_err();
assert!(error.to_string().contains("payload bytes"));
assert!(
matches!(&error, HawDBError::ReadBudgetExceeded(cause)
if cause.resource == ReadBudgetResource::PayloadBytes && cause.limit == 64),
"hydration lost its result budget cause (out_of_core={out_of_core}): {error}"
);
let request_error = handle.search_projection_documents(&ids, 0).unwrap_err();
assert!(matches!(request_error, HawDBError::Execution(_)));
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.admissions, 1);
assert_eq!(snapshot.completions, 1);
drop(handle);
if out_of_core {
std::fs::remove_dir_all(root).unwrap();
}
}
}
#[test]
fn embedded_store_handle_checkpoints_external_projection_delta_before_returning() {
let root = unique_nowledge_mem_test_dir("external_projection_delta_checkpoint");
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let report = handle
.apply_search_projection_delta_and_checkpoint(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "memory-1".to_string(),
title: "Imported memory".to_string(),
body: "Preserved legacy projection payload".to_string(),
embedding: Some(vec![0.25, 0.75]),
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([("space_id".to_string(), "default".to_string())]),
}],
deletes: Vec::new(),
max_operations: Some(1),
source_graph_commit_epoch: Some(7),
})
.unwrap();
assert_eq!(report.upserted_documents, 1);
drop(handle);
let reopened = SearchIndex::open(&root).unwrap();
let freshness = reopened.projection_freshness();
assert_eq!(freshness.document_count, 1);
assert_eq!(freshness.source_graph_commit_epoch, Some(7));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_query_runtime_executes_vector_seed_with_observability() {
let mut index = SearchIndex::in_memory();
for (external_id, embedding) in [("nearest", vec![1.0, 0.0]), ("farther", vec![0.0, 1.0])] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.to_string(),
title: external_id.to_string(),
body: String::new(),
embedding: Some(embedding),
source_id: None,
metadata: BTreeMap::new(),
})
.unwrap();
}
let projection = NowledgeMemSearchProjection::from_index(index);
let graph = NowledgeMemGraph::from_database(
Database::new_with_config(DatabaseConfig {
slow_query_log_threshold_micros: 0,
..DatabaseConfig::default()
}),
NowledgeMemGraphMode::WritableCutover,
);
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let parameters = BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]);
let output = store
.query_with_params_with_report_options(
"CALL vector_search($embedding, topK := 1) RETURN id, score",
¶meters,
NowledgeMemQueryReportOptions {
capture_physical_plan: true,
slow_log_threshold_micros: Some(0),
},
)
.unwrap();
assert_eq!(
output.output.rows[0].get("id"),
Some(&Value::String("memory:nearest".to_string()))
);
assert_eq!(output.report.statement_kind, "vector_search");
assert_eq!(
output.report.physical_operator_counts.get("VectorSeedScan"),
Some(&1)
);
assert_eq!(output.report.plan_cache_lookup.as_deref(), Some("bypass"));
assert_eq!(
output.report.vector_execution_reports[0].scalar_filtered_count,
0
);
assert_eq!(output.report.vector_execution_reports.len(), 1);
assert_eq!(
output.report.vector_execution_reports[0].backend,
hawdb_executor::VectorExecutionBackend::ScalarFlat
);
assert_eq!(
output.report.vector_execution_reports[0].compression_mode,
hawdb_executor::VectorCompressionMode::Disabled
);
assert_eq!(
output.report.vector_execution_reports[0].candidate_source,
hawdb_plan_cypher::VectorCandidateSource::Scalar
);
assert_eq!(
output.report.vector_execution_reports[0].backend_selection_reason,
Some(hawdb_plan_cypher::VectorBackendSelectionReason::CompressionDisabled)
);
assert_eq!(
output.report.vector_execution_reports[0].estimated_raw_vector_bytes,
Some(16)
);
assert_eq!(
output.report.vector_execution_reports[0].filter_selectivity_per_million,
Some(0)
);
assert_eq!(
output.report.vector_execution_reports[0].final_score_source,
hawdb_executor::VectorScoreSource::RawVector
);
assert_eq!(
output.report.vector_execution_reports[0].index_covered_document_count,
Some(2)
);
assert_eq!(
output.report.vector_execution_reports[0].index_candidate_document_count,
Some(2)
);
assert_eq!(
output.report.vector_execution_reports[0].index_coverage_complete,
Some(true)
);
assert!(output.report.vector_execution_reports[0]
.fallback_reason_codes
.is_empty());
assert!(output.report.vector_execution_reports[0].raw_vector_bytes_read > 0);
let explain = store
.query_with_params_with_report(
"EXPLAIN ANALYZE CALL vector_search($embedding, topK := 1) RETURN id, score",
¶meters,
)
.unwrap();
let Value::List(vector_reports) = explain.output.rows[0]
.get("vector_execution_reports")
.expect("explain analyze vector reports")
else {
panic!("expected vector execution report list");
};
assert_eq!(vector_reports.len(), 1);
let Some(Value::String(plan)) = explain.output.rows[0].get("plan") else {
panic!("expected explain plan");
};
assert!(plan.contains("VectorSeedScan embedding=$embedding"));
assert!(plan.contains("priority=128 max_parallelism=16"));
assert!(plan.contains("max_working_memory_bytes=Some(67108864)"));
assert!(plan.contains("Filter->VectorCandidateScan->RawVectorRerank->TopK"));
assert!(!plan.contains("[1"));
let slow_log = store.graph().database().slow_query_log_jsonl().unwrap();
let slow_event: serde_json::Value =
serde_json::from_str(slow_log.lines().next().unwrap()).unwrap();
assert_eq!(slow_event["vector_execution_report_count"], 1);
assert_eq!(
slow_event["vector_execution_reports"][0]["backend"],
"scalar_flat"
);
assert_eq!(
slow_event["vector_execution_reports"][0]["compression_mode"],
"disabled"
);
assert_eq!(
slow_event["vector_execution_reports"][0]["backend_selection_reason"],
"compression_disabled"
);
assert_eq!(
slow_event["vector_execution_reports"][0]["estimated_raw_vector_bytes"],
16
);
assert_eq!(
slow_event["vector_execution_reports"][0]["index_coverage_complete"],
true
);
assert!(slow_event.get("query_text").is_none());
assert!(!slow_log.contains("1.0"));
}
#[test]
fn embedded_query_runtime_feeds_vector_candidates_into_graph_match() {
let mut index = SearchIndex::in_memory();
for (external_id, embedding) in [("nearest", vec![1.0, 0.0]), ("farther", vec![0.8, 0.2])] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.to_string(),
title: external_id.to_string(),
body: String::new(),
embedding: Some(embedding),
source_id: None,
metadata: BTreeMap::from([(
"space_id".to_string(),
if external_id == "nearest" {
"selected"
} else {
"other"
}
.to_string(),
)]),
})
.unwrap();
}
let projection = NowledgeMemSearchProjection::from_index(index);
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'nearest', space_id: 'selected'})")
.unwrap();
graph
.query("CREATE (:Memory {id: 'farther', space_id: 'other'})")
.unwrap();
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let parameters = BTreeMap::from([
(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
),
(
"space_id".to_string(),
Value::String("selected".to_string()),
),
]);
let query = "CALL vector_search($embedding, topK := 2) YIELD id, score \
MATCH (m:Memory) WHERE m.space_id = $space_id \
RETURN m.id AS memory_id, score";
let logical =
crate::planner::plan_with_params(&crate::cypher::parse(query).unwrap(), ¶meters)
.unwrap();
let plan = crate::optimizer::CascadesOptimizer::new(Default::default())
.optimize(&logical)
.explain(0);
let vector_seed_line = plan
.lines()
.find(|line| line.contains("VectorSeedScan"))
.expect("vector seed plan");
assert!(vector_seed_line.contains("metadata_filter_fields=[\"space_id\"]"));
assert!(!vector_seed_line.contains("selected"));
let output = store
.query_with_params_with_report_options(
query,
¶meters,
NowledgeMemQueryReportOptions {
capture_physical_plan: true,
slow_log_threshold_micros: None,
},
)
.unwrap();
assert_eq!(output.output.rows.len(), 1);
assert_eq!(
output.output.rows[0].get("memory_id"),
Some(&Value::String("nearest".to_string()))
);
assert!(matches!(
output.output.rows[0].get("score"),
Some(Value::Float(score)) if *score > 0.0
));
assert!(!output.output.rows[0].contains_key("external_id"));
assert_eq!(output.report.statement_kind, "vector_graph_search");
assert_eq!(output.report.plan_cache_lookup.as_deref(), Some("bypass"));
assert_eq!(
output.report.physical_operator_counts.get("VectorSeedScan"),
Some(&1)
);
assert_eq!(
output
.report
.physical_operator_counts
.get("NodeColumnLookupExec"),
Some(&1)
);
assert!(output.report.physical_plan_captured);
assert_eq!(
output.report.vector_execution_reports[0].scalar_filtered_count,
1
);
}
#[test]
fn embedded_query_runtime_expands_from_vector_seed_candidates() {
let mut index = SearchIndex::in_memory();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "nearest".to_string(),
title: "nearest".to_string(),
body: String::new(),
embedding: Some(vec![1.0, 0.0]),
source_id: None,
metadata: BTreeMap::new(),
})
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(index);
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph.query("CREATE (:Memory {id: 'nearest'})").unwrap();
graph.query("CREATE (:Entity {id: 'entity-rust'})").unwrap();
graph
.query(
"MATCH (m:Memory {id: 'nearest'}), (e:Entity {id: 'entity-rust'}) \
CREATE (m)-[:MENTIONS]->(e)",
)
.unwrap();
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let parameters = BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]);
let output = store
.query_with_params_with_report_options(
"CALL vector_search($embedding, topK := 1) YIELD id, score \
MATCH (m:Memory)-[:MENTIONS]->(e:Entity) \
RETURN m.id AS memory_id, e.id AS entity_id, score",
¶meters,
NowledgeMemQueryReportOptions {
capture_physical_plan: true,
slow_log_threshold_micros: None,
},
)
.unwrap();
assert_eq!(output.output.rows.len(), 1);
assert_eq!(
output.output.rows[0].get("memory_id"),
Some(&Value::String("nearest".to_string()))
);
assert_eq!(
output.output.rows[0].get("entity_id"),
Some(&Value::String("entity-rust".to_string()))
);
assert!(matches!(
output.output.rows[0].get("score"),
Some(Value::Float(score)) if *score > 0.0
));
assert_eq!(
output
.report
.physical_operator_counts
.get("AdjacencyExpandExec"),
Some(&1)
);
assert_eq!(output.report.graph_expansion_reports.len(), 1);
let graph_report = &output.report.graph_expansion_reports[0];
assert_eq!(graph_report.seed_count, 1);
assert_eq!(graph_report.expanded_node_count, 1);
assert_eq!(graph_report.expanded_edge_count, 1);
assert_eq!(graph_report.relation_types, vec!["MENTIONS".to_string()]);
assert_eq!(graph_report.min_hops, 1);
assert_eq!(graph_report.max_hops, 1);
assert_eq!(graph_report.reranked_seed_count, 1);
assert_eq!(graph_report.returned_count, 1);
assert!(graph_report.payload_bytes_used > 0);
assert!(!graph_report.truncated());
let explain = store
.query_with_params_with_report(
"EXPLAIN ANALYZE CALL vector_search($embedding, topK := 1) YIELD id, score \
MATCH (m:Memory)-[:MENTIONS]->(e:Entity) \
RETURN m.id AS memory_id, e.id AS entity_id, score",
¶meters,
)
.unwrap();
let Value::List(graph_reports) = explain.output.rows[0]
.get("graph_expansion_reports")
.expect("explain analyze graph reports")
else {
panic!("expected graph expansion report list");
};
assert_eq!(graph_reports.len(), 1);
}
#[test]
fn embedded_query_runtime_bounds_vector_seed_graph_fanout() {
let mut index = SearchIndex::in_memory();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "fanout-seed".to_string(),
title: "fanout seed".to_string(),
body: String::new(),
embedding: Some(vec![1.0, 0.0]),
source_id: None,
metadata: BTreeMap::new(),
})
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(index);
let mut graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
graph.query("CREATE (:Memory {id: 'fanout-seed'})").unwrap();
for index in 0..65 {
graph
.query(&format!("CREATE (:Entity {{id: 'entity-{index}'}})"))
.unwrap();
graph
.query(&format!(
"MATCH (m:Memory {{id: 'fanout-seed'}}), \
(e:Entity {{id: 'entity-{index}'}}) \
CREATE (m)-[:MENTIONS]->(e)"
))
.unwrap();
}
let mut store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let parameters = BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]);
let error = store
.query_with_params_with_report(
"CALL vector_search($embedding, topK := 1) YIELD id, score \
MATCH (m:Memory)-[:MENTIONS]->(e:Entity) \
RETURN e.id AS entity_id",
¶meters,
)
.unwrap_err();
assert_eq!(
error,
HawDBError::GraphExpansionCandidateLimitExceeded {
requested: 65,
limit: 64
}
);
}
#[test]
fn embedded_query_runtime_rejects_vector_seed_expansion_over_two_hops() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(
graph,
Some(NowledgeMemSearchProjection::from_index(
SearchIndex::in_memory(),
)),
);
let parameters = BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]);
let error = store
.query_with_params_with_report(
"CALL vector_search($embedding, topK := 1) YIELD id, score \
MATCH (m:Memory)-[:MENTIONS*1..3]->(e:Entity) \
RETURN e.id AS entity_id",
¶meters,
)
.unwrap_err();
assert!(error
.to_string()
.contains("vector-seeded graph expansion supports at most 2 hops"));
}
#[test]
fn graph_only_query_runtime_rejects_vector_seed_capability() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::WritableCutover);
let mut store = NowledgeMemEmbeddedStore::new(graph, None);
let parameters = BTreeMap::from([(
"embedding".to_string(),
Value::List(vec![Value::Float(1.0), Value::Float(0.0)]),
)]);
let error = store
.query_with_params_with_report(
"CALL vector_search($embedding, topK := 1) RETURN id, score",
¶meters,
)
.unwrap_err();
assert!(error.to_string().contains("search projection"));
}
#[test]
fn embedded_store_reopens_search_projection_with_descriptor_pruning() {
let root = unique_nowledge_mem_test_dir("search_candidate_library_reopen_pruning");
let graph_path = root.join("graph");
let search_path = root.join("search");
{
let mut db = Database::open(&graph_path).unwrap();
db.query(
"CREATE (:Memory {id: 'active', title: 'Active candidate', space_id: 'default'})",
)
.unwrap();
db.checkpoint().unwrap();
}
{
let mut index = SearchIndex::open(&search_path).unwrap();
for (external_id, lifecycle_state, importance) in [
("deleted", "deleted", "0.95"),
("forgotten", "forgotten", "0.90"),
("active", "active", "0.80"),
] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.to_string(),
title: format!("{external_id} candidate"),
body: "checkpointed candidate read".to_string(),
embedding: None,
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("unit_type".to_string(), "memory".to_string()),
("lifecycle_state".to_string(), lifecycle_state.to_string()),
("importance".to_string(), importance.to_string()),
]),
})
.unwrap();
}
upsert_excluded_search_projection_rows(&mut index, "excluded", 126, None);
index.checkpoint().unwrap();
}
let options = NowledgeMemOpenOptions::with_search_projection(
graph_path,
search_path,
NowledgeMemGraphMode::ShadowReadOnly,
);
let (store, open_report) = NowledgeMemEmbeddedStore::open_with_options(options).unwrap();
let request = NowledgeMemSearchCandidateRequest::text("candidate read", 10)
.with_metadata_filters(BTreeMap::from([
(
"lifecycle_state__not_in".to_string(),
r#"["deleted","forgotten"]"#.to_string(),
),
("importance__gte".to_string(), "0.8".to_string()),
]));
let output = store.search_candidates(&request).unwrap();
assert!(open_report.graph_opened);
assert!(open_report.search_projection_opened);
assert_eq!(output.result.total_hits, 1);
assert_eq!(output.result.hits[0].id, "memory:active");
assert_eq!(output.report.metadata_filter_count, 2);
assert_eq!(output.report.pushed_predicate_count, 2);
assert_eq!(output.report.residual_predicate_count, 0);
assert!(output.report.persisted_segment_descriptor_used);
assert_eq!(output.report.segment_count, 2);
assert_eq!(output.report.pruned_segment_count, 1);
assert_eq!(output.report.scanned_segment_count, 1);
assert_eq!(output.report.physical_range_read_count, 1);
assert!(output.report.physical_bytes_read > 0);
assert_eq!(output.report.filtered_out_count, 128);
assert!(output
.report
.candidate_set
.metadata_predicate_pushdown
.field_summaries
.iter()
.any(|summary| summary.field == "lifecycle_state" && summary.value_summary_used));
assert!(output
.report
.candidate_set
.metadata_predicate_pushdown
.field_summaries
.iter()
.any(|summary| summary.field == "importance" && summary.numeric_range_summary_used));
let direct_evidence = store
.search_candidate_shadow_evidence_json(&request, ["memory:active"])
.unwrap();
assert_eq!(direct_evidence["ready"], true);
assert_eq!(
direct_evidence["filter_pushdown"]["field_capabilities_ready"],
true
);
assert_eq!(
direct_evidence["filter_pushdown"]["missing_numeric_range_fields"],
serde_json::json!([])
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_projection_candidate_api_reports_vector_generation_inputs() {
let root = unique_nowledge_mem_test_dir("search_candidate_api_vector");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
for (external_id, lifecycle_state, embedding) in [
("aaa-deleted", "deleted", vec![1.0, 0.0]),
("aab-forgotten", "forgotten", vec![1.0, 0.0]),
("zza-active", "active", vec![1.0, 0.0]),
("zzb-other", "active", vec![0.6, 0.8]),
] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.to_string(),
title: format!("{external_id} vector candidate"),
body: "vector candidate read".to_string(),
embedding: Some(embedding),
source_id: Some("source-vector".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), lifecycle_state.to_string()),
]),
})
.unwrap();
}
upsert_excluded_search_projection_rows(
&mut index,
"aac-excluded",
126,
Some(vec![1.0, 0.0]),
);
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = NowledgeMemSearchCandidateRequest::vector(vec![1.0, 0.0], 10)
.with_offset(1)
.with_rank_window(Some(2))
.with_metadata_filters(BTreeMap::from([(
"lifecycle_state__not_in".to_string(),
r#"["deleted","forgotten"]"#.to_string(),
)]));
let output = store.search_candidates(&request).unwrap();
assert_eq!(output.result.total_hits, 2);
assert_eq!(output.result.offset, 1);
assert_eq!(output.result.hits.len(), 1);
assert_eq!(output.result.hits[0].id, "memory:zzb-other");
assert_eq!(output.result.hits[0].vector_rank, Some(2));
assert_eq!(output.report.mode, SearchMode::Vector);
assert_eq!(output.report.query_embedding_dimension, Some(2));
assert_eq!(output.report.offset, 1);
assert_eq!(output.report.rank_window, Some(2));
assert_eq!(
output.report.retriever_backends.get("vector"),
Some(&"scalar_vector_scan".to_string())
);
assert_eq!(output.report.retriever_available.get("vector"), Some(&true));
assert_eq!(output.report.retriever_available.get("text"), Some(&false));
assert_eq!(
output.report.retriever_candidate_counts.get("vector"),
Some(&2)
);
assert_eq!(
output
.report
.retriever_candidate_score_sources
.get("vector"),
Some(&"raw_vector".to_string())
);
assert_eq!(
output.report.retriever_final_score_sources.get("vector"),
Some(&"raw_vector".to_string())
);
assert_eq!(output.report.pushed_predicate_count, 1);
assert_eq!(output.report.pruned_segment_count, 1);
assert_eq!(output.report.filtered_out_count, 128);
assert!(output.report.persisted_segment_descriptor_used);
assert_eq!(output.report.json()["query_embedding_dimension"], 2);
assert_eq!(output.report.json()["offset"], 1);
assert_eq!(
output.report.json()["retriever_candidate_counts"]["vector"],
2
);
assert_eq!(
output.report.json()["retriever_final_score_sources"]["vector"],
"raw_vector"
);
assert!(!output.report.json().to_string().contains("[1.0,0.0]"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_shadow_bridge_aggregates_text_and_vector_leg_evidence() {
let root = unique_nowledge_mem_test_dir("search_candidate_bridge_retrievers");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-leg".to_string(),
title: "Retriever leg candidate".to_string(),
body: "retriever leg candidate read".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-leg".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(31),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let text_request = NowledgeMemSearchCandidateRequest::text("retriever leg", 10)
.with_metadata_filters(BTreeMap::from([(
"lifecycle_state__not_in".to_string(),
r#"["deleted","forgotten"]"#.to_string(),
)]));
let vector_request = NowledgeMemSearchCandidateRequest::vector(vec![1.0, 0.0], 10)
.with_metadata_filters(BTreeMap::from([(
"lifecycle_state__not_in".to_string(),
r#"["deleted","forgotten"]"#.to_string(),
)]));
let text_output = projection
.search_candidates_with_report(&text_request)
.unwrap();
let vector_output = projection
.search_candidates_with_report(&vector_request)
.unwrap();
let mut accumulator = NowledgeMemSearchCandidateShadowAccumulator::new();
accumulator.record_search_candidate_output(["memory:mem-leg"], &text_output);
accumulator.record_search_candidate_output(["memory:mem-leg"], &vector_output);
let evidence = accumulator.json();
assert_eq!(evidence["ready"], true);
assert_eq!(evidence["request_count"], 2);
assert_eq!(evidence["text_retriever_ready"], true);
assert_eq!(evidence["vector_retriever_ready"], true);
assert_eq!(evidence["fts_top_k_overlap_ready"], true);
assert_eq!(evidence["vector_top_k_overlap_ready"], true);
assert_eq!(evidence["top_k_overlap_observed"]["fts"], true);
assert_eq!(evidence["top_k_overlap_observed"]["vector"], true);
assert_eq!(
evidence["candidate_readiness"]["source_chunk_identity_ready"],
false
);
assert_eq!(evidence["candidate_readiness"]["fail_soft_observed"], false);
assert_eq!(
evidence["candidate_readiness"]["projection_marker_status_visible"],
true
);
assert_eq!(
evidence["candidate_readiness"]["projection_watermark_ready"],
true
);
assert_eq!(
evidence["candidate_readiness"]["embedding_identity_ready"],
true
);
assert_eq!(evidence["retriever_leg_candidate_counts"]["text"], 1);
assert_eq!(evidence["retriever_leg_candidate_counts"]["vector"], 1);
assert_eq!(
evidence["filter_pushdown"]["field_capabilities_ready"],
true
);
assert_eq!(evidence["blocker_codes"], serde_json::json!([]));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_projection_candidate_api_preserves_source_chunk_identity() {
let root = unique_nowledge_mem_test_dir("search_candidate_api_source_chunk");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "chunk-1".to_string(),
title: "Source chunk candidate".to_string(),
body: "source chunk identity candidate read".to_string(),
embedding: None,
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "memory-1".to_string(),
title: "Memory candidate".to_string(),
body: "source chunk identity candidate read".to_string(),
embedding: None,
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = NowledgeMemSearchCandidateRequest::text("source chunk identity", 10)
.with_metadata_filters(BTreeMap::from([(
"kind__in".to_string(),
r#"["source_chunk"]"#.to_string(),
)]));
let output = store.search_candidates(&request).unwrap();
assert_eq!(output.result.total_hits, 1);
let hit = &output.result.hits[0];
assert_eq!(hit.id, "source_chunk:chunk-1");
assert_eq!(hit.kind.as_deref(), Some("source_chunk"));
assert_eq!(hit.external_id.as_deref(), Some("chunk-1"));
assert_eq!(hit.source_id.as_deref(), Some("source-1"));
assert_eq!(
output.report.returned_kind_counts.get("source_chunk"),
Some(&1)
);
assert_eq!(output.report.returned_missing_external_id_count, 0);
assert_eq!(output.report.returned_missing_source_id_count, 0);
assert_eq!(output.report.metadata_filter_count, 1);
assert_eq!(output.report.pushed_predicate_count, 1);
assert_eq!(
output.report.json()["returned_kind_counts"]["source_chunk"],
1
);
assert!(!output
.report
.json()
.to_string()
.contains("source chunk identity candidate read"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_readiness_reports_lancedb_replacement_ready_shape() {
let root = unique_nowledge_mem_test_dir("search_candidate_readiness_ready");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "chunk-ready".to_string(),
title: "Ready source chunk".to_string(),
body: "ready candidate replacement body".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-ready".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(19),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let handle = NowledgeMemEmbeddedStoreHandle::new(NowledgeMemEmbeddedStore::new(
graph,
Some(projection),
))
.unwrap();
let request = NowledgeMemSearchCandidateRequest::text("ready source chunk", 10)
.with_metadata_filters(BTreeMap::from([(
"kind__in".to_string(),
r#"["source_chunk"]"#.to_string(),
)]));
let options =
NowledgeMemSearchCandidateReadinessOptions::lancedb_replacement_candidate_read()
.with_text_retriever(true)
.with_source_chunk_identity(true)
.with_embedding_identity("bge-m3", 2);
let readiness = handle
.search_candidate_readiness(&request, &options)
.unwrap();
assert!(readiness.ready);
assert!(readiness.present);
assert_eq!(
readiness.protocol,
NOWLEDGE_MEM_SEARCH_CANDIDATE_READINESS_PROTOCOL
);
assert!(readiness.metadata_pushdown_ready);
assert!(readiness.segment_descriptor_ready);
assert!(readiness.text_retriever_ready);
assert!(readiness.source_chunk_identity_ready);
assert!(readiness.projection_marker_status_visible);
assert!(readiness.projection_watermark_ready);
assert!(readiness.embedding_identity_ready);
assert!(readiness.blocker_codes.is_empty());
assert_eq!(
readiness
.candidate_report
.projection_source_graph_commit_epoch,
Some(19)
);
assert_eq!(
readiness
.candidate_report
.projection_embedding_model
.as_deref(),
Some("bge-m3")
);
assert_eq!(
readiness.candidate_report.projection_embedding_dimension,
Some(2)
);
assert_eq!(
readiness
.candidate_report
.returned_kind_counts
.get("source_chunk"),
Some(&1)
);
assert_eq!(readiness.json()["projection_watermark_ready"], true);
assert_eq!(readiness.json()["embedding_identity_ready"], true);
assert!(!readiness
.json()
.to_string()
.contains("ready candidate replacement body"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_readiness_blocks_missing_source_chunk_identity() {
let root = unique_nowledge_mem_test_dir("search_candidate_readiness_blocked");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "memory-only".to_string(),
title: "Memory only candidate".to_string(),
body: "memory only candidate replacement body".to_string(),
embedding: None,
source_id: Some("source-memory".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let request = NowledgeMemSearchCandidateRequest::text("memory only candidate", 10);
let options =
NowledgeMemSearchCandidateReadinessOptions::default().with_source_chunk_identity(true);
let readiness = projection
.search_candidate_readiness(&request, &options)
.unwrap();
assert!(!readiness.ready);
assert_eq!(readiness.candidate_report.returned_hit_count, 1);
assert!(!readiness.source_chunk_identity_ready);
assert!(readiness
.blocker_codes
.iter()
.any(|code| code == "search_candidate_source_chunk_identity_missing"));
assert!(!readiness
.json()
.to_string()
.contains("memory only candidate replacement body"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_readiness_blocks_missing_projection_watermark() {
let root = unique_nowledge_mem_test_dir("search_candidate_readiness_watermark");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "chunk-no-watermark".to_string(),
title: "No watermark source chunk".to_string(),
body: "candidate watermark body".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-no-watermark".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let request = NowledgeMemSearchCandidateRequest::text("no watermark source chunk", 10)
.with_metadata_filters(BTreeMap::from([(
"kind__in".to_string(),
r#"["source_chunk"]"#.to_string(),
)]));
let options =
NowledgeMemSearchCandidateReadinessOptions::lancedb_replacement_candidate_read()
.with_text_retriever(true)
.with_source_chunk_identity(true)
.with_embedding_identity("bge-m3", 2);
let readiness = projection
.search_candidate_readiness(&request, &options)
.unwrap();
assert!(!readiness.ready);
assert!(readiness.text_retriever_ready);
assert!(readiness.source_chunk_identity_ready);
assert!(!readiness.projection_watermark_ready);
assert!(readiness.embedding_identity_ready);
assert!(readiness
.blocker_codes
.iter()
.any(|code| code == "search_candidate_projection_watermark_missing"));
assert!(!readiness
.json()
.to_string()
.contains("candidate watermark body"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_readiness_lancedb_default_requires_embedding_identity() {
let root = unique_nowledge_mem_test_dir("search_candidate_readiness_manifest_required");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "chunk-no-manifest".to_string(),
title: "No manifest source chunk".to_string(),
body: "candidate manifest body".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-no-manifest".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(29),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let request = NowledgeMemSearchCandidateRequest::text("no manifest source chunk", 10)
.with_metadata_filters(BTreeMap::from([(
"kind__in".to_string(),
r#"["source_chunk"]"#.to_string(),
)]));
let options =
NowledgeMemSearchCandidateReadinessOptions::lancedb_replacement_candidate_read()
.with_text_retriever(true)
.with_source_chunk_identity(true);
let readiness = projection
.search_candidate_readiness(&request, &options)
.unwrap();
assert!(!readiness.ready);
assert!(readiness.projection_watermark_ready);
assert!(!readiness.embedding_identity_ready);
assert_eq!(readiness.candidate_report.projection_embedding_model, None);
assert_eq!(
readiness.candidate_report.projection_embedding_dimension,
Some(2)
);
assert!(readiness
.blocker_codes
.iter()
.any(|code| code == "search_candidate_embedding_identity_not_ready"));
assert!(!readiness
.json()
.to_string()
.contains("candidate manifest body"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_candidate_readiness_blocks_embedding_identity_mismatch() {
let root = unique_nowledge_mem_test_dir("search_candidate_readiness_embedding");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::SourceChunk,
external_id: "chunk-embedding".to_string(),
title: "Embedding identity source chunk".to_string(),
body: "candidate embedding body".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-embedding".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
}],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(23),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let request =
NowledgeMemSearchCandidateRequest::text("embedding identity source chunk", 10)
.with_metadata_filters(BTreeMap::from([(
"kind__in".to_string(),
r#"["source_chunk"]"#.to_string(),
)]));
let options =
NowledgeMemSearchCandidateReadinessOptions::lancedb_replacement_candidate_read()
.with_text_retriever(true)
.with_source_chunk_identity(true)
.with_embedding_identity("bge-m3", 3);
let readiness = projection
.search_candidate_readiness(&request, &options)
.unwrap();
assert!(!readiness.ready);
assert!(readiness.projection_watermark_ready);
assert!(!readiness.embedding_identity_ready);
assert_eq!(
readiness.candidate_report.projection_embedding_dimension,
Some(2)
);
assert!(readiness
.blocker_codes
.iter()
.any(|code| code == "search_candidate_embedding_identity_not_ready"));
assert!(!readiness
.json()
.to_string()
.contains("candidate embedding body"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_projection_candidate_api_reports_fail_soft_vector_fallback() {
let root = unique_nowledge_mem_test_dir("search_candidate_api_fail_soft");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-fallback".to_string(),
title: "Fallback candidate".to_string(),
body: "hybrid fallback candidate read".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-fallback".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request =
NowledgeMemSearchCandidateRequest::hybrid("hybrid fallback", vec![1.0, 0.0, 0.0], 10);
let output = store.search_candidates(&request).unwrap();
assert_eq!(output.result.total_hits, 1);
assert_eq!(output.result.hits[0].id, "memory:mem-fallback");
assert_eq!(output.report.mode, SearchMode::Hybrid);
assert_eq!(output.report.query_embedding_dimension, Some(3));
assert_eq!(
output.report.retriever_available.get("vector"),
Some(&false)
);
assert_eq!(output.report.retriever_available.get("text"), Some(&true));
assert_eq!(
output.report.retriever_candidate_counts.get("text"),
Some(&1)
);
assert!(output
.report
.fallback_reason_codes
.iter()
.any(|code| code == "vector_dimension_mismatch"));
assert!(output.report.empty_reason_codes.is_empty());
assert_eq!(
output.report.json()["fallback_reason_codes"],
serde_json::json!(["vector_dimension_mismatch"])
);
assert!(!output
.report
.json()
.to_string()
.contains("hybrid fallback candidate read"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn search_projection_candidate_api_gates_compressed_vector_preference() {
let mut index = SearchIndex::in_memory();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 2,
})
.unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-advisor".to_string(),
title: "Advisor gated candidate".to_string(),
body: "advisor gated compressed candidate".to_string(),
embedding: Some(vec![1.0, 0.0]),
source_id: Some("source-advisor".to_string()),
metadata: BTreeMap::new(),
})
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(index);
let request = NowledgeMemSearchCandidateRequest::vector(vec![1.0, 0.0], 10)
.with_compressed_vector_search_mode(CompressedVectorSearchMode::Preferred);
let output = projection.search_candidates_with_report(&request).unwrap();
assert_eq!(
output.report.compressed_vector_search_mode,
CompressedVectorSearchMode::Disabled
);
assert_eq!(
output.report.requested_compressed_vector_search_mode,
CompressedVectorSearchMode::Preferred
);
assert_eq!(
output
.report
.retriever_backend_selection_reasons
.get("vector")
.map(String::as_str),
Some("compression_disabled")
);
assert_eq!(
output.report.retriever_backends.get("vector"),
Some(&"scalar_vector_scan".to_string())
);
assert_eq!(
output.report.retrieval_projection_advisor_blocker_codes,
vec![
"retrieval_projection_recall_evidence_missing".to_string(),
"retrieval_projection_parity_evidence_missing".to_string(),
"retrieval_projection_segment_not_advised".to_string()
]
);
assert_eq!(
output.report.json()["retrieval_projection_advisor"]["ready"],
false
);
assert!(!output
.report
.json()
.to_string()
.contains("advisor gated compressed candidate"));
}
#[test]
fn search_projection_candidate_api_reports_repair_markers_without_hits() {
let root = unique_nowledge_mem_test_dir("search_candidate_api_repair_markers");
{
let mut index = SearchIndex::open(&root).unwrap();
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-marker".to_string(),
title: "Marker candidate".to_string(),
body: "repair marker candidate read".to_string(),
embedding: None,
source_id: Some("source-marker".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
]),
})
.unwrap();
index.mark_full_reindex_needed("stale projection").unwrap();
index
.mark_metadata_repair_needed("missing metadata")
.unwrap();
index.checkpoint().unwrap();
}
let projection = NowledgeMemSearchProjection::open(&root).unwrap();
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let request = NowledgeMemSearchCandidateRequest::text("not present", 10);
let output = store.search_candidates(&request).unwrap();
assert_eq!(output.result.total_hits, 0);
assert!(output.report.projection_full_reindex_needed);
assert!(output.report.projection_metadata_repair_needed);
assert!(output
.report
.empty_reason_codes
.iter()
.any(|code| code == "retriever_no_hits"));
assert_eq!(output.report.json()["projection_full_reindex_needed"], true);
assert_eq!(
output.report.json()["projection_metadata_repair_needed"],
true
);
assert!(!output
.report
.json()
.to_string()
.contains("stale projection"));
assert!(!output
.report
.json()
.to_string()
.contains("missing metadata"));
assert!(!output
.report
.json()
.to_string()
.contains("repair marker candidate read"));
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_open_options_can_prefer_compressed_vector_search() {
let root = unique_nowledge_mem_test_dir("compressed_vector_open_options");
let graph_path = root.join("graph");
let search_path = root.join("search");
{
let mut db = Database::open(&graph_path).unwrap();
db.query(
"CREATE (:Memory {id: 'mem-vector', title: 'Vector facade', content: 'Compressed vector retrieval'})",
)
.unwrap();
db.checkpoint().unwrap();
}
{
let mut index = SearchIndex::open(&search_path).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 8,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: vec![
SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-vector".to_string(),
title: "Vector facade".to_string(),
body: "Compressed vector retrieval".to_string(),
embedding: Some(vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
source_id: None,
metadata: BTreeMap::new(),
},
SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "mem-vector-neighbor".to_string(),
title: "Vector neighbor".to_string(),
body: "Recall validation neighbor".to_string(),
embedding: Some(vec![0.9, 0.1, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
source_id: None,
metadata: BTreeMap::new(),
},
],
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(1),
})
.unwrap();
index.checkpoint().unwrap();
}
let recall_report = SearchIndex::open(&search_path)
.unwrap()
.validate_sampled_vector_recall(VectorRecallValidationOptions {
max_samples: 2,
top_k: 1,
candidate_limit: 1,
minimum_recall_per_million: 1_000_000,
metadata_filters: BTreeMap::new(),
});
assert!(recall_report.ready, "{:?}", recall_report.blocker_codes);
let options = NowledgeMemOpenOptions::with_search_projection(
graph_path,
search_path,
NowledgeMemGraphMode::ShadowReadOnly,
)
.with_compressed_vector_search_mode(CompressedVectorSearchMode::Preferred)
.with_adaptive_vector_backend_policy(AdaptiveVectorBackendPolicy {
flat_scan_max_documents: 0,
high_filter_selectivity_per_million: u32::MAX,
flat_scan_memory_budget_bytes: 0,
})
.with_retrieval_projection_advisor(
NowledgeMemRetrievalProjectionAdvisor::cold_local_with_recall_parity(&recall_report),
);
let (store, report) = NowledgeMemEmbeddedStore::open_with_options(options).unwrap();
let retrieval = store
.retrieve_knowledge_with_report(&KnowledgeRetrievalRequest {
query_text: String::new(),
query_embedding: Some(vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
mode: SearchMode::Vector,
limit: 10,
offset: 0,
rank_window: None,
search_fusion_weights: SearchFusionWeights::default(),
metadata_filters: BTreeMap::new(),
candidate_limit: None,
candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
graph_seed_limit: 0,
graph_context_limit: 0,
graph_context_max_hops: 0,
})
.unwrap();
let output = retrieval.output;
assert_eq!(
report.compressed_vector_search_mode,
CompressedVectorSearchMode::Preferred
);
assert_eq!(
report.requested_compressed_vector_search_mode,
CompressedVectorSearchMode::Preferred
);
assert!(report.retrieval_projection_advisor.ready());
assert_eq!(output.search.hits[0].id, "memory:mem-vector");
assert_eq!(
output.search.retrievers[0].backend,
"hawdb_rabitq_candidate_projection"
);
assert_eq!(
retrieval.report.compressed_vector_search_mode,
CompressedVectorSearchMode::Preferred
);
assert_eq!(
retrieval.report.vector_backend,
Some("hawdb_rabitq_candidate_projection".to_string())
);
assert_eq!(
retrieval.report.json()["vector_backend"],
"hawdb_rabitq_candidate_projection"
);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn embedded_store_retrieval_requires_search_projection() {
let graph =
NowledgeMemGraph::from_database(Database::new(), NowledgeMemGraphMode::ShadowReadOnly);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let error = store
.retrieve_knowledge(&KnowledgeRetrievalRequest {
query_text: "missing projection".to_string(),
query_embedding: None,
mode: SearchMode::Text,
limit: 10,
offset: 0,
rank_window: None,
search_fusion_weights: SearchFusionWeights::default(),
metadata_filters: BTreeMap::new(),
candidate_limit: None,
candidate_scoring: KnowledgeCandidateScoringPolicy::Max,
graph_seed_limit: 4,
graph_context_limit: 4,
graph_context_max_hops: 1,
})
.unwrap_err();
assert_eq!(
error.to_string(),
"storage error: nowledge mem search projection is not configured"
);
}
#[test]
fn embedded_store_reports_background_maintenance_summary() {
let db = Database::new();
let mut graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
graph
.query("CREATE (:Memory {id: 'mem-maintenance', title: 'Maintenance summary'})")
.unwrap();
let projection = NowledgeMemSearchProjection::from_index(SearchIndex::in_memory());
let store = NowledgeMemEmbeddedStore::new(graph, Some(projection));
let summary = store
.background_maintenance_summary(
&LocalQosPolicy::default(),
&LocalQosState::default(),
BackgroundMaintenanceOptions {
include_schema_maintenance: false,
include_property_index_projection: false,
include_search_projection_rebuild: false,
include_search_projection_metadata_repair: false,
include_hawdb_lightning_bootstrap_export: false,
include_external_content_artifact_jobs: false,
..BackgroundMaintenanceOptions::default()
},
)
.unwrap();
assert_eq!(summary.total_candidates, 1);
assert_eq!(summary.admitted_count, 1);
assert_eq!(
summary.top_admitted_kind,
Some(BackgroundMaintenanceKind::SearchProjectionGraphDelta)
);
assert_eq!(summary.executable_search_projection_graph_delta_count, 1);
assert_eq!(summary.admitted_search_projection_graph_delta_count, 1);
let item = &summary.ranked[0];
assert_eq!(
summary.max_search_projection_graph_delta_complete_through_graph_commit_epoch,
item.search_projection_graph_delta_complete_through_graph_commit_epoch
);
assert!(summary
.max_search_projection_graph_delta_complete_through_graph_commit_epoch
.is_some());
assert_eq!(item.name, "search_projection_graph_delta");
assert_eq!(item.admission_name, "admit");
assert_eq!(
item.search_projection_graph_delta_upsert_node_count,
Some(1)
);
assert_eq!(
item.search_projection_graph_delta_delete_document_count,
Some(0)
);
let report = store
.background_maintenance_report(
&LocalQosPolicy::default(),
&LocalQosState::default(),
BackgroundMaintenanceOptions {
include_schema_maintenance: false,
include_property_index_projection: false,
include_search_projection_rebuild: false,
include_search_projection_metadata_repair: false,
include_hawdb_lightning_bootstrap_export: false,
include_external_content_artifact_jobs: false,
..BackgroundMaintenanceOptions::default()
},
)
.unwrap();
let json = report.json();
assert_eq!(report.protocol, "hawdb-background-maintenance-report");
assert!(report.present);
assert!(report.ready);
assert_eq!(report.total_candidates, 1);
assert_eq!(report.ranked_count, 1);
assert_eq!(report.foreground_ranked_count, 0);
assert_eq!(report.unknown_admission_count, 0);
assert_eq!(report.executable_search_projection_graph_delta_count, 1);
assert_eq!(report.admitted_search_projection_graph_delta_count, 1);
assert_eq!(report.slow_query_ready, Some(true));
assert_eq!(report.slow_query_record_count, Some(0));
assert_eq!(report.slow_query_capacity, Some(256));
assert_eq!(report.slow_query_redaction_ready, Some(true));
assert_eq!(report.memory_pressure_ready, Some(true));
assert!(report.memory_budget_bytes.is_some());
assert!(report.estimated_memory_bytes.is_some());
assert!(report.blocker_codes.is_empty());
assert_eq!(json["protocol"], "hawdb-background-maintenance-report");
assert_eq!(json["memory_pressure"]["ready"], true);
assert!(json["memory_pressure"]["budget_bytes"].as_u64().is_some());
assert!(json["memory_pressure"]["estimated_bytes"]
.as_u64()
.is_some());
assert_eq!(json["slow_query"]["ready"], true);
assert_eq!(json["slow_query"]["record_count"], 0);
assert_eq!(json["slow_query"]["capacity"], 256);
assert_eq!(json["ranked"].as_array().unwrap().len(), 1);
}
#[test]
fn embedded_store_background_maintenance_report_fails_closed_without_work() {
let db = Database::new();
let graph = NowledgeMemGraph::from_database(db, NowledgeMemGraphMode::WritableCutover);
let store = NowledgeMemEmbeddedStore::new(graph, None);
let report = store
.background_maintenance_report(
&LocalQosPolicy::default(),
&LocalQosState::default(),
BackgroundMaintenanceOptions {
include_schema_maintenance: false,
include_property_index_projection: false,
include_search_projection_graph_delta_freshness: false,
include_search_projection_rebuild: false,
include_search_projection_metadata_repair: false,
include_hawdb_lightning_bootstrap_export: false,
include_external_content_artifact_jobs: false,
..BackgroundMaintenanceOptions::default()
},
)
.unwrap();
assert!(!report.ready);
assert_eq!(report.total_candidates, 0);
assert_eq!(report.ranked_count, 0);
assert_eq!(
report.blocker_codes,
vec!["no_candidates".to_string(), "no_ranked_work".to_string()]
);
}
fn nowledge_projection_evidence_rows() -> Vec<SearchProjectionRow> {
let mut rows = vec![
nowledge_projection_evidence_row(SearchProjectionKind::Memory, "mem_1", true),
nowledge_projection_evidence_row(SearchProjectionKind::Message, "msg_1", false),
nowledge_projection_evidence_row(SearchProjectionKind::Community, "community_1", true),
nowledge_projection_evidence_row(SearchProjectionKind::Entity, "entity_1", true),
nowledge_projection_evidence_row(SearchProjectionKind::Source, "source_1", true),
nowledge_projection_evidence_row(SearchProjectionKind::SourceChunk, "chunk_1", true),
];
rows.extend((0..124).map(|ordinal| {
nowledge_projection_evidence_row(
SearchProjectionKind::Memory,
&format!("mem_filler_{ordinal:03}"),
true,
)
}));
rows
}
fn upsert_excluded_search_projection_rows(
index: &mut SearchIndex,
external_id_prefix: &str,
count: usize,
embedding: Option<Vec<f32>>,
) {
for ordinal in 0..count {
let external_id = format!("{external_id_prefix}-{ordinal:03}");
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: external_id.clone(),
title: format!("{external_id} candidate"),
body: "metadata filtered candidate read".to_string(),
embedding: embedding.clone(),
source_id: Some("source-1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("unit_type".to_string(), "memory".to_string()),
("lifecycle_state".to_string(), "deleted".to_string()),
("importance".to_string(), "0.95".to_string()),
]),
})
.unwrap();
}
}
fn incremental_maintenance_fixture(
name: &str,
maintenance: super::NowledgeMemIncrementalSearchMaintenanceOptions,
) -> (
std::path::PathBuf,
NowledgeMemEmbeddedStoreHandle,
super::NowledgeMemOpenOptions,
) {
let root = unique_nowledge_mem_test_dir(name);
let search_path = root.join("search");
let mut index = SearchIndex::open(&search_path).unwrap();
for id in ["a", "z"] {
index
.upsert_projection_row(SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: id.into(),
title: id.into(),
body: format!("bootstrap {id}"),
embedding: None,
source_id: None,
metadata: BTreeMap::new(),
})
.unwrap();
}
index.checkpoint().unwrap();
drop(index);
let options = super::NowledgeMemOpenOptions::with_search_projection(
root.join("graph"),
search_path,
NowledgeMemGraphMode::WritableCutover,
)
.with_incremental_search_maintenance(maintenance);
let (handle, report) =
NowledgeMemEmbeddedStoreHandle::open_with_options(options.clone()).unwrap();
assert_eq!(
report.search_projection_role,
Some(super::NowledgeMemSearchProjectionRole::IncrementalMaintenance)
);
assert!(!report.search_production_qualification_bound);
(root, handle, options)
}
#[test]
fn incremental_maintenance_preserves_bootstrap_artifacts_and_reopens_deltas() {
let (root, handle, options) =
incremental_maintenance_fixture("incremental_maintenance_reopen", Default::default());
let search_path = root.join("search");
let pinned = crate::SearchOutOfCoreReader::open(&search_path).unwrap();
let old_generation = pinned.generation();
handle
.query_with_report("CREATE (:Memory {id: 'm', title: 'Inserted'})")
.unwrap();
let catch_up = handle
.catch_up_search_projection_with_batch_hydrator(4, 4, 1, |_, _| {
Ok(SearchProjectionRelationalDelta::default())
})
.unwrap();
assert!(catch_up.complete);
assert_eq!(catch_up.end_applied_epoch, catch_up.end_durable_epoch);
let update = handle
.apply_search_projection_delta_and_checkpoint(SearchProjectionDelta {
upserts: vec![SearchProjectionRow {
kind: SearchProjectionKind::Memory,
external_id: "m".into(),
title: "Inserted".into(),
body: "durable external embedding".into(),
embedding: Some(vec![0.25, 0.75]),
source_id: None,
metadata: BTreeMap::new(),
}],
max_operations: Some(1),
..Default::default()
})
.unwrap();
assert_eq!(update.action, "incremental_mutation_publish");
let resident_error = SearchIndex::open(&search_path).unwrap_err();
assert!(resident_error
.to_string()
.contains("resident search snapshot"));
assert_eq!(pinned.generation(), old_generation);
assert_eq!(pinned.document_count(), 2);
assert!(pinned.hydrate_documents(&["memory:m".into()]).is_err());
let snapshot = handle.runtime_governor_snapshot().unwrap();
assert_eq!(snapshot.admissions, snapshot.completions);
drop(handle);
let (reopened, _) = NowledgeMemEmbeddedStoreHandle::open_with_options(options).unwrap();
let rows = reopened
.search_projection_documents(&["memory:m".into()], 1)
.unwrap();
assert_eq!(rows[0].content, "durable external embedding");
assert_eq!(rows[0].embedding, Some(vec![0.25, 0.75]));
assert_eq!(
reopened
.runtime_status()
.unwrap()
.projection_freshness
.unwrap()
.durable_source_graph_commit_epoch,
catch_up.end_durable_epoch
);
drop(reopened);
drop(pinned);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
#[cfg(feature = "background-maintenance")]
fn incremental_maintenance_compaction_honors_qos_and_cancellation_before_retry() {
use crate::search::SearchOutOfCoreSegmentCompactionStopReason;
let (root, handle, options) = incremental_maintenance_fixture(
"incremental_maintenance_compaction",
Default::default(),
);
let search_path = root.join("search");
handle
.query_with_report("CREATE (:Memory {id: 'm', title: 'Inserted'})")
.unwrap();
let catch_up = handle
.catch_up_search_projection_with_batch_hydrator(4, 4, 1, |_, _| {
Ok(SearchProjectionRelationalDelta::default())
})
.unwrap();
let pinned = crate::SearchOutOfCoreReader::open(&search_path).unwrap();
let manifest = search_path.join("search_projection.out_of_core.manifest.hawdb");
let before = std::fs::read(&manifest).unwrap();
let deferred = handle
.compact_search_projection_segments_with_context(
Default::default(),
hawdb_qos::BackgroundWorkHint {
tenant_budget_remaining_operations: Some(1),
..Default::default()
},
Default::default(),
)
.unwrap();
assert!(matches!(
deferred.stop_reason(),
SearchOutOfCoreSegmentCompactionStopReason::Deferred(_)
));
assert_eq!(std::fs::read(&manifest).unwrap(), before);
let cancellation = crate::RuntimeCancellationToken::new();
cancellation.cancel();
let error = handle
.compact_search_projection_segments_with_context(
Default::default(),
Default::default(),
crate::RuntimeTaskContext::without_deadline(cancellation),
)
.unwrap_err();
assert!(error.to_string().contains("cancel"), "{error}");
assert_eq!(std::fs::read(&manifest).unwrap(), before);
let completed = handle
.compact_search_projection_segments_with_context(
Default::default(),
Default::default(),
Default::default(),
)
.unwrap();
assert_eq!(
completed.stop_reason(),
SearchOutOfCoreSegmentCompactionStopReason::Completed
);
assert_eq!(completed.compaction().unwrap().source_segment_count(), 2);
let documents = handle
.search_projection_documents(
&["memory:a".into(), "memory:m".into(), "memory:z".into()],
3,
)
.unwrap();
assert_eq!(
pinned
.hydrate_documents(&["memory:m".into()])
.unwrap()
.documents[0]
.title,
"Inserted"
);
assert_eq!(pinned.document_count(), 3);
assert_eq!(
handle
.runtime_status()
.unwrap()
.projection_freshness
.unwrap()
.durable_source_graph_commit_epoch,
catch_up.end_durable_epoch
);
let governor = handle.runtime_governor_snapshot().unwrap();
assert_eq!(governor.admissions, governor.completions);
drop(handle);
let (reopened, _) = NowledgeMemEmbeddedStoreHandle::open_with_options(options).unwrap();
assert_eq!(
reopened
.search_projection_documents(
&["memory:a".into(), "memory:m".into(), "memory:z".into()],
3
)
.unwrap(),
documents
);
drop(reopened);
drop(pinned);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn incremental_maintenance_budget_failure_does_not_advance_the_watermark() {
let maintenance = super::NowledgeMemIncrementalSearchMaintenanceOptions {
working_memory_bytes: std::num::NonZeroU64::new(1).unwrap(),
..Default::default()
};
let (root, handle, options) =
incremental_maintenance_fixture("incremental_maintenance_budget", maintenance);
let manifest = root.join("search/search_projection.out_of_core.manifest.hawdb");
let before = std::fs::read(&manifest).unwrap();
handle
.query_with_report("CREATE (:Memory {id: 'm', title: 'Rejected'})")
.unwrap();
let error = handle
.catch_up_search_projection_with_batch_hydrator(4, 4, 1, |_, _| {
Ok(SearchProjectionRelationalDelta::default())
})
.unwrap_err();
assert!(error.to_string().contains("memory"), "{error}");
assert_eq!(std::fs::read(&manifest).unwrap(), before);
let freshness = handle
.runtime_status()
.unwrap()
.projection_freshness
.unwrap();
assert_eq!(freshness.source_graph_commit_epoch, None);
assert_eq!(freshness.durable_source_graph_commit_epoch, None);
drop(handle);
let (reopened, _) = NowledgeMemEmbeddedStoreHandle::open_with_options(options).unwrap();
assert!(reopened
.search_projection_documents(&["memory:m".into()], 1)
.is_err());
drop(reopened);
std::fs::remove_dir_all(root).unwrap();
}
#[test]
fn incremental_maintenance_rejects_missing_bootstrap_without_recreating_it() {
let root = unique_nowledge_mem_test_dir("incremental_missing_bootstrap");
let options = super::NowledgeMemOpenOptions::with_search_projection(
root.join("graph"),
root.join("search"),
NowledgeMemGraphMode::WritableCutover,
)
.with_incremental_search_maintenance(Default::default());
assert!(NowledgeMemEmbeddedStoreHandle::open_with_options(options).is_err());
assert!(!root
.join("search/search_projection.out_of_core.manifest.hawdb")
.exists());
std::fs::remove_dir_all(root).unwrap();
}
fn persisted_nowledge_projection_evidence_index(name: &str) -> SearchIndex {
let path = unique_nowledge_mem_test_dir(name);
let mut index = SearchIndex::open(&path).unwrap();
index
.apply_embedding_manifest(SearchEmbeddingManifest {
model: "bge-m3".to_string(),
version: None,
dimension: 8,
})
.unwrap();
index
.apply_projection_delta(SearchProjectionDelta {
upserts: nowledge_projection_evidence_rows(),
deletes: Vec::new(),
max_operations: None,
source_graph_commit_epoch: Some(17),
})
.unwrap();
index.checkpoint().unwrap();
SearchIndex::open(path).unwrap()
}
fn nowledge_projection_evidence_row(
kind: SearchProjectionKind,
external_id: &str,
include_embedding: bool,
) -> SearchProjectionRow {
SearchProjectionRow {
kind,
external_id: external_id.to_string(),
title: format!("{external_id} title"),
body: format!("{external_id} body"),
embedding: include_embedding.then_some(vec![1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]),
source_id: Some("source_1".to_string()),
metadata: BTreeMap::from([
("space_id".to_string(), "default".to_string()),
("unit_type".to_string(), "fact".to_string()),
("lifecycle_state".to_string(), "active".to_string()),
("importance".to_string(), "0.8".to_string()),
("confidence".to_string(), "0.9".to_string()),
("created_at".to_string(), "11".to_string()),
("updated_at".to_string(), "12".to_string()),
("event_start".to_string(), "10".to_string()),
("event_end".to_string(), "20".to_string()),
("is_latest".to_string(), "true".to_string()),
]),
}
}
fn full_bounded_read_routes() -> Vec<String> {
REQUIRED_NOWLEDGE_MEM_BOUNDED_READ_ROUTES
.iter()
.map(|route| (*route).to_string())
.collect()
}
fn ready_route_readiness_summary() -> NowledgeMemRouteReadinessSummary {
NowledgeMemRouteReadinessSummary {
route_primary_ready: true,
primary_ready_routes: full_bounded_read_routes(),
route_query_plan_evidence_ready: true,
route_query_profile_evidence_ready: true,
route_query_api_behavior_evidence_ready: true,
relationship_property_pruning_required_count: 0,
relationship_property_pruning_report_count: 0,
route_relationship_property_pruning_evidence_ready: true,
}
}
fn ready_search_route_ownership() -> NowledgeMemSearchRouteOwnershipReadinessReport {
nowledge_mem_search_route_ownership_readiness(
&nowledge_mem_search_route_ownership_all_hawdb(),
NowledgeMemSearchRouteOwnershipPolicy::production_cutover(),
)
}
fn ready_active_search_route_ownership() -> NowledgeMemActiveSearchRouteOwnershipReadinessReport
{
nowledge_mem_active_search_route_ownership_readiness(
&nowledge_mem_active_search_route_ownership_all_hawdb(),
NowledgeMemSearchRouteOwnershipPolicy::production_cutover(),
)
}
fn ready_active_search_route_readiness() -> NowledgeMemActiveSearchRouteReadinessReport {
nowledge_mem_active_search_route_readiness(
&nowledge_mem_active_search_route_read_evidence_all_hawdb_ready(),
NowledgeMemSearchRouteOwnershipPolicy::production_cutover(),
)
}
fn ready_query_family_replacement() -> serde_json::Value {
serde_json::json!(REQUIRED_NOWLEDGE_REPLACEMENT_QUERY_FAMILIES
.iter()
.map(|family| serde_json::json!({
"query_family": family,
"required_checks": 1,
"covered_checks": 1,
"shadow_matched_checks": 1,
"replacement_readiness_per_million": 1_000_000,
}))
.collect::<Vec<_>>())
}
fn readiness_dashboard_area<'a>(
dashboard: &'a NowledgeMemReadinessDashboard,
name: &str,
) -> &'a NowledgeMemReadinessAreaSummary {
dashboard
.areas
.iter()
.find(|area| area.name == name)
.expect("readiness dashboard area")
}
fn unique_nowledge_mem_test_dir(name: &str) -> std::path::PathBuf {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
std::env::temp_dir().join(format!(
"hawdb_nowledge_mem_{name}_{}_{}",
std::process::id(),
nanos
))
}
}