feat: /sessions cluster --algorithm kmeans --k N for concept-based session grouping (F5.2) #23
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem Statement
The session-search spec (F5.2, lines 432-444) defines three concept-discovery commands. Two are tracked:
/sessions by-concept --connected-to(#22) and/sessions path(#12). The third --/sessions cluster --algorithm kmeans --k 5-- is not tracked and not implemented. Verified:Without clustering, agents cannot answer "group my last 30 days of work into 5 thematic buckets" -- a key cross-session retrospective query that complements
/sessions analyze(#16) and/sessions timeline. Clustering reuses concept vectors already produced bySessionEnricherand the rolegraph's concept-frequency data, so this is an analyser, not new infrastructure.Upstream: spec F5.2 third bullet, Phase 3 Task 3.1.3 dominant-topic identification "concept clustering" callout.
Acceptance Criteria
SessionsSubcommand::Cluster { algorithm: ClusterAlgorithm, k: usize, since: Option<String>, top_n_concepts: Option<usize> }parsed from/sessions cluster --algorithm kmeans --k 5ClusterAlgorithmenum with at leastKmeans(extensible; defaultkmeans)crates/terraphim_sessions/src/enrichment/cluster.rs::SessionClustererthat:SessionEnrichment::conceptskmeans = "..."crate or simple in-tree implementation; no new heavy deps)Vec<SessionCluster { id: usize, dominant_concepts: Vec<String>, session_ids: Vec<SessionId>, centroid_size: usize }>k: clamp to[2, sessions.len().min(20)]; returnExitCode::ERROR_USAGEwith suggestion if input invalidExitCode::ERROR_NOT_FOUNDwith suggestion "run /sessions import first"RobotResponse { data: { algorithm, k, cluster_count, clusters: [{ id, dominant_concepts, session_ids, size }] } }robot/docs.rssorobot schemas sessions clusterworks (matches Task 1.3 self-doc pattern)cargo test -p terraphim_sessions --features enrichment clusterpassescargo test -p terraphim_agent --features repl-sessions,enrichment sessions_clusterpassescargo clippy -p terraphim_sessions --features enrichment -- -D warningspassesProposed Approach
terraphim_sessions(newenrichment/cluster.rs),terraphim_agent(repl/commands.rs,repl/handler.rs,robot/docs.rs)SessionEnrichment::conceptsfor vectors; index concepts viaBTreeMap<String, usize>for sparse-to-dense mappingVec<f32>cosine distance -- avoids adding a crate just for one algorithm--seedflag (hidden) for determinismDependencies
None. Concept enrichment is already shipped per Phase 3 status. Independent of #16 (analyse), #12 (path), #22 (by-concept).
Verification
Scope
Single PR, ~350-450 lines (clusterer ~150, command/handler ~120, robot docs ~30, tests ~100). Closes the third F5.2 command and brings spec coverage of F5.2 from 2/3 to 3/3.