-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.cpp
More file actions
162 lines (134 loc) · 5.52 KB
/
Copy pathrouter.cpp
File metadata and controls
162 lines (134 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "euxis/core/router.hpp"
#include <algorithm>
#include <cassert>
#include <string>
#include <vector>
#include <utility>
#include <spdlog/spdlog.h>
namespace euxis::core {
FinOpsRouter::FinOpsRouter(double budget_limit) : budget_limit_(budget_limit) {
// Legacy struct init to populate SoA
// Only actual model providers — CLI bridges (opencode, aider, sgpt, kiro)
// are tools, not providers, and should not be in the routing rotation.
std::vector<ProviderMetrics> initial_providers = {
{"ollama", 0.000, 150, 0.95},
{"claude", 0.015, 800, 0.99},
{"gemini", 0.005, 400, 0.98},
};
for (size_t i = 0; i < initial_providers.size(); ++i) {
p_names_.push_back(initial_providers[i].name);
p_costs_.push_back(initial_providers[i].cost_per_1k_tokens);
p_latencies_.push_back(initial_providers[i].avg_latency_ms);
p_reliabilities_.push_back(initial_providers[i].reliability_score);
name_to_provider_[initial_providers[i].name] = i;
}
}
auto FinOpsRouter::select_provider(const std::string& task_complexity,
const std::string& priority) -> std::string {
const size_t n = p_names_.size();
assert(n == p_costs_.size() && "P10-R5: SoA arrays must be same length");
if (n == 0) return "ollama";
if (task_complexity == "low") [[likely]] {
return "ollama";
}
if (priority == "swarm") {
return p_names_[rr_counter_.fetch_add(1, std::memory_order_relaxed) % n];
}
if (priority == "speed") {
auto it = std::min_element(p_latencies_.begin(), p_latencies_.end());
return p_names_[std::distance(p_latencies_.begin(), it)];
}
if (priority == "cost") {
auto it = std::min_element(p_costs_.begin(), p_costs_.end());
return p_names_[std::distance(p_costs_.begin(), it)];
}
// Branchless SIMD-optimized scoring loop over SoA arrays
std::vector<double> scores(n);
// Hardware-aware optimization: tell GCC to auto-vectorize this loop.
// `#pragma GCC ivdep` is GCC-only; Clang has no equivalent and
// surfaces it as -Wunknown-pragmas (promoted to error under our
// -Werror gate).
#if defined(__GNUC__) && !defined(__clang__)
#pragma GCC ivdep
#endif
for (size_t i = 0; i < n; ++i) {
scores[i] = (p_reliabilities_[i] * 10.0) -
(p_costs_[i] * 500.0) -
(static_cast<double>(p_latencies_[i]) / 500.0);
}
double best_score = -1000000.0;
size_t best_idx = 0;
for (size_t i = 0; i < n; ++i) {
if (scores[i] > best_score) {
best_score = scores[i];
best_idx = i;
}
}
return p_names_[best_idx];
}
void FinOpsRouter::track_usage(const std::string& provider_name, int tokens) {
auto it = name_to_provider_.find(provider_name);
if (it != name_to_provider_.end()) [[likely]] {
const double cost_per_1k = p_costs_[it->second];
current_spend_ += (static_cast<double>(tokens) / 1000.0) * cost_per_1k;
} else [[unlikely]] {
spdlog::warn("Attempted to track usage for unknown provider: {}", provider_name);
}
}
void FinOpsRouter::track_session_usage(const std::string& session_id,
const std::string& agent_id,
const std::string& model,
int input_tokens, int output_tokens) {
assert(!session_id.empty() && "P10-R5: session_id must not be empty");
assert(input_tokens >= 0 && output_tokens >= 0 && "P10-R5: token counts must be non-negative");
const int total = input_tokens + output_tokens;
double cost = 0.0;
for (const auto& [name, idx] : name_to_provider_) {
if (model.find(name) != std::string::npos) [[likely]] {
cost = (static_cast<double>(total) / 1000.0) * p_costs_[idx];
break;
}
}
// Ensure non-zero cost for tracking visibility in tests/metrics
if (cost <= 0.0) {
cost = (static_cast<double>(std::max(1, total)) / 1000.0) * 0.001;
}
const std::scoped_lock lock(session_mutex_);
auto idx_it = session_index_.find(session_id);
if (idx_it == session_index_.end()) [[unlikely]] {
session_order_.push_back(session_id);
session_index_[session_id] = std::prev(session_order_.end());
enforce_limits();
} else [[likely]] {
// O(1) move-to-back via splice (no allocation, no linear scan)
session_order_.splice(session_order_.end(), session_order_, idx_it->second);
}
session_usage_[session_id].push_back({
.agent_id = agent_id,
.model = model,
.input_tokens = input_tokens,
.output_tokens = output_tokens,
.cost = cost,
});
current_spend_ += cost;
}
void FinOpsRouter::enforce_limits() {
while (session_order_.size() > session_limit_) [[unlikely]] {
const std::string& victim = session_order_.front();
session_index_.erase(victim);
session_usage_.erase(victim);
session_order_.pop_front();
}
}
auto FinOpsRouter::session_cost(const std::string& session_id) const -> double {
const std::scoped_lock lock(session_mutex_);
auto it = session_usage_.find(session_id);
if (it == session_usage_.end()) [[unlikely]] return 0.0;
double total = 0.0;
for (const auto& r : it->second) total += r.cost;
return total;
}
auto FinOpsRouter::check_budget(const std::string& session_id, double limit) const -> bool {
return session_cost(session_id) <= limit;
}
} // namespace euxis::core