use crate::types::ChunkType;
pub const MAX_AUTO_PRIORITY: u8 = 7;
pub const MIN_AUTO_PRIORITY: u8 = 3;
pub fn stamp_auto_priority(
chunk_type: ChunkType,
text: &str,
tags: &mut Vec<String>,
) -> Option<u8> {
if has_explicit_priority(tags) {
return None;
}
let computed = compute_priority(chunk_type, text, tags);
if computed < MIN_AUTO_PRIORITY {
return None;
}
let clamped = computed.min(MAX_AUTO_PRIORITY);
tags.push(format!("priority:{clamped}"));
Some(clamped)
}
pub fn has_explicit_priority(tags: &[String]) -> bool {
tags.iter().any(|tag| {
tag.strip_prefix("priority:")
.or_else(|| tag.strip_prefix("importance:"))
.and_then(|value| value.parse::<f32>().ok())
.map(f32::is_finite)
.unwrap_or(false)
})
}
pub fn compute_priority(chunk_type: ChunkType, text: &str, tags: &[String]) -> u8 {
let mut score: i32 = 0;
score += match chunk_type {
ChunkType::Decision => 5,
ChunkType::Summary => 1,
ChunkType::Research => 2,
ChunkType::Trace => 0,
ChunkType::Plan => 1,
_ => 0,
};
for tag in tags {
match tag.as_str() {
"kind:decision" => score += 4,
"kind:finish" => score += 3,
"kind:evidence" => score += 3,
"kind:run" => score += 1,
"kind:progress" => score += 0,
_ if tag.starts_with("kind:consolidated") => score += 6,
_ if tag.starts_with("kind:superseded") => return 0, _ if tag.starts_with("validated:true") || tag == "supports:true" => score += 1,
_ if tag.starts_with("status:failed") => score += 1,
_ if tag.starts_with("ctx:file:") || tag.starts_with("ctx:subsystem:") => score += 1,
_ => {}
}
}
let lowered = text.to_ascii_lowercase();
if lowered.contains("validated:")
|| lowered.contains("validation:")
|| lowered.contains("reproduced")
|| lowered.contains("confirmed")
{
score += 1;
}
if lowered.contains("root cause") || lowered.contains("fix:") {
score += 1;
}
if lowered.starts_with("todo")
|| lowered.starts_with("starting")
|| lowered.starts_with("about to ")
{
score -= 2;
}
score.clamp(0, MAX_AUTO_PRIORITY as i32) as u8
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decision_chunk_with_kind_decision_gets_high_priority() {
let mut tags = vec!["kind:decision".to_string(), "task:T1".to_string()];
let stamped = stamp_auto_priority(
ChunkType::Decision,
"Use tenant-scoped cache keys.",
&mut tags,
);
assert_eq!(stamped, Some(MAX_AUTO_PRIORITY));
assert!(tags
.iter()
.any(|t| t == &format!("priority:{MAX_AUTO_PRIORITY}")));
}
#[test]
fn explicit_user_priority_is_preserved() {
let mut tags = vec!["priority:5".to_string(), "kind:decision".to_string()];
let stamped = stamp_auto_priority(
ChunkType::Decision,
"Use tenant-scoped cache keys.",
&mut tags,
);
assert_eq!(stamped, None);
assert!(tags.iter().filter(|t| t.starts_with("priority:")).count() == 1);
}
#[test]
fn explicit_importance_is_preserved() {
let mut tags = vec!["importance:9".to_string()];
let stamped = stamp_auto_priority(ChunkType::Doc, "any", &mut tags);
assert_eq!(stamped, None);
}
#[test]
fn low_signal_chunk_is_not_stamped() {
let mut tags = vec!["kind:progress".to_string()];
let stamped = stamp_auto_priority(ChunkType::Doc, "todo: investigate", &mut tags);
assert_eq!(stamped, None);
assert!(!tags.iter().any(|t| t.starts_with("priority:")));
}
#[test]
fn routine_summary_without_quality_signal_is_not_stamped() {
let mut tags = vec!["kind:progress".to_string()];
let stamped = stamp_auto_priority(
ChunkType::Summary,
"Read the relevant files and started planning the next change.",
&mut tags,
);
assert_eq!(stamped, None);
assert!(!tags.iter().any(|t| t.starts_with("priority:")));
}
#[test]
fn evidence_with_validation_text_gets_stamped() {
let mut tags = vec!["kind:evidence".to_string(), "supports:true".to_string()];
let stamped = stamp_auto_priority(
ChunkType::Research,
"The failure reproduced before the patch and passed after; validated: yes.",
&mut tags,
);
let value = stamped.expect("evidence with validation should stamp");
assert!(value >= 5, "expected >= 5, got {value}");
}
#[test]
fn superseded_chunk_is_never_stamped() {
let mut tags = vec!["kind:superseded".to_string(), "kind:decision".to_string()];
let stamped = stamp_auto_priority(ChunkType::Decision, "old text", &mut tags);
assert_eq!(stamped, None);
}
#[test]
fn consolidated_summary_gets_high_priority() {
let mut tags = vec!["kind:consolidated".to_string()];
let stamped = stamp_auto_priority(
ChunkType::Summary,
"Tenant-scoped cache keys are the canonical fix.",
&mut tags,
);
assert_eq!(stamped, Some(MAX_AUTO_PRIORITY));
}
#[test]
fn caps_at_max_auto_priority() {
let mut tags = vec![
"kind:decision".to_string(),
"kind:evidence".to_string(),
"kind:finish".to_string(),
"validated:true".to_string(),
];
let stamped =
stamp_auto_priority(ChunkType::Decision, "root cause: fix: validated", &mut tags);
assert_eq!(stamped, Some(MAX_AUTO_PRIORITY));
}
}