pub(super) const UNBOUND_MEMORY_PREDICATE: &str =
"NOT EXISTS (SELECT 1 FROM memory_entities me WHERE me.memory_id = m.id)";
pub(super) const NULL_DESCRIPTION_PREDICATE: &str = "(description IS NULL OR description = '')";
pub(super) const LOW_QUALITY_DESCRIPTION_PREDICATE: &str = "(
description LIKE '%is a software component%'
OR description LIKE '%is a software system%'
OR description LIKE '%is a software application%'
OR description LIKE '%in the software project%'
OR description LIKE '%in the context of software%'
OR description LIKE '%of the software system%'
OR description LIKE '%in the software system%'
OR description LIKE '%software/system design%'
OR description LIKE '%is a configuration file%'
OR description LIKE '%a configuration file used%'
OR description LIKE '%generic configuration file%'
OR description LIKE '%configuration file in the software%'
OR description LIKE '%enhances chatbot%'
OR description LIKE '%chatbot response%'
OR description LIKE '%European digital identity%'
OR description LIKE '%no additional information%'
OR description LIKE '%no further information%'
OR description LIKE '%details are not specified%'
OR description LIKE '%no specific details%'
OR description LIKE '%details are unknown%'
OR description LIKE '%not enough information%'
OR description LIKE '%insufficient information%'
OR description LIKE '%no information is available%'
OR description LIKE '%whose details are%'
OR description LIKE '%fictional%'
OR description LIKE '%may refer to%'
OR description LIKE '%is a placeholder%'
OR description LIKE '%hypothetical%'
)";
const LOW_QUALITY_MARKERS: &[&str] = &[
"is a software component",
"is a software system",
"is a software application",
"in the software project",
"in the context of software",
"of the software system",
"in the software system",
"software/system design",
"is a configuration file",
"a configuration file used",
"generic configuration file",
"configuration file in the software",
"enhances chatbot",
"chatbot response",
"european digital identity",
"no additional information",
"no further information",
"details are not specified",
"no specific details",
"details are unknown",
"not enough information",
"insufficient information",
"no information is available",
"whose details are",
"fictional",
"may refer to",
"is a placeholder",
"hypothetical",
];
pub(crate) fn is_low_quality_description(desc: &str) -> bool {
let d = desc.trim();
if d.is_empty() {
return true;
}
let lower = d.to_ascii_lowercase();
LOW_QUALITY_MARKERS.iter().any(|m| lower.contains(m))
}
pub(super) const SHORT_BODY_PREDICATE: &str = "LENGTH(COALESCE(m.body,'')) < ?2";
pub(super) fn generic_description_predicate() -> String {
let max_chars = crate::constants::ENRICH_GENERIC_DESCRIPTION_MAX_CHARS;
format!(
"(description LIKE '%ingested%' \
OR description LIKE '%imported%' OR description LIKE '%added%' \
OR length(description) < {max_chars})"
)
}
pub(super) fn entity_description_scan_predicate(force_redescribe: bool, named: bool) -> String {
if force_redescribe && named {
"1=1".to_string()
} else if force_redescribe {
format!("({NULL_DESCRIPTION_PREDICATE} OR {LOW_QUALITY_DESCRIPTION_PREDICATE})")
} else {
NULL_DESCRIPTION_PREDICATE.to_string()
}
}
pub(super) fn high_weight_predicate() -> String {
format!(
"r.weight >= {}",
crate::constants::ENRICH_HIGH_WEIGHT_THRESHOLD
)
}
pub(super) fn generic_relation_predicate() -> String {
format!("r.relation = '{}'", crate::parsers::GENERIC_RELATION)
}
pub(super) fn reembed_memory_predicate(dim: usize) -> String {
let bytes = dim * 4;
format!(
"NOT EXISTS (SELECT 1 FROM memory_embeddings me WHERE me.memory_id = m.id \
AND LENGTH(me.embedding) = {bytes})"
)
}
pub(super) fn reembed_entity_predicate(dim: usize) -> String {
let bytes = dim * 4;
format!(
"NOT EXISTS (SELECT 1 FROM entity_embeddings ev WHERE ev.entity_id = e.id \
AND LENGTH(ev.embedding) = {bytes})"
)
}
pub(super) fn reembed_chunk_predicate(dim: usize) -> String {
let bytes = dim * 4;
format!(
"NOT EXISTS (SELECT 1 FROM chunk_embeddings ce WHERE ce.chunk_id = c.id \
AND LENGTH(ce.embedding) = {bytes})"
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn boilerplate_software_component_is_low_quality() {
assert!(is_low_quality_description(
"Foo is a software component in the software project"
));
}
#[test]
fn legitimate_architecture_decision_is_not_low_quality() {
assert!(!is_low_quality_description(
"software architecture decision for auth"
));
}
#[test]
fn software_development_process_is_not_low_quality() {
assert!(!is_low_quality_description(
"Documents the software development process for the billing team"
));
}
#[test]
fn empty_description_is_low_quality() {
assert!(is_low_quality_description(""));
assert!(is_low_quality_description(" "));
}
#[test]
fn chatbot_boilerplate_is_low_quality() {
assert!(is_low_quality_description(
"Module that enhances chatbot response quality"
));
}
#[test]
fn configuration_file_boilerplate_is_low_quality() {
assert!(is_low_quality_description(
"Foo is a configuration file used by the build system"
));
assert!(is_low_quality_description(
"Bar is a configuration file in the software project"
));
assert!(is_low_quality_description(
"A generic configuration file placeholder"
));
}
#[test]
fn legitimate_configuration_file_prose_is_not_low_quality() {
assert!(!is_low_quality_description(
"clippy-toml is a Rust lint configuration file that shapes code quality standards and design consistency"
));
assert!(!is_low_quality_description(
"TOML configuration file for Clippy lints in this workspace"
));
}
#[test]
fn high_weight_predicate_uses_constant_threshold() {
let sql = high_weight_predicate();
assert!(
sql.contains(&format!(
"r.weight >= {}",
crate::constants::ENRICH_HIGH_WEIGHT_THRESHOLD
)),
"predicate must be built from the constant: {sql}"
);
}
#[test]
fn generic_description_predicate_uses_constant_cutoff() {
let sql = generic_description_predicate();
assert!(sql.contains(&format!(
"length(description) < {}",
crate::constants::ENRICH_GENERIC_DESCRIPTION_MAX_CHARS
)));
}
#[test]
fn specific_domain_prose_is_not_low_quality() {
assert!(!is_low_quality_description(
"OAuth2 token refresh endpoint used by the mobile client"
));
}
#[test]
fn quality_post_filter_rejects_boilerplate_done_candidates() {
let bad = "Foo is a software component in the software project";
assert!(is_low_quality_description(bad));
let good = "ICMS P05 rule for NFC-e ordered invoice sequences in Brazilian state tax";
assert!(!is_low_quality_description(good));
}
}