KeiSeiKit-1.0/_primitives/_rust/kei-brain-view/src/clusters.rs
Parfii-bot 0be354a920 KeiSeiKit-public — clean state
Single-commit clean baseline after security scrub of niche-tells,
project codenames, internal jargon, and contributor-email leaks.

Contents:
- 100 Rust crates (_primitives/_rust/)
- 37 agent manifests (_manifests/) + generated specs (_generated/)
- 67 user-invocable skills (skills/)
- 33 hooks (hooks/)
- Composition blocks (_blocks/)
- Documentation (docs/, README.md)
- TS adapter packages (_ts_packages/)
- Assembler (_assembler/)
- Roles (_roles/)
- Templates (_templates/)
- Forgejo CI (.forgejo/)

Author: Denis Parfionovich <info@greendragon.info>

License: see LICENSE.
2026-05-01 12:09:03 +08:00

48 lines
1.6 KiB
Rust

//! Cluster rendering over kei-dna-index groupings.
//!
//! Constructor Pattern: one file = one responsibility (render clusters).
//! Pulls cluster groupings from `kei-dna-index` and decorates each member
//! with its current ledger status. Read-only — no schema mutation.
use crate::error::BrainViewError;
use kei_dna_index::{cluster_by, ClusterBy};
use rusqlite::Connection;
/// Render the cluster tree for `by` as an ASCII text block.
///
/// Each non-singleton cluster becomes a `CLUSTER <key> (N members)` header
/// followed by ` └─ <dna> [status]` rows. Empty clusters produce `""`.
pub fn render_clusters(
conn: &Connection,
by: ClusterBy,
) -> Result<String, BrainViewError> {
let clusters = cluster_by(conn, by)?;
if clusters.is_empty() {
return Ok(String::new());
}
let mut out = String::new();
for c in clusters {
out.push_str(&format!(
"CLUSTER {} ({} members)\n",
c.key,
c.members.len()
));
for dna in &c.members {
let status = lookup_status(conn, dna);
out.push_str(&format!(" └─ {} [{}]\n", dna, status));
}
}
Ok(out)
}
/// Return the `agents.status` column for the row whose DNA matches.
/// Falls back to `"unknown"` if the row is missing or the query fails —
/// rendering must always succeed even against a sparsely-populated ledger.
fn lookup_status(conn: &Connection, dna: &str) -> String {
conn.query_row(
"SELECT COALESCE(status,'unknown') FROM agents WHERE dna = ?1",
[dna],
|r| r.get::<_, String>(0),
)
.unwrap_or_else(|_| "unknown".to_string())
}