extract truncation helpers, fix clippy warnings, dedup batching loop
Add util::truncate() and util::first_n_chars() to replace 16 call sites doing the same floor_char_boundary or chars().take().collect() patterns. Deduplicate the batching loop in consolidate.rs (4 copies → 1 loop over an array). Fix all clippy warnings: redundant closures, needless borrows, collapsible if, unnecessary cast, manual strip_prefix. Net: -44 lines across 16 files.
This commit is contained in:
parent
e24dee6bdf
commit
52523403c5
16 changed files with 85 additions and 129 deletions
|
|
@ -66,40 +66,18 @@ pub fn consolidate_full_with_progress(
|
|||
if plan.run_health {
|
||||
runs.push(("health", 0));
|
||||
}
|
||||
if plan.replay_count > 0 {
|
||||
let batch = 5;
|
||||
let mut remaining = plan.replay_count;
|
||||
let batch_size = 5;
|
||||
for (name, count) in [
|
||||
("replay", plan.replay_count),
|
||||
("linker", plan.linker_count),
|
||||
("separator", plan.separator_count),
|
||||
("transfer", plan.transfer_count),
|
||||
] {
|
||||
let mut remaining = count;
|
||||
while remaining > 0 {
|
||||
let this_batch = remaining.min(batch);
|
||||
runs.push(("replay", this_batch));
|
||||
remaining -= this_batch;
|
||||
}
|
||||
}
|
||||
if plan.linker_count > 0 {
|
||||
let batch = 5;
|
||||
let mut remaining = plan.linker_count;
|
||||
while remaining > 0 {
|
||||
let this_batch = remaining.min(batch);
|
||||
runs.push(("linker", this_batch));
|
||||
remaining -= this_batch;
|
||||
}
|
||||
}
|
||||
if plan.separator_count > 0 {
|
||||
let batch = 5;
|
||||
let mut remaining = plan.separator_count;
|
||||
while remaining > 0 {
|
||||
let this_batch = remaining.min(batch);
|
||||
runs.push(("separator", this_batch));
|
||||
remaining -= this_batch;
|
||||
}
|
||||
}
|
||||
if plan.transfer_count > 0 {
|
||||
let batch = 5;
|
||||
let mut remaining = plan.transfer_count;
|
||||
while remaining > 0 {
|
||||
let this_batch = remaining.min(batch);
|
||||
runs.push(("transfer", this_batch));
|
||||
remaining -= this_batch;
|
||||
let batch = remaining.min(batch_size);
|
||||
runs.push((name, batch));
|
||||
remaining -= batch;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -104,7 +104,7 @@ fn job_experience_mine(ctx: &ExecutionContext, path: &str, segment: Option<usize
|
|||
let mut store = crate::store::Store::load()?;
|
||||
ctx.log_line("mining");
|
||||
let count = crate::enrich::experience_mine(&mut store, &path, segment)?;
|
||||
ctx.log_line(&format!("{} entries mined", count));
|
||||
ctx.log_line(format!("{count} entries mined"));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ fn job_fact_mine(ctx: &ExecutionContext, path: &str) -> Result<(), TaskError> {
|
|||
let p = std::path::Path::new(&path);
|
||||
let progress = |msg: &str| { ctx.set_progress(msg); };
|
||||
let count = crate::fact_mine::mine_and_store(p, Some(&progress))?;
|
||||
ctx.log_line(&format!("{} facts stored", count));
|
||||
ctx.log_line(format!("{count} facts stored"));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
|
@ -140,7 +140,7 @@ fn job_knowledge_loop(ctx: &ExecutionContext) -> Result<(), TaskError> {
|
|||
};
|
||||
ctx.log_line("running agents");
|
||||
let results = crate::knowledge::run_knowledge_loop(&config)?;
|
||||
ctx.log_line(&format!("{} cycles, {} actions",
|
||||
ctx.log_line(format!("{} cycles, {} actions",
|
||||
results.len(),
|
||||
results.iter().map(|r| r.total_applied).sum::<usize>()));
|
||||
Ok(())
|
||||
|
|
@ -505,7 +505,7 @@ pub fn run_daemon() -> Result<(), String> {
|
|||
if extract_pending > 0 { parts.push(format!("{} extract", extract_pending)); }
|
||||
if fact_pending > 0 { parts.push(format!("{} fact", fact_pending)); }
|
||||
if still_open > 0 { parts.push(format!("{} open", still_open)); }
|
||||
ctx.set_progress(&parts.join(", "));
|
||||
ctx.set_progress(parts.join(", "));
|
||||
} else {
|
||||
ctx.set_progress("idle");
|
||||
}
|
||||
|
|
@ -575,7 +575,7 @@ pub fn run_daemon() -> Result<(), String> {
|
|||
digest.depend_on(&knowledge);
|
||||
|
||||
*last_daily_sched.lock().unwrap() = Some(today);
|
||||
ctx.set_progress(&format!("daily pipeline triggered ({})", today));
|
||||
ctx.set_progress(format!("daily pipeline triggered ({today})"));
|
||||
}
|
||||
|
||||
// Prune finished tasks from registry
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ use std::collections::BTreeSet;
|
|||
|
||||
// --- Digest level descriptors ---
|
||||
|
||||
#[allow(clippy::type_complexity)]
|
||||
struct DigestLevel {
|
||||
name: &'static str,
|
||||
title: &'static str,
|
||||
|
|
|
|||
|
|
@ -165,11 +165,7 @@ pub fn split_on_compaction(messages: Vec<(usize, String, String, String)>) -> Ve
|
|||
fn format_conversation(messages: &[(usize, String, String, String)]) -> String {
|
||||
messages.iter()
|
||||
.map(|(line, role, text, ts)| {
|
||||
let text = if text.len() > 2000 {
|
||||
format!("{}...[truncated]", &text[..text.floor_char_boundary(1800)])
|
||||
} else {
|
||||
text.clone()
|
||||
};
|
||||
let text = crate::util::truncate(text, 1800, "...[truncated]");
|
||||
if ts.is_empty() {
|
||||
format!("L{} [{}]: {}", line, role, text)
|
||||
} else {
|
||||
|
|
@ -424,13 +420,8 @@ pub fn experience_mine(
|
|||
let _ = store.upsert_node(node);
|
||||
count += 1;
|
||||
|
||||
let preview = if content.len() > 80 {
|
||||
let end = content.floor_char_boundary(77);
|
||||
&content[..end]
|
||||
} else {
|
||||
content
|
||||
};
|
||||
println!(" + [{}] {}...", ts, preview);
|
||||
let preview = crate::util::truncate(content, 77, "...");
|
||||
println!(" + [{}] {}", ts, preview);
|
||||
}
|
||||
|
||||
// Record this transcript/segment as mined (even if count == 0, to prevent re-runs)
|
||||
|
|
|
|||
|
|
@ -144,13 +144,7 @@ fn extract_conversation(path: &Path) -> Vec<Message> {
|
|||
fn format_for_extraction(messages: &[Message]) -> String {
|
||||
messages.iter()
|
||||
.map(|msg| {
|
||||
let text = if msg.text.len() > 3000 {
|
||||
// Find a char boundary near 2800
|
||||
let trunc = msg.text.floor_char_boundary(2800);
|
||||
format!("{}\n[...truncated...]", &msg.text[..trunc])
|
||||
} else {
|
||||
msg.text.clone()
|
||||
};
|
||||
let text = crate::util::truncate(&msg.text, 2800, "\n[...truncated...]");
|
||||
let ts = if msg.timestamp.len() >= 19 { &msg.timestamp[..19] } else { "" };
|
||||
if ts.is_empty() {
|
||||
format!("[{}] {}", msg.role, text)
|
||||
|
|
@ -244,8 +238,7 @@ pub fn mine_transcript(
|
|||
if dry_run {
|
||||
for (i, (offset, chunk)) in chunks.iter().enumerate() {
|
||||
eprintln!("\n--- Chunk {} (offset {}, {} chars) ---", i + 1, offset, chunk.len());
|
||||
let preview = if chunk.len() > 500 { &chunk[..chunk.floor_char_boundary(500)] } else { chunk };
|
||||
eprintln!("{}", preview);
|
||||
eprintln!("{}", crate::util::truncate(chunk, 500, ""));
|
||||
if chunk.len() > 500 {
|
||||
eprintln!(" ... ({} more chars)", chunk.len() - 500);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -631,8 +631,8 @@ pub fn run_challenger(store: &Store, graph: &Graph, batch_size: usize) -> Result
|
|||
let template = load_prompt("challenger")?;
|
||||
let topology = get_graph_topology(store, graph);
|
||||
|
||||
let mut candidates: Vec<(&String, usize)> = store.nodes.iter()
|
||||
.map(|(k, _)| (k, graph.degree(k)))
|
||||
let mut candidates: Vec<(&String, usize)> = store.nodes.keys()
|
||||
.map(|k| (k, graph.degree(k)))
|
||||
.collect();
|
||||
candidates.sort_by(|a, b| b.1.cmp(&a.1));
|
||||
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ fn call_model(agent: &str, model: &str, prompt: &str) -> Result<String, String>
|
|||
Ok(response)
|
||||
} else {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
let preview: String = stderr.chars().take(500).collect();
|
||||
let preview = crate::util::first_n_chars(&stderr, 500);
|
||||
log_usage(agent, model, prompt, &preview, elapsed, false);
|
||||
Err(format!("claude exited {}: {}", output.status, preview.trim()))
|
||||
}
|
||||
|
|
@ -129,7 +129,7 @@ pub(crate) fn parse_json_response(response: &str) -> Result<serde_json::Value, S
|
|||
}
|
||||
}
|
||||
|
||||
let preview: String = cleaned.chars().take(200).collect();
|
||||
let preview = crate::util::first_n_chars(cleaned, 200);
|
||||
Err(format!("no valid JSON in response: {preview}..."))
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -637,10 +637,11 @@ fn cmd_search(terms: &[String], expand: bool) -> Result<(), String> {
|
|||
let weight = view.node_weight(k);
|
||||
println!(" ~ [{:.2}] {}", weight, k);
|
||||
if let Some(content) = view.node_content(k) {
|
||||
let snippet: String = content.lines()
|
||||
.find(|l| !l.trim().is_empty() && !l.starts_with('#'))
|
||||
.unwrap_or("")
|
||||
.chars().take(100).collect();
|
||||
let snippet = util::first_n_chars(
|
||||
content.lines()
|
||||
.find(|l| !l.trim().is_empty() && !l.starts_with('#'))
|
||||
.unwrap_or(""),
|
||||
100);
|
||||
if !snippet.is_empty() {
|
||||
println!(" {}", snippet);
|
||||
}
|
||||
|
|
@ -671,13 +672,11 @@ fn cmd_init() -> Result<(), String> {
|
|||
let mut store = store::Store::load()?;
|
||||
let count = store.init_from_markdown()?;
|
||||
for key in &cfg.core_nodes {
|
||||
if !store.nodes.contains_key(key.as_str()) {
|
||||
if key == "identity" {
|
||||
let default = include_str!("../defaults/identity.md");
|
||||
store.upsert(key, default)
|
||||
.map_err(|e| format!("seed {}: {}", key, e))?;
|
||||
println!("Seeded {} in store", key);
|
||||
}
|
||||
if !store.nodes.contains_key(key) && key == "identity" {
|
||||
let default = include_str!("../defaults/identity.md");
|
||||
store.upsert(key, default)
|
||||
.map_err(|e| format!("seed {}: {}", key, e))?;
|
||||
println!("Seeded {} in store", key);
|
||||
}
|
||||
}
|
||||
store.save()?;
|
||||
|
|
@ -1319,12 +1318,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
|
|||
}
|
||||
|
||||
// Show content preview
|
||||
let preview = if node.content.len() > 200 {
|
||||
let end = node.content.floor_char_boundary(200);
|
||||
format!("{}...", &node.content[..end])
|
||||
} else {
|
||||
node.content.clone()
|
||||
};
|
||||
let preview = util::truncate(&node.content, 200, "...");
|
||||
println!("\n{}\n", preview);
|
||||
|
||||
// Walk neighbors, grouped by node type
|
||||
|
|
@ -1354,7 +1348,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
|
|||
if !episodic_weekly.is_empty() {
|
||||
println!("Weekly digests:");
|
||||
for (k, s, n) in &episodic_weekly {
|
||||
let preview = n.content.lines().next().unwrap_or("").chars().take(80).collect::<String>();
|
||||
let preview = util::first_n_chars(n.content.lines().next().unwrap_or(""), 80);
|
||||
println!(" [{:.2}] {} — {}", s, k, preview);
|
||||
}
|
||||
}
|
||||
|
|
@ -1362,7 +1356,7 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
|
|||
if !episodic_daily.is_empty() {
|
||||
println!("Daily digests:");
|
||||
for (k, s, n) in &episodic_daily {
|
||||
let preview = n.content.lines().next().unwrap_or("").chars().take(80).collect::<String>();
|
||||
let preview = util::first_n_chars(n.content.lines().next().unwrap_or(""), 80);
|
||||
println!(" [{:.2}] {} — {}", s, k, preview);
|
||||
}
|
||||
}
|
||||
|
|
@ -1370,9 +1364,11 @@ fn cmd_trace(key: &[String]) -> Result<(), String> {
|
|||
if !episodic_session.is_empty() {
|
||||
println!("Session entries:");
|
||||
for (k, s, n) in &episodic_session {
|
||||
let preview = n.content.lines()
|
||||
.find(|l| !l.is_empty() && !l.starts_with("<!--"))
|
||||
.unwrap_or("").chars().take(80).collect::<String>();
|
||||
let preview = util::first_n_chars(
|
||||
n.content.lines()
|
||||
.find(|l| !l.is_empty() && !l.starts_with("<!--"))
|
||||
.unwrap_or(""),
|
||||
80);
|
||||
println!(" [{:.2}] {}", s, k);
|
||||
if !n.source_ref.is_empty() {
|
||||
println!(" ↳ source: {}", n.source_ref);
|
||||
|
|
@ -1625,7 +1621,7 @@ fn cmd_journal_ts_migrate() -> Result<(), String> {
|
|||
}
|
||||
}
|
||||
if let Some(node) = store.nodes.get_mut(key) {
|
||||
node.created_at = node.timestamp as i64;
|
||||
node.created_at = node.timestamp;
|
||||
node.version += 1;
|
||||
updated += 1;
|
||||
}
|
||||
|
|
@ -1811,7 +1807,7 @@ fn cmd_history(key: &[String], full: bool) -> Result<(), String> {
|
|||
node.version, ts, node.provenance.label(), node.weight, content_len);
|
||||
eprintln!("{}", node.content);
|
||||
} else {
|
||||
let preview: String = node.content.chars().take(120).collect();
|
||||
let preview = util::first_n_chars(&node.content, 120);
|
||||
let preview = preview.replace('\n', "\\n");
|
||||
eprintln!(" v{:<3} {} {:24} w={:.3} {}b",
|
||||
node.version, ts, node.provenance.label(), node.weight, content_len);
|
||||
|
|
@ -2065,18 +2061,12 @@ fn extract_title(content: &str) -> String {
|
|||
let stripped = line.trim();
|
||||
if stripped.is_empty() { continue; }
|
||||
if date_re.is_match(stripped) && stripped.len() < 25 { continue; }
|
||||
if stripped.starts_with("## ") {
|
||||
return stripped[3..].to_string();
|
||||
} else if stripped.starts_with("# ") {
|
||||
return stripped[2..].to_string();
|
||||
if let Some(h) = stripped.strip_prefix("## ") {
|
||||
return h.to_string();
|
||||
} else if let Some(h) = stripped.strip_prefix("# ") {
|
||||
return h.to_string();
|
||||
} else {
|
||||
return if stripped.len() > 70 {
|
||||
let mut end = 67;
|
||||
while !stripped.is_char_boundary(end) { end -= 1; }
|
||||
format!("{}...", &stripped[..end])
|
||||
} else {
|
||||
stripped.to_string()
|
||||
};
|
||||
return util::truncate(stripped, 67, "...");
|
||||
}
|
||||
}
|
||||
String::from("(untitled)")
|
||||
|
|
|
|||
|
|
@ -118,9 +118,9 @@ fn format_nodes_section(store: &Store, items: &[ReplayItem], graph: &Graph) -> S
|
|||
// Content (truncated for large nodes)
|
||||
let content = &node.content;
|
||||
if content.len() > 1500 {
|
||||
let end = content.floor_char_boundary(1500);
|
||||
out.push_str(&format!("\nContent ({} chars, truncated):\n{}\n[...]\n\n",
|
||||
content.len(), &content[..end]));
|
||||
let truncated = crate::util::truncate(content, 1500, "\n[...]");
|
||||
out.push_str(&format!("\nContent ({} chars, truncated):\n{}\n\n",
|
||||
content.len(), truncated));
|
||||
} else {
|
||||
out.push_str(&format!("\nContent:\n{}\n\n", content));
|
||||
}
|
||||
|
|
@ -248,12 +248,7 @@ fn format_pairs_section(
|
|||
// Node A
|
||||
out.push_str(&format!("\n### {} ({})\n", a, ca));
|
||||
if let Some(node) = store.nodes.get(a) {
|
||||
let content = if node.content.len() > 500 {
|
||||
let end = node.content.floor_char_boundary(500);
|
||||
format!("{}...", &node.content[..end])
|
||||
} else {
|
||||
node.content.clone()
|
||||
};
|
||||
let content = crate::util::truncate(&node.content, 500, "...");
|
||||
out.push_str(&format!("Weight: {:.2}\n{}\n",
|
||||
node.weight, content));
|
||||
}
|
||||
|
|
@ -261,12 +256,7 @@ fn format_pairs_section(
|
|||
// Node B
|
||||
out.push_str(&format!("\n### {} ({})\n", b, cb));
|
||||
if let Some(node) = store.nodes.get(b) {
|
||||
let content = if node.content.len() > 500 {
|
||||
let end = node.content.floor_char_boundary(500);
|
||||
format!("{}...", &node.content[..end])
|
||||
} else {
|
||||
node.content.clone()
|
||||
};
|
||||
let content = crate::util::truncate(&node.content, 500, "...");
|
||||
out.push_str(&format!("Weight: {:.2}\n{}\n",
|
||||
node.weight, content));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -106,10 +106,11 @@ pub fn differentiate_hub_with_graph(store: &Store, hub_key: &str, graph: &Graph)
|
|||
|
||||
// Only propose move if there's a reasonable match
|
||||
if best_sim > 0.05 && !best_section.is_empty() {
|
||||
let snippet = neighbor_content.lines()
|
||||
.find(|l| !l.is_empty() && !l.starts_with("<!--") && !l.starts_with("##"))
|
||||
.unwrap_or("")
|
||||
.chars().take(80).collect::<String>();
|
||||
let snippet = crate::util::first_n_chars(
|
||||
neighbor_content.lines()
|
||||
.find(|l| !l.is_empty() && !l.starts_with("<!--") && !l.starts_with("##"))
|
||||
.unwrap_or(""),
|
||||
80);
|
||||
|
||||
moves.push(LinkMove {
|
||||
neighbor_key: neighbor_key.to_string(),
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ pub fn replay_queue_with_graph(
|
|||
let pos = positions.get(key);
|
||||
let outlier_score = pos.map(|p| p.outlier_score).unwrap_or(0.0);
|
||||
let classification = pos
|
||||
.map(|p| spectral::classify_position(p))
|
||||
.map(spectral::classify_position)
|
||||
.unwrap_or("unknown");
|
||||
|
||||
let priority = consolidation_priority(
|
||||
|
|
|
|||
|
|
@ -404,13 +404,13 @@ fn execute_parsed(
|
|||
has_sort = true;
|
||||
let asc = *ascending;
|
||||
results.sort_by(|a, b| {
|
||||
let va = a.fields.get(field).and_then(|v| as_num(v));
|
||||
let vb = b.fields.get(field).and_then(|v| as_num(v));
|
||||
let va = a.fields.get(field).and_then(as_num);
|
||||
let vb = b.fields.get(field).and_then(as_num);
|
||||
let ord = match (va, vb) {
|
||||
(Some(a), Some(b)) => a.total_cmp(&b),
|
||||
_ => {
|
||||
let sa = a.fields.get(field).map(|v| as_str(v)).unwrap_or_default();
|
||||
let sb = b.fields.get(field).map(|v| as_str(v)).unwrap_or_default();
|
||||
let sa = a.fields.get(field).map(as_str).unwrap_or_default();
|
||||
let sb = b.fields.get(field).map(as_str).unwrap_or_default();
|
||||
sa.cmp(&sb)
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -89,12 +89,7 @@ pub fn search(query: &str, store: &impl StoreView) -> Vec<SearchResult> {
|
|||
.take(3)
|
||||
.map(|l| {
|
||||
let t = l.trim();
|
||||
if t.len() > 100 {
|
||||
let end = t.floor_char_boundary(97);
|
||||
format!("{}...", &t[..end])
|
||||
} else {
|
||||
t.to_string()
|
||||
}
|
||||
crate::util::truncate(t, 97, "...")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n ");
|
||||
|
|
|
|||
|
|
@ -26,6 +26,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
|||
macro_rules! capnp_enum {
|
||||
($rust_type:ident, $capnp_type:path, [$($variant:ident),+ $(,)?]) => {
|
||||
impl $rust_type {
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
pub(crate) fn to_capnp(&self) -> $capnp_type {
|
||||
match self {
|
||||
$(Self::$variant => <$capnp_type>::$variant,)+
|
||||
|
|
|
|||
|
|
@ -106,7 +106,7 @@ impl StoreView for MmapView {
|
|||
fn for_each_node<F: FnMut(&str, &str, f32)>(&self, mut f: F) {
|
||||
let snap = self.snapshot();
|
||||
for (key, node) in snap.nodes.iter() {
|
||||
f(&key, &node.content, node.weight);
|
||||
f(key, &node.content, node.weight);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,3 +12,19 @@ pub fn memory_subdir(name: &str) -> Result<PathBuf, String> {
|
|||
.map_err(|e| format!("create {}: {}", dir.display(), e))?;
|
||||
Ok(dir)
|
||||
}
|
||||
|
||||
/// Truncate text to `max_len` bytes at a char boundary, appending `suffix`.
|
||||
/// Returns the original string if it's already short enough.
|
||||
pub fn truncate(text: &str, max_len: usize, suffix: &str) -> String {
|
||||
if text.len() <= max_len {
|
||||
text.to_string()
|
||||
} else {
|
||||
let end = text.floor_char_boundary(max_len);
|
||||
format!("{}{}", &text[..end], suffix)
|
||||
}
|
||||
}
|
||||
|
||||
/// Take the first `n` chars from a string.
|
||||
pub fn first_n_chars(s: &str, n: usize) -> String {
|
||||
s.chars().take(n).collect()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue