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
147 .families
148 .first()
149 .map(String::as_str)
150 .unwrap_or("one family");
151 let _ = priors::CostClass::Cheap; Some(format!(
153 "Single-family pool ({fam}). For blind-spot independence on the highest-leverage role, \
154 add a cheap second-family API key (e.g. DeepSeek or Gemini Flash) used only as the \
155 evaluator — pennies, and Rinne charges nothing either way."
156 ))
157}
158
159#[cfg(test)]
160mod tests {
161 use super::*;
162 use crate::worker::{Capability, LatencyProfile, QuotaModel, Transport, WorkerFamily};
163
164 fn desc(name: &str, auth: AuthMode, models: &[&str]) -> WorkerDescriptor {
165 WorkerDescriptor {
166 name: name.into(),
167 family: WorkerFamily::Harness,
168 capabilities: vec![Capability::CodeEdit, Capability::Reasoning],
169 auth_mode: auth,
170 quota: QuotaModel::unlimited(),
171 latency: LatencyProfile::Medium,
172 transport: Transport::SubprocessJson,
173 models: models.iter().map(|s| s.to_string()).collect(),
174 }
175 }
176
177 #[test]
178 fn only_claude_is_single_family_with_a_ladder() {
179 let p = profile(&[desc(
181 "claude-code",
182 AuthMode::Subscription,
183 &["haiku", "sonnet", "opus"],
184 )]);
185 assert_eq!(p.shape, PoolShape::SingleFamily);
186 assert!(p.recommend_eval_key);
187 assert!(!has_cross_family(&p));
188 assert_eq!(p.workers[0].ladder, vec!["haiku", "sonnet", "opus"]);
189 }
190
191 #[test]
192 fn claude_plus_grok_is_multi_vendor() {
193 let p = profile(&[
194 desc("claude-code", AuthMode::Subscription, &["sonnet", "opus"]),
195 desc(
196 "grok",
197 AuthMode::Subscription,
198 &["grok-composer-2.5-fast", "grok-build"],
199 ),
200 ]);
201 assert_eq!(p.shape, PoolShape::MultiVendor);
202 assert!(!p.recommend_eval_key);
203 assert!(has_cross_family(&p));
204 }
205
206 #[test]
207 fn subscription_plus_api_is_mixed() {
208 let p = profile(&[
209 desc("claude-code", AuthMode::Subscription, &["sonnet"]),
210 desc("deepseek", AuthMode::ApiKey, &["deepseek-chat"]),
211 ]);
212 assert_eq!(p.shape, PoolShape::Mixed);
213 }
214
215 #[test]
216 fn single_api_pool() {
217 let p = profile(&[desc("openai", AuthMode::ApiKey, &["gpt-mini", "gpt-pro"])]);
218 assert_eq!(p.shape, PoolShape::SingleApi);
219 assert!(eval_key_recommendation(&p).is_some());
220 }
221}