spectral-fleet
Spectral graph theory for AI agent fleet analysis and optimization.
A fleet of AI agents is modeled as a directed weighted graph: nodes are agents, edges are communication/dependency channels. Spectral analysis of the graph Laplacian reveals hidden structure — clusters, bottlenecks, resilience, and optimal organization.
Why Spectral?
The eigenvalues of the graph Laplacian encode everything about fleet topology:
| Spectral Property | Fleet Meaning |
|---|---|
| Zero eigenvalues | Number of disconnected sub-fleets |
| Fiedler value (λ₂) | Algebraic connectivity — how well-connected the fleet is |
| Spectral gap | Resilience — how robust the fleet is to agent failure |
| Eigenvector signs | Natural cluster boundaries |
| Eigengap | Optimal number of teams |
Quick Start
[]
= "0.1.0"
use ;
// Build a fleet from a GitHub organization
let fleet = from_github_org;
// Analyze the fleet's spectral properties
let spec = spectrum;
println!;
println!;
println!;
// Detect bottlenecks
let bottlenecks = detect_bottlenecks;
for bn in &bottlenecks
// Suggest reorganization
let reorg = suggest_reorganization;
println!;
Modules
fleet_graph — Fleet as a Graph
let g = complete; // All agents connected
let g = star; // Hub-and-spoke
let g = barbell; // Two teams with a bridge
let g = path; // Linear pipeline
let g = two_cliques; // Disconnected teams
Core types:
AgentNode { id, capabilities, load }— a fleet agentCommEdge { from, to, bandwidth, latency }— a communication channelFleetGraph { agents, edges }— the complete fleet topology
laplacian — Graph Laplacian & Spectrum
let spec = spectrum;
// spec.eigenvalues — sorted ascending
// spec.fiedler_value — algebraic connectivity
// spec.spectral_gap — resilience metric
// spec.eigenvectors[i] — i-th eigenvector
let fiedler = fiedler_vector;
// Signs of the Fiedler vector give a natural bisection
The combinatorial Laplacian L = D - A and normalized Laplacian L_sys = I - D^{-1/2} A D^{-1/2} are supported. Eigenvalue decomposition uses the Jacobi algorithm for symmetric matrices.
clustering — Spectral Clustering
// Automatic cluster detection
let clusters = spectral_clustering_auto;
// Or specify k
let clusters = spectral_clustering;
for c in &clusters
// Optimal k from eigengap heuristic
let spec = spectrum;
let k = optimal_k;
Uses k smallest eigenvectors of the Laplacian to embed agents in ℝᵏ, then k-means clustering. The eigengap heuristic automatically selects the best number of teams.
bottleneck — Bottleneck Detection
// Find critical agents
let bottlenecks = detect_bottlenecks;
// Find bridge edges (single points of failure)
let bridges = find_bridge_edges;
// Get bypass suggestions
let bypasses = suggest_bypasses;
// Centrality measures
let betweenness = betweenness_centrality;
let spectral = spectral_centrality;
Identifies agents that are communication choke points using betweenness centrality, spectral centrality, and articulation point detection. Suggests bypass edges to reduce dependency on bottleneck agents.
reorganization — Fleet Optimization
// Optimize for resilience (maximize spectral gap)
let reorg = suggest_for_spectral_gap;
// Optimize for latency (minimize diameter)
let reorg = suggest_for_diameter;
// Optimize for load balance
let reorg = suggest_for_balance;
// Combined optimization
let reorg = suggest_reorganization;
Greedy edge addition/removal to improve spectral gap, diameter, and degree balance.
dynamics — Temporal Evolution
let mut fleet = new;
fleet.add_agent;
fleet.add_edge;
fleet.remove_edge;
// Track spectral changes over time
let traj = fleet.fiedler_trajectory;
let transitions = fleet.detect_transitions;
for t in transitions
Tracks how the fleet graph evolves over time, detecting phase transitions (e.g., fleet becoming disconnected) when the Fiedler value drops to zero.
embedding — Visualization
// 2D spectral embedding
let emb = spectral_embedding;
// t-SNE style embedding for better visualization
let emb = tsne_embedding;
// ASCII art rendering
let ascii = render_ascii;
println!;
Maps agents to low-dimensional space where distance ≈ communication cost.
Fleet Optimization Examples
Example 1: Detecting Team Structure
// Two teams connected by a single bridge agent
let fleet = barbell;
let clusters = spectral_clustering;
// Correctly identifies the two teams
let bridges = find_bridge_edges;
// Finds the bridge edge between teams
Example 2: Improving Fleet Resilience
let fleet = star; // Hub-and-spoke: fragile
let spec_before = spectrum;
let reorg = suggest_for_spectral_gap;
let mut improved = fleet.clone;
for & in &reorg.add_edges
let spec_after = spectrum;
// Spectral gap improved → more resilient fleet
Example 3: Monitoring Fleet Health
let mut fleet = new;
// Agent failure removes edges
fleet.remove_edge;
fleet.remove_edge;
// Phase transition detected: fleet became disconnected
for t in fleet.detect_transitions
Test Coverage
39 tests covering:
- Complete graph → single zero eigenvalue
- Disconnected graph → multiple zero eigenvalues
- Connected graph → positive Fiedler value
- Disconnected graph → zero Fiedler value
- Laplacian row sums = 0
- Laplacian diagonal non-negative
- Component counting from spectrum
- Fiedler vector existence
- Normalized Laplacian correctness
- Spectral clustering recovers known communities (barbell)
- Eigengap heuristic detects cluster count
- Bridge agent detection (star graph center)
- Bridge edge detection (barbell bridge)
- Betweenness centrality (star graph center highest)
- Spectral centrality non-negativity
- Bottleneck detection
- Bypass suggestions
- Graph diameter (path, complete)
- Degree balance (complete = 1.0, star < 0.5)
- Spectral gap improvement after reorganization
- Average shortest path
- Phase transition on disconnection
- Agent dynamics (add/remove)
- Fiedler trajectory tracking
- Spectral embedding dimensions
- Cluster preservation in embedding
- t-SNE embedding
- ASCII rendering
Architecture
spectral-fleet/
├── src/
│ ├── lib.rs # Re-exports and documentation
│ ├── fleet_graph.rs # Graph types and constructors
│ ├── laplacian.rs # Laplacian, Jacobi eigenvalue decomposition
│ ├── clustering.rs # Spectral clustering, k-means, eigengap
│ ├── bottleneck.rs # Betweenness centrality, bridge detection
│ ├── reorganization.rs # Edge optimization (spectral gap, diameter, balance)
│ ├── dynamics.rs # Temporal evolution, phase transitions
│ └── embedding.rs # Spectral embedding, t-SNE, ASCII rendering
└── tests/
└── spectral_tests.rs # 39 integration tests
Dependencies
ndarray— matrix operationsserde/serde_json— serialization
License
MIT