fmt: cargo fmt sweep
No logic changes - line reflow, brace placement, and method-chain splits across handlers / personas / state / faces / knowledge / insights_dao / knowledge_dao / populate_knowledge. Picked up incidentally while running fmt for the sms-search work. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -691,7 +691,10 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
// (user_id, persona_id); All = union across the user's
|
||||
// personas (mirror PersonaFilter::All read semantics).
|
||||
let fact_count_join = match persona {
|
||||
PersonaFilter::Single { user_id: _, persona_id: _ } => {
|
||||
PersonaFilter::Single {
|
||||
user_id: _,
|
||||
persona_id: _,
|
||||
} => {
|
||||
"LEFT JOIN (\
|
||||
SELECT subject_entity_id, COUNT(*) AS fact_count \
|
||||
FROM entity_facts \
|
||||
@@ -712,9 +715,7 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
let order_by = match sort {
|
||||
EntitySort::UpdatedDesc => "e.updated_at DESC",
|
||||
EntitySort::NameAsc => "lower(e.name) ASC",
|
||||
EntitySort::FactCountDesc => {
|
||||
"COALESCE(fc.fact_count, 0) DESC, lower(e.name) ASC"
|
||||
}
|
||||
EntitySort::FactCountDesc => "COALESCE(fc.fact_count, 0) DESC, lower(e.name) ASC",
|
||||
};
|
||||
|
||||
let select_sql = format!(
|
||||
@@ -728,9 +729,7 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
LIMIT ? OFFSET ?"
|
||||
);
|
||||
|
||||
let count_sql = format!(
|
||||
"SELECT COUNT(*) AS total FROM entities e {where_clause}"
|
||||
);
|
||||
let count_sql = format!("SELECT COUNT(*) AS total FROM entities e {where_clause}");
|
||||
|
||||
// ── Total count ─────────────────────────────────────────
|
||||
#[derive(diesel::QueryableByName)]
|
||||
@@ -776,7 +775,10 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
// Persona binds first (they're earlier in the SQL — inside
|
||||
// the subquery LEFT JOIN).
|
||||
match persona {
|
||||
PersonaFilter::Single { user_id, persona_id } => {
|
||||
PersonaFilter::Single {
|
||||
user_id,
|
||||
persona_id,
|
||||
} => {
|
||||
q = q
|
||||
.bind::<Integer, _>(*user_id)
|
||||
.bind::<Text, _>(persona_id.clone());
|
||||
@@ -970,14 +972,11 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
// Biggest clusters first; tie-break on the strongest
|
||||
// pair so the most-obvious dupes surface at the top.
|
||||
result.sort_by(|a, b| {
|
||||
b.entities
|
||||
.len()
|
||||
.cmp(&a.entities.len())
|
||||
.then_with(|| {
|
||||
b.max_cosine
|
||||
.partial_cmp(&a.max_cosine)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
})
|
||||
b.entities.len().cmp(&a.entities.len()).then_with(|| {
|
||||
b.max_cosine
|
||||
.partial_cmp(&a.max_cosine)
|
||||
.unwrap_or(std::cmp::Ordering::Equal)
|
||||
})
|
||||
});
|
||||
result.truncate(max_groups);
|
||||
Ok(result)
|
||||
@@ -1286,7 +1285,10 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
.filter(status.ne("rejected"))
|
||||
.filter(user_id.eq(persona.user_id()))
|
||||
.into_boxed();
|
||||
if let PersonaFilter::Single { persona_id: pid, .. } = persona {
|
||||
if let PersonaFilter::Single {
|
||||
persona_id: pid, ..
|
||||
} = persona
|
||||
{
|
||||
q = q.filter(persona_id.eq(pid.clone()));
|
||||
}
|
||||
q.load::<EntityFact>(conn.deref_mut())
|
||||
@@ -1326,7 +1328,11 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
query = query.filter(predicate.eq(pred));
|
||||
count_query = count_query.filter(predicate.eq(pred));
|
||||
}
|
||||
if let PersonaFilter::Single { persona_id: ref pid, .. } = filter.persona {
|
||||
if let PersonaFilter::Single {
|
||||
persona_id: ref pid,
|
||||
..
|
||||
} = filter.persona
|
||||
{
|
||||
query = query.filter(persona_id.eq(pid.clone()));
|
||||
count_query = count_query.filter(persona_id.eq(pid.clone()));
|
||||
}
|
||||
@@ -1499,52 +1505,50 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
None => (None, None),
|
||||
};
|
||||
|
||||
conn.transaction::<Option<EntityFact>, diesel::result::Error, _>(
|
||||
|conn| {
|
||||
// Pull the new fact's valid_from so we can close
|
||||
// the old fact's interval at the same point.
|
||||
let new_fact: Option<EntityFact> = entity_facts
|
||||
.filter(id.eq(new_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()?;
|
||||
let Some(new_fact) = new_fact else {
|
||||
return Ok(None);
|
||||
};
|
||||
conn.transaction::<Option<EntityFact>, diesel::result::Error, _>(|conn| {
|
||||
// Pull the new fact's valid_from so we can close
|
||||
// the old fact's interval at the same point.
|
||||
let new_fact: Option<EntityFact> = entity_facts
|
||||
.filter(id.eq(new_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()?;
|
||||
let Some(new_fact) = new_fact else {
|
||||
return Ok(None);
|
||||
};
|
||||
|
||||
// Verify the old fact exists before touching it —
|
||||
// returning None lets the handler 404 cleanly.
|
||||
let old_fact: Option<EntityFact> = entity_facts
|
||||
.filter(id.eq(old_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()?;
|
||||
if old_fact.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
// Verify the old fact exists before touching it —
|
||||
// returning None lets the handler 404 cleanly.
|
||||
let old_fact: Option<EntityFact> = entity_facts
|
||||
.filter(id.eq(old_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()?;
|
||||
if old_fact.is_none() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
// Only stamp valid_until if the user hasn't
|
||||
// already set it — respecting hand-curated bounds.
|
||||
let target_valid_until = old_fact
|
||||
.as_ref()
|
||||
.and_then(|f| f.valid_until)
|
||||
.or(new_fact.valid_from);
|
||||
// Only stamp valid_until if the user hasn't
|
||||
// already set it — respecting hand-curated bounds.
|
||||
let target_valid_until = old_fact
|
||||
.as_ref()
|
||||
.and_then(|f| f.valid_until)
|
||||
.or(new_fact.valid_from);
|
||||
|
||||
diesel::update(entity_facts.filter(id.eq(old_id)))
|
||||
.set((
|
||||
status.eq("superseded"),
|
||||
superseded_by.eq(Some(new_id)),
|
||||
valid_until.eq(target_valid_until),
|
||||
last_modified_by_model.eq(audit_model.clone()),
|
||||
last_modified_by_backend.eq(audit_backend.clone()),
|
||||
last_modified_at.eq(Some(now)),
|
||||
))
|
||||
.execute(conn)?;
|
||||
diesel::update(entity_facts.filter(id.eq(old_id)))
|
||||
.set((
|
||||
status.eq("superseded"),
|
||||
superseded_by.eq(Some(new_id)),
|
||||
valid_until.eq(target_valid_until),
|
||||
last_modified_by_model.eq(audit_model.clone()),
|
||||
last_modified_by_backend.eq(audit_backend.clone()),
|
||||
last_modified_at.eq(Some(now)),
|
||||
))
|
||||
.execute(conn)?;
|
||||
|
||||
entity_facts
|
||||
.filter(id.eq(old_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()
|
||||
},
|
||||
)
|
||||
entity_facts
|
||||
.filter(id.eq(old_id))
|
||||
.first::<EntityFact>(conn)
|
||||
.optional()
|
||||
})
|
||||
.map_err(|e| anyhow::anyhow!("Supersede error: {}", e))
|
||||
})
|
||||
.map_err(|e| {
|
||||
@@ -1722,7 +1726,10 @@ impl KnowledgeDao for SqliteKnowledgeDao {
|
||||
.filter(ef::created_at.gt(since))
|
||||
.filter(ef::user_id.eq(persona.user_id()))
|
||||
.into_boxed();
|
||||
if let PersonaFilter::Single { persona_id: pid, .. } = persona {
|
||||
if let PersonaFilter::Single {
|
||||
persona_id: pid, ..
|
||||
} = persona
|
||||
{
|
||||
facts_q = facts_q.filter(ef::persona_id.eq(pid.clone()));
|
||||
}
|
||||
let recent_facts = facts_q
|
||||
@@ -1880,7 +1887,14 @@ mod tests {
|
||||
let mut dao = SqliteKnowledgeDao::from_connection(conn.clone());
|
||||
let entity = make_entity(&mut dao, "Cabin");
|
||||
|
||||
add_fact(&mut dao, entity.id, "located_in", "Vermont", alice, "default");
|
||||
add_fact(
|
||||
&mut dao,
|
||||
entity.id,
|
||||
"located_in",
|
||||
"Vermont",
|
||||
alice,
|
||||
"default",
|
||||
);
|
||||
add_fact(&mut dao, entity.id, "color", "red", bob, "default");
|
||||
|
||||
let alice_view = dao
|
||||
@@ -1987,8 +2001,22 @@ mod tests {
|
||||
let mut dao = SqliteKnowledgeDao::from_connection(conn.clone());
|
||||
let entity = make_entity(&mut dao, "Cabin");
|
||||
|
||||
add_fact(&mut dao, entity.id, "p_alice_default", "x", alice, "default");
|
||||
add_fact(&mut dao, entity.id, "p_alice_journal", "y", alice, "journal");
|
||||
add_fact(
|
||||
&mut dao,
|
||||
entity.id,
|
||||
"p_alice_default",
|
||||
"x",
|
||||
alice,
|
||||
"default",
|
||||
);
|
||||
add_fact(
|
||||
&mut dao,
|
||||
entity.id,
|
||||
"p_alice_journal",
|
||||
"y",
|
||||
alice,
|
||||
"journal",
|
||||
);
|
||||
add_fact(&mut dao, entity.id, "p_bob_journal", "z", bob, "journal");
|
||||
|
||||
// Delete alice's journal persona — CASCADE should remove only
|
||||
@@ -2167,7 +2195,9 @@ mod tests {
|
||||
let old = add_fact(&mut dao, cameron.id, "lives_in", "NYC", alice, "default");
|
||||
let new = add_fact(&mut dao, cameron.id, "lives_in", "SF", alice, "default");
|
||||
|
||||
dao.supersede_fact(&cx, old.id, new.id, None).unwrap().unwrap();
|
||||
dao.supersede_fact(&cx, old.id, new.id, None)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
dao.delete_fact(&cx, new.id).unwrap();
|
||||
|
||||
let rehydrated = dao
|
||||
|
||||
Reference in New Issue
Block a user