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
// Parallel tool calls capability
//
// Controls whether the agent asks the provider to emit multiple tool calls per
// turn (and whether the local tool scheduler runs a batch concurrently):
//
// - `prefer`: explicitly request parallel tool calls. On providers that expose
// a wire control (OpenAI/Anthropic families) this is sent on the request; the
// local scheduler keeps its class-aware concurrent default. Lets the model
// batch independent reads/searches instead of relying on each provider's
// undocumented default.
// - `avoid`: ask the provider to emit at most one tool call per turn AND force
// the local tool scheduler to serialize the batch. The local serialization
// applies to every driver, so `avoid` is honored even on providers without a
// wire control (Gemini/Bedrock).
// - `none`: no preference — omit the field and keep the provider default and the
// scheduler's concurrent schedule. Same effect as not enabling the capability;
// useful to neutralize an inherited preference from a parent harness.
//
// The resolved preference threads through `RuntimeAgent.parallel_tool_calls`
// into both the LLM request (provider-gated, see `ChatDriver::
// supports_parallel_tool_calls`) and `ActInput.parallel_tool_calls` (the local
// scheduler). An explicit `parallel_tool_calls` field on harness/agent/session
// is a lower-level escape hatch and takes precedence over this capability.
use super::{Capability, CapabilityLocalization, SystemPromptContext};
use async_trait::async_trait;
/// Capability ID for the request-level parallel tool calls preference.
pub const PARALLEL_TOOL_CALLS_CAPABILITY_ID: &str = "parallel_tool_calls";
/// Resolved preference mode for the `parallel_tool_calls` capability.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParallelToolCallsMode {
/// Request parallel tool calls (provider + concurrent scheduler).
Prefer,
/// Disable parallel tool calls (provider hint + serialized scheduler).
Avoid,
/// No preference — provider default and concurrent scheduler.
None,
}
impl ParallelToolCallsMode {
/// Parse a config string into a mode. Unknown values yield `None`.
pub fn parse(value: &str) -> Option<Self> {
match value {
"prefer" => Some(Self::Prefer),
"avoid" => Some(Self::Avoid),
"none" => Some(Self::None),
_ => None,
}
}
/// Map the mode onto the request-level `parallel_tool_calls` preference
/// carried by `RuntimeAgent`/`LlmCallConfig`/`ActInput`.
///
/// `None` mode resolves to `None` (omit, provider default).
pub fn to_preference(self) -> Option<bool> {
match self {
Self::Prefer => Some(true),
Self::Avoid => Some(false),
Self::None => None,
}
}
}
/// Resolve the `parallel_tool_calls` capability config into a request-level
/// preference.
///
/// - Capability present with no explicit `mode` (empty/`null` config) → `prefer`.
/// - Valid `mode` → that mode (`none` resolves to no preference).
/// - Malformed config (not an object) or an invalid/non-string `mode` → `None`,
/// so a bad runtime config neutralizes the capability rather than silently
/// enabling parallel tool calls. (`validate_config` already rejects these on
/// the write path; this is the defensive runtime fallback.)
pub fn parallel_tool_calls_from_config(config: &serde_json::Value) -> Option<bool> {
if config.is_null() {
return ParallelToolCallsMode::Prefer.to_preference();
}
let object = config.as_object()?;
match object.get("mode") {
None => ParallelToolCallsMode::Prefer.to_preference(),
Some(serde_json::Value::String(mode)) => {
ParallelToolCallsMode::parse(mode).and_then(ParallelToolCallsMode::to_preference)
}
Some(_) => None,
}
}
/// Parallel tool calls capability.
///
/// Adds no tools or prompt text; it only configures the outbound LLM request
/// and the local tool scheduler.
pub struct ParallelToolCallsCapability;
#[async_trait]
impl Capability for ParallelToolCallsCapability {
fn id(&self) -> &str {
PARALLEL_TOOL_CALLS_CAPABILITY_ID
}
fn name(&self) -> &str {
"Parallel Tool Calls"
}
fn description(&self) -> &str {
"Controls whether the agent requests multiple tool calls per turn and \
runs them concurrently: prefer (request parallel), avoid (one at a \
time, serialized), or none (provider default)."
}
fn category(&self) -> Option<&str> {
Some("Optimization")
}
fn config_schema(&self) -> Option<serde_json::Value> {
Some(serde_json::json!({
"type": "object",
"properties": {
"mode": {
"type": "string",
"title": "Parallel tool calls",
"description": "prefer: request parallel tool calls (default); avoid: one tool call per turn, serialized locally; none: provider default.",
"enum": ["prefer", "avoid", "none"],
"default": "prefer"
}
}
}))
}
fn validate_config(&self, config: &serde_json::Value) -> Result<(), String> {
if config.is_null() {
return Ok(());
}
if !config.is_object() {
return Err("parallel_tool_calls config must be an object".to_string());
}
match config.get("mode") {
None => Ok(()),
Some(serde_json::Value::String(mode))
if ParallelToolCallsMode::parse(mode).is_some() =>
{
Ok(())
}
Some(value) => Err(format!(
"mode must be one of \"prefer\", \"avoid\", \"none\", got {value}"
)),
}
}
fn localizations(&self) -> Vec<CapabilityLocalization> {
vec![
CapabilityLocalization {
locale: "en",
name: None,
description: None,
config_description: Some(
"Chooses whether the agent batches independent tool calls or runs them one at a time.",
),
config_overlay: None,
},
CapabilityLocalization {
locale: "uk",
name: Some("Паралельні виклики інструментів"),
description: Some(
"Визначає, чи запитує агент кілька викликів інструментів за хід і чи виконує їх одночасно: prefer (запитувати паралельні), avoid (по одному, послідовно) або none (типова поведінка провайдера).",
),
config_description: Some(
"Обирає, чи агент об'єднує незалежні виклики інструментів, чи виконує їх по одному.",
),
config_overlay: Some(serde_json::json!({
"properties": {
"mode": {
"title": "Паралельні виклики інструментів",
"description": "prefer: запитувати паралельні виклики (типово); avoid: один виклик за хід, послідовно; none: типова поведінка провайдера."
}
}
})),
},
]
}
async fn system_prompt_contribution(&self, _ctx: &SystemPromptContext) -> Option<String> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_known_modes() {
assert_eq!(
ParallelToolCallsMode::parse("prefer"),
Some(ParallelToolCallsMode::Prefer)
);
assert_eq!(
ParallelToolCallsMode::parse("avoid"),
Some(ParallelToolCallsMode::Avoid)
);
assert_eq!(
ParallelToolCallsMode::parse("none"),
Some(ParallelToolCallsMode::None)
);
assert_eq!(ParallelToolCallsMode::parse("loud"), None);
}
#[test]
fn mode_to_preference() {
assert_eq!(ParallelToolCallsMode::Prefer.to_preference(), Some(true));
assert_eq!(ParallelToolCallsMode::Avoid.to_preference(), Some(false));
assert_eq!(ParallelToolCallsMode::None.to_preference(), None);
}
#[test]
fn from_config_defaults_to_prefer() {
// Capability present without explicit mode => prefer.
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({})),
Some(true)
);
assert_eq!(
parallel_tool_calls_from_config(&serde_json::Value::Null),
Some(true)
);
}
#[test]
fn from_config_honors_mode() {
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({"mode": "prefer"})),
Some(true)
);
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({"mode": "avoid"})),
Some(false)
);
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({"mode": "none"})),
None
);
}
#[test]
fn from_config_malformed_neutralizes() {
// Invalid/non-string mode and non-object configs do not silently enable
// parallel tool calls — they resolve to no preference.
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({"mode": "loud"})),
None
);
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!({"mode": 5})),
None
);
assert_eq!(
parallel_tool_calls_from_config(&serde_json::json!([])),
None
);
}
#[test]
fn validate_config_accepts_known_modes_only() {
let cap = ParallelToolCallsCapability;
assert!(cap.validate_config(&serde_json::Value::Null).is_ok());
assert!(cap.validate_config(&serde_json::json!({})).is_ok());
assert!(
cap.validate_config(&serde_json::json!({"mode": "prefer"}))
.is_ok()
);
assert!(
cap.validate_config(&serde_json::json!({"mode": "avoid"}))
.is_ok()
);
assert!(
cap.validate_config(&serde_json::json!({"mode": "loud"}))
.is_err()
);
assert!(cap.validate_config(&serde_json::json!([])).is_err());
}
#[test]
fn localizations_resolve_uk() {
let cap = ParallelToolCallsCapability;
assert_eq!(
cap.localized_name(Some("uk-UA")),
"Паралельні виклики інструментів"
);
}
}