1use std::collections::BTreeSet;
10
11use crate::priors::{self, family_of, family_of_worker, tier_ladder};
12use crate::worker::{AuthMode, WorkerDescriptor};
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum PoolShape {
17 SingleFamily,
20 SingleApi,
22 MultiVendor,
24 Mixed,
27}
28
29impl PoolShape {
30 pub fn label(self) -> &'static str {
31 match self {
32 PoolShape::SingleFamily => "single-family (quota-aware)",
33 PoolShape::SingleApi => "single-api (cost-aware)",
34 PoolShape::MultiVendor => "multi-vendor (cross-family)",
35 PoolShape::Mixed => "mixed subscription + api",
36 }
37 }
38}
39
40#[derive(Debug, Clone)]
42pub struct WorkerTiers {
43 pub worker: String,
44 pub family: String,
45 pub auth: AuthMode,
46 pub ladder: Vec<String>,
48}
49
50#[derive(Debug, Clone)]
52pub struct PoolProfile {
53 pub shape: PoolShape,
54 pub workers: Vec<WorkerTiers>,
55 pub families: Vec<String>,
57 pub recommend_eval_key: bool,
60}
61
62impl PoolProfile {
63 pub fn is_single_family(&self) -> bool {
64 matches!(self.shape, PoolShape::SingleFamily | PoolShape::SingleApi)
65 }
66
67 pub fn ladders(&self) -> std::collections::HashMap<String, Vec<String>> {
71 self.workers
72 .iter()
73 .filter(|w| !w.ladder.is_empty())
74 .map(|w| (w.worker.clone(), w.ladder.clone()))
75 .collect()
76 }
77}
78
79pub fn profile(descriptors: &[WorkerDescriptor]) -> PoolProfile {
81 let mut workers = Vec::new();
82 let mut families: BTreeSet<String> = BTreeSet::new();
83 let mut has_sub = false;
84 let mut has_api = false;
85
86 for d in descriptors {
87 let family = d
89 .models
90 .first()
91 .map(|m| family_of(m))
92 .filter(|f| *f != "unknown")
93 .unwrap_or_else(|| family_of_worker(&d.name))
94 .to_string();
95
96 if family != "unknown" {
97 families.insert(family.clone());
98 }
99 match d.auth_mode {
100 AuthMode::Subscription => has_sub = true,
101 AuthMode::ApiKey => has_api = true,
102 _ => {}
103 }
104
105 workers.push(WorkerTiers {
106 worker: d.name.clone(),
107 family,
108 auth: d.auth_mode,
109 ladder: tier_ladder(&d.models),
110 });
111 }
112
113 let distinct = families.len();
114 let shape = if distinct <= 1 {
115 if has_sub {
116 PoolShape::SingleFamily
117 } else {
118 PoolShape::SingleApi
119 }
120 } else if has_sub && has_api {
121 PoolShape::Mixed
122 } else {
123 PoolShape::MultiVendor
124 };
125
126 PoolProfile {
127 shape,
128 workers,
129 families: families.into_iter().collect(),
130 recommend_eval_key: distinct <= 1,
131 }
132}
133
134pub fn has_cross_family(profile: &PoolProfile) -> bool {
136 profile.families.len() >= 2
137}
138
139pub fn eval_key_recommendation(profile: &PoolProfile) -> Option<String> {
143 if !profile.recommend_eval_key {
144 return None;
145 }
146 let fam = profile.families.first().map(String::as_str).unwrap_or("one family");
147 let _ = priors::CostClass::Cheap; Some(format!(
149 "Single-family pool ({fam}). For blind-spot independence on the highest-leverage role, \
150 add a cheap second-family API key (e.g. DeepSeek or Gemini Flash) used only as the \
151 evaluator — pennies, and Rinne charges nothing either way."
152 ))
153}
154
155#[cfg(test)]
156mod tests {
157 use super::*;
158 use crate::worker::{Capability, LatencyProfile, QuotaModel, Transport, WorkerFamily};
159
160 fn desc(name: &str, auth: AuthMode, models: &[&str]) -> WorkerDescriptor {
161 WorkerDescriptor {
162 name: name.into(),
163 family: WorkerFamily::Harness,
164 capabilities: vec![Capability::CodeEdit, Capability::Reasoning],
165 auth_mode: auth,
166 quota: QuotaModel::unlimited(),
167 latency: LatencyProfile::Medium,
168 transport: Transport::SubprocessJson,
169 models: models.iter().map(|s| s.to_string()).collect(),
170 }
171 }
172
173 #[test]
174 fn only_claude_is_single_family_with_a_ladder() {
175 let p = profile(&[desc("claude-code", AuthMode::Subscription, &["haiku", "sonnet", "opus"])]);
177 assert_eq!(p.shape, PoolShape::SingleFamily);
178 assert!(p.recommend_eval_key);
179 assert!(!has_cross_family(&p));
180 assert_eq!(p.workers[0].ladder, vec!["haiku", "sonnet", "opus"]);
181 }
182
183 #[test]
184 fn claude_plus_grok_is_multi_vendor() {
185 let p = profile(&[
186 desc("claude-code", AuthMode::Subscription, &["sonnet", "opus"]),
187 desc("grok", AuthMode::Subscription, &["grok-composer-2.5-fast", "grok-build"]),
188 ]);
189 assert_eq!(p.shape, PoolShape::MultiVendor);
190 assert!(!p.recommend_eval_key);
191 assert!(has_cross_family(&p));
192 }
193
194 #[test]
195 fn subscription_plus_api_is_mixed() {
196 let p = profile(&[
197 desc("claude-code", AuthMode::Subscription, &["sonnet"]),
198 desc("deepseek", AuthMode::ApiKey, &["deepseek-chat"]),
199 ]);
200 assert_eq!(p.shape, PoolShape::Mixed);
201 }
202
203 #[test]
204 fn single_api_pool() {
205 let p = profile(&[desc("openai", AuthMode::ApiKey, &["gpt-mini", "gpt-pro"])]);
206 assert_eq!(p.shape, PoolShape::SingleApi);
207 assert!(eval_key_recommendation(&p).is_some());
208 }
209}