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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//! The extension-runtime LLM port built over the WORKER's own resolved
//! backend.
//!
//! `build_ext_runtime`'s fallback used to be the env-keyed `EnvLlmPort`, whose
//! provider defaults to `deepseek`. So in every deployed lane an extension's
//! `host.llm.complete` ran on a provider the operator never selected — or, with
//! no env key at all, failed with "llm not configured for this runtime" — while
//! the correct backend was already resolved a few lines below in the same
//! function. This wraps THAT backend.
//!
//! The port ignores `role`, as `EnvLlmPort` does: a deployed runtime has no
//! per-role provider map, and inventing one would be a configuration surface an
//! operator must fill in before their extension works at all. The gate that
//! matters is upstream, in `host_state_llm`, which refuses any role the
//! extension did not declare.
use std::collections::HashMap;
use std::str::FromStr;
use std::sync::Arc;
use greentic_aw_runtime::AgentConfig;
use greentic_ext_runtime::host_ports::{
HostCallContext, LlmPort, LlmPortError, LlmPortRequest, LlmPortResponse,
};
/// An extension-runtime LLM port over the worker's own resolved backend.
pub(crate) struct AgentLlmPort {
backend: Arc<dyn greentic_aw_runtime::llm::LlmBackend>,
provider: String,
model: String,
// Named in `LlmPort::complete`'s tracing line below, and by the
// `#[cfg(test)]` accessor the deterministic-tie-break tests use to
// assert WHICH agent won.
agent_id: String,
}
impl AgentLlmPort {
/// Build a port over `backend`, taking provider and model from the agent
/// this runtime serves.
///
/// The agent is chosen deterministically by
/// [`crate::runner::agent_node::first_declared_llm_agent`] — sorted id
/// order, first one declaring a non-empty provider — the same
/// agent-selection rule (`configured_llm_provider` also builds on it, to
/// decide keylessness rather than the per-request provider).
///
/// Only that ONE candidate is ever examined — this does not fall through
/// to a later agent when the first one fails a check below. That is
/// deliberate: it is what keeps this port and the in-process agent
/// backend looking at the same agent's declaration, rather than this
/// port silently picking a second, differently-configured agent behind
/// the backend's back. So an unparseable provider or a missing model on
/// the sorted-first agent yields no port at all, even when a later agent
/// would have qualified.
///
/// Two checks on top of that selection, either of which returns `None`:
/// the declared provider must be one [`greentic_llm::ProviderKind`] can
/// parse, and the declared model must be non-empty (`first_declared_llm_agent`
/// only guarantees a non-empty PROVIDER — an empty model would build
/// exactly the kind of port that first check exists to prevent: one that
/// fails on every call). In every `None` case the caller falls back to
/// the env-keyed port instead of getting one that fails on every call.
pub(crate) fn from_agents(
backend: Arc<dyn greentic_aw_runtime::llm::LlmBackend>,
agents: &HashMap<String, AgentConfig>,
) -> Option<Self> {
let (id, agent) = crate::runner::agent_node::first_declared_llm_agent(agents)?;
let provider = agent.llm.provider.trim();
// Refuse a provider the backend cannot resolve. Building a port for
// one produces an opaque failure on every single call instead of one
// legible line here.
if greentic_llm::ProviderKind::from_str(provider).is_err() {
tracing::warn!(
agent_id = %id,
%provider,
"extension runtime LLM port: agent declares an unknown provider; \
no worker-backed ext LLM port (falling back to the env-keyed port)"
);
return None;
}
let model = agent.llm.model.trim();
if model.is_empty() {
// Same failure mode as an unparseable provider: a port with an
// empty model is exactly the opaque-failure-on-every-call shape
// the provider check above exists to prevent.
tracing::warn!(
agent_id = %id,
%provider,
"extension runtime LLM port: agent declares a provider but no model; \
no worker-backed ext LLM port (falling back to the env-keyed port)"
);
return None;
}
tracing::info!(
agent_id = %id,
%provider,
%model,
"extension runtime LLM port wired (the worker's own agent LLM)"
);
// A graph worker's coordinator plus specialists is the common shape
// with more than one agent, and every extension call on this runtime
// uses THIS ONE agent's provider regardless of what the others
// declare. Warn when another agent disagrees, so an operator can see
// that an extension's completions are not necessarily running on
// every agent's own choice — today nothing else says so.
let other_providers: std::collections::BTreeSet<&str> = agents
.values()
.filter_map(|other| {
let other_provider = other.llm.provider.trim();
(!other_provider.is_empty() && other_provider != provider).then_some(other_provider)
})
.collect();
if !other_providers.is_empty() {
tracing::warn!(
agent_id = %id,
%provider,
other_providers = ?other_providers,
"extension runtime LLM port: other agents in this runtime declare a \
different provider; every host.llm.complete call still uses this \
one agent's provider"
);
}
Some(Self {
backend,
provider: provider.to_string(),
model: model.to_string(),
agent_id: id.clone(),
})
}
#[cfg(test)]
pub(crate) fn provider(&self) -> &str {
&self.provider
}
#[cfg(test)]
pub(crate) fn model(&self) -> &str {
&self.model
}
#[cfg(test)]
pub(crate) fn agent_id(&self) -> &str {
&self.agent_id
}
}
impl LlmPort for AgentLlmPort {
fn complete(
&self,
extension_id: &str,
_ctx: &HostCallContext,
role: &str,
request: LlmPortRequest,
) -> Result<LlmPortResponse, LlmPortError> {
// The single most useful diagnostic this port can offer: which
// agent's declaration decided the provider an extension's
// completion actually ran on.
tracing::debug!(
agent_id = %self.agent_id,
extension_id = %extension_id,
role = %role,
provider = %self.provider,
model = %self.model,
"extension runtime LLM port: dispatching to the worker's own agent LLM"
);
let llm_request = crate::runner::agent_node::port_request_to_llm_request(
request,
&self.provider,
&self.model,
);
crate::runner::agent_node::complete_on_thread(self.backend.clone(), llm_request)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn agent_with(provider: &str, model: &str) -> AgentConfig {
let mut agent = crate::runner::agent_node::test_support::sample_agent_config("a");
agent.llm.provider = provider.into();
agent.llm.model = model.into();
agent
}
/// An [`greentic_aw_runtime::llm::LlmBackend`] whose `complete` is never
/// meant to run — every test here only inspects the resolved provider,
/// model and agent id, never actually calls out to a backend.
struct NeverCalledBackend;
impl greentic_aw_runtime::llm::LlmBackend for NeverCalledBackend {
fn complete<'a>(
&'a self,
_request: greentic_aw_runtime::llm::LlmRequest,
) -> std::pin::Pin<
Box<
dyn std::future::Future<
Output = Result<
greentic_aw_runtime::llm::LlmResponse,
greentic_aw_runtime::error::LlmError,
>,
> + Send
+ 'a,
>,
> {
unreachable!("these tests never drive a completion")
}
}
fn fake_backend() -> Arc<dyn greentic_aw_runtime::llm::LlmBackend> {
Arc::new(NeverCalledBackend)
}
#[test]
fn the_sorted_first_agent_that_declares_a_provider_decides() {
let mut agents = HashMap::new();
agents.insert("zz".to_string(), agent_with("ollama", "llama3"));
agents.insert("aa".to_string(), agent_with("anthropic", "claude-x"));
for _ in 0..50 {
let port = AgentLlmPort::from_agents(fake_backend(), &agents)
.expect("an agent declares a provider");
assert_eq!(port.provider(), "anthropic");
assert_eq!(port.model(), "claude-x");
assert_eq!(port.agent_id(), "aa");
}
}
#[test]
fn an_agent_declaring_no_provider_is_skipped() {
let mut agents = HashMap::new();
agents.insert("aa".to_string(), agent_with("", ""));
agents.insert("bb".to_string(), agent_with("groq", "llama-fast"));
let port =
AgentLlmPort::from_agents(fake_backend(), &agents).expect("bb declares a provider");
assert_eq!(port.provider(), "groq");
assert_eq!(port.agent_id(), "bb");
}
#[test]
fn a_provider_no_provider_kind_knows_builds_no_port() {
let mut agents = HashMap::new();
agents.insert("aa".to_string(), agent_with("not-a-real-provider", "m"));
assert!(
AgentLlmPort::from_agents(fake_backend(), &agents).is_none(),
"a provider the backend cannot parse must not produce a port that \
fails on every call instead"
);
}
#[test]
fn an_agent_declaring_a_provider_but_no_model_builds_no_port() {
let mut agents = HashMap::new();
agents.insert("aa".to_string(), agent_with("anthropic", ""));
assert!(
AgentLlmPort::from_agents(fake_backend(), &agents).is_none(),
"a provider without a model must not produce a port that fails on \
every call instead — first_declared_llm_agent only guarantees a \
non-empty provider, not a non-empty model"
);
}
#[test]
fn only_the_sorted_first_agent_is_ever_examined() {
let mut agents = HashMap::new();
// "aa" sorts first and declares a provider ProviderKind cannot parse;
// "bb" would qualify on its own. The port must not fall through to
// "bb" — that would let this port disagree with whichever agent the
// in-process backend was built for.
agents.insert("aa".to_string(), agent_with("not-a-real-provider", "m"));
agents.insert("bb".to_string(), agent_with("anthropic", "claude-x"));
assert!(
AgentLlmPort::from_agents(fake_backend(), &agents).is_none(),
"a disqualified sorted-first candidate must not fall through to a \
later, otherwise-qualifying agent"
);
}
#[test]
fn no_agents_at_all_builds_no_port() {
assert!(AgentLlmPort::from_agents(fake_backend(), &HashMap::new()).is_none());
}
/// A graph worker's coordinator plus specialists is the common shape
/// with more than one agent declaring a provider. The port still builds
/// over the sorted-first one's declaration — the disagreement is only
/// warned about (see `from_agents`), never refused.
#[test]
fn agents_disagreeing_on_provider_still_builds_over_the_sorted_first_one() {
let mut agents = HashMap::new();
agents.insert("aa".to_string(), agent_with("anthropic", "claude-x"));
agents.insert("bb".to_string(), agent_with("groq", "llama-fast"));
agents.insert("cc".to_string(), agent_with("ollama", "llama3"));
let port = AgentLlmPort::from_agents(fake_backend(), &agents)
.expect("aa sorts first and declares a provider");
assert_eq!(port.provider(), "anthropic");
assert_eq!(port.model(), "claude-x");
assert_eq!(port.agent_id(), "aa");
}
}