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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Temporary client launcher and managed-server CLI arguments.
use std::ffi::OsString;
use clap::{Args, Subcommand};
use crate::clients::ClientKind;
/// Normalize wrapper-owned flags that appear after the client positional.
///
/// Before an explicit `--`, a flag owned by the wrapper is accepted in either
/// position. Everything else is protected behind a clap argument boundary and
/// reaches the client verbatim.
#[must_use]
pub fn protect_client_arguments(arguments: Vec<OsString>, nested: bool) -> Vec<OsString> {
let start = if nested {
arguments
.iter()
.position(|argument| argument == "with")
.map_or(arguments.len(), |position| position + 1)
} else {
1
};
let value_options = [
"--server",
"--token",
"--model",
"--run-ttl-hours",
"--run-max-requests",
];
// Derived from the client table rather than hand-listed: a name missing
// here silently stops protecting that client's arguments, which is what a
// hardcoded copy invites every time a name changes (issue #220).
let clients: Vec<&str> = crate::clients::ClientKind::ALL
.iter()
.flat_map(|kind| [kind.canonical_name(), kind.legacy_name()])
.collect();
let boolean_options = [
"--global",
"--undo",
"--non-interactive",
"--interactive",
"--token-stdin",
"--extend-global-config",
];
let mut position = start;
while position < arguments.len() {
let value = arguments[position].to_string_lossy();
if value_options.contains(&value.as_ref()) {
position += 2;
continue;
}
if value_options
.iter()
.any(|option| value.starts_with(&format!("{option}=")))
{
position += 1;
continue;
}
if clients.contains(&value.as_ref()) {
let client = arguments[position].clone();
let prefix = arguments[..position].to_vec();
// Router options already given *before* the client name are the
// router's. A second occurrence after the client belongs to the
// client: `--model` is defined by five of the seven clients, so
// routing to one model while telling the client another was
// otherwise inexpressible, and the parser reported a duplicate
// router option instead (issue #236).
// A router option may be given once. The first occurrence is the
// router's — whether it came before the client name or after it —
// and any repeat belongs to the client: `--model` is defined by
// five of the seven clients, so routing to one model while telling
// the client another was otherwise inexpressible, and the parser
// reported a duplicate router option instead (issue #236).
let mut consumed: Vec<String> = prefix
.iter()
.map(|item| {
let item = item.to_string_lossy();
item.split_once('=')
.map_or_else(|| item.to_string(), |(name, _)| name.to_string())
})
.collect();
let mut wrapper = Vec::new();
let mut forwarded = Vec::new();
let mut cursor = position + 1;
let mut explicit_boundary = false;
while cursor < arguments.len() {
let item = arguments[cursor].to_string_lossy();
if explicit_boundary {
forwarded.push(arguments[cursor].clone());
cursor += 1;
continue;
}
if item == "--" {
explicit_boundary = true;
cursor += 1;
continue;
}
if boolean_options.contains(&item.as_ref()) {
if consumed.iter().any(|seen| seen == item.as_ref()) {
forwarded.push(arguments[cursor].clone());
} else {
consumed.push(item.to_string());
wrapper.push(arguments[cursor].clone());
}
cursor += 1;
continue;
}
if value_options.contains(&item.as_ref()) {
let repeated = consumed.iter().any(|seen| seen == item.as_ref());
if !repeated {
consumed.push(item.to_string());
}
let target = if repeated {
&mut forwarded
} else {
&mut wrapper
};
target.push(arguments[cursor].clone());
if let Some(value) = arguments.get(cursor + 1) {
target.push(value.clone());
cursor += 2;
} else {
cursor += 1;
}
continue;
}
if value_options
.iter()
.any(|option| item.starts_with(&format!("{option}=")))
{
wrapper.push(arguments[cursor].clone());
cursor += 1;
continue;
}
forwarded.push(arguments[cursor].clone());
cursor += 1;
}
let mut normalized = prefix;
normalized.extend(wrapper);
normalized.push(client);
if !forwarded.is_empty() {
normalized.push("--".into());
normalized.extend(forwarded);
}
return normalized;
}
position += 1;
}
arguments
}
/// Options shared by `router with` and the standalone `with-router` binary.
#[derive(Clone, Debug, Args)]
#[command(trailing_var_arg = true)]
pub struct WithArgs {
/// Permanently configure the client instead of launching it temporarily.
#[arg(long)]
pub global: bool,
/// Restore the exact configuration saved by a previous `--global` call.
#[arg(long, requires = "global")]
pub undo: bool,
/// Force the client's non-interactive/one-shot mode.
#[arg(long, conflicts_with = "interactive")]
pub non_interactive: bool,
/// Force the client's interactive mode.
#[arg(long, conflicts_with = "non_interactive")]
pub interactive: bool,
/// Keep the user's own configuration and add only the router's connection
/// settings, so sessions and settings stay visible.
///
/// The default gives the client a configuration directory of its own, which
/// is right for CI and one-off runs but makes a session started outside the
/// router impossible to resume: `/resume` lists nothing, because the user's
/// `projects/` and `settings.json` are not on the path (issue #233).
#[arg(long)]
pub extend_global_config: bool,
/// Router origin. No local server is started when this is supplied.
#[arg(long)]
pub server: Option<String>,
/// Router token. Prefer the environment or `--token-stdin` to shell history.
#[arg(long, hide_env_values = true, conflicts_with = "token_stdin")]
pub token: Option<String>,
/// Read the router token as one line from standard input.
#[arg(long, conflicts_with = "token")]
pub token_stdin: bool,
/// Model passed to the client instead of the integration default.
#[arg(long)]
pub model: Option<String>,
/// Lifetime of an automatically minted per-run token.
#[arg(long, default_value_t = 1)]
pub run_ttl_hours: i64,
/// Optional request budget for an automatically minted per-run token.
#[arg(long)]
pub run_max_requests: Option<u64>,
/// Client integration to launch or configure.
#[arg(value_enum)]
pub client: ClientKind,
/// Arguments forwarded to the client. Use `--` to make the boundary explicit.
#[arg(value_name = "CLIENT_ARGS", allow_hyphen_values = true)]
pub client_args: Vec<OsString>,
}
/// Persistent and managed-local server operations.
#[derive(Debug, Subcommand)]
pub enum ServerOp {
/// Persist a remote router URL and optional token, or clear that selection.
Use {
/// Remote router origin to persist.
server: Option<String>,
/// Token to persist with owner-only permissions.
#[arg(long, hide_env_values = true, conflicts_with = "token_stdin")]
token: Option<String>,
/// Read the token as one line from standard input.
#[arg(long, conflicts_with = "token")]
token_stdin: bool,
/// Clear the persisted remote selection and return to automatic local mode.
#[arg(long)]
clear: bool,
/// Default request budget for tokens minted for wrapper runs.
#[arg(long)]
run_max_requests: Option<u64>,
},
/// Show the selected server source and managed-container lifecycle.
Status,
/// Start the shared managed local container.
Start,
/// Reveal and claim the managed router's bootstrap administrator credential.
Claim,
/// Stop the shared managed local container without deleting its state.
Stop,
/// Remove the managed container and volume, destroying saved credentials.
Remove {
/// Confirm destructive removal without an interactive prompt.
#[arg(long)]
yes: bool,
},
/// Reap a crashed wrapper's managed-server reference.
#[command(hide = true)]
Reap { pid: u32 },
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn wrapper_flags_after_client_are_protected() {
for option in [
"--global",
"--undo",
"--non-interactive",
"--interactive",
"--token-stdin",
] {
let arguments = ["router", "with", "codex", option, "prompt"]
.into_iter()
.map(OsString::from)
.collect();
assert_eq!(
protect_client_arguments(arguments, true),
["router", "with", option, "codex", "--", "prompt"].map(OsString::from),
"{option} after the client must remain wrapper-owned"
);
}
}
#[test]
fn value_wrapper_flags_after_client_are_accepted() {
for (option, value) in [
("--server", "https://router.test"),
("--token", "test-token"),
("--model", "gpt-test"),
("--run-ttl-hours", "2"),
("--run-max-requests", "3"),
] {
let arguments = ["with-router", "codex", option, value, "hi"]
.into_iter()
.map(OsString::from)
.collect();
assert_eq!(
protect_client_arguments(arguments, false),
["with-router", option, value, "codex", "--", "hi"].map(OsString::from),
"{option} VALUE after the client must remain wrapper-owned"
);
let equals = format!("{option}={value}");
let arguments = ["with-router", "codex", &equals, "hi"]
.into_iter()
.map(OsString::from)
.collect();
assert_eq!(
protect_client_arguments(arguments, false),
[
OsString::from("with-router"),
OsString::from(&equals),
OsString::from("codex"),
OsString::from("--"),
OsString::from("hi"),
],
"{option}=VALUE after the client must remain wrapper-owned"
);
}
}
#[test]
fn explicit_boundary_forwards_every_colliding_wrapper_flag_verbatim() {
for option in [
"--global",
"--undo",
"--non-interactive",
"--interactive",
"--token-stdin",
"--server",
"--token",
"--model",
"--run-ttl-hours",
"--run-max-requests",
] {
let arguments = ["with-router", "codex", "--", option, "client-value"]
.into_iter()
.map(OsString::from)
.collect::<Vec<_>>();
assert_eq!(
protect_client_arguments(arguments.clone(), false),
arguments,
"{option} after -- must be forwarded to the client"
);
}
}
#[test]
fn option_values_that_match_clients_are_not_boundaries() {
let arguments = ["with-router", "--model", "codex", "qwen", "hello"]
.into_iter()
.map(OsString::from)
.collect();
assert_eq!(
protect_client_arguments(arguments, false),
["with-router", "--model", "codex", "qwen", "--", "hello"].map(OsString::from)
);
}
}
#[cfg(test)]
mod collision_tests {
use super::protect_client_arguments;
use std::ffi::OsString;
fn split(arguments: &[&str]) -> Vec<String> {
protect_client_arguments(arguments.iter().map(OsString::from).collect(), false)
.iter()
.map(|value| value.to_string_lossy().into_owned())
.collect()
}
/// A router option may be given once; a repeat belongs to the client.
/// `--model` is defined by five of the seven clients, so routing to one
/// model while telling the client another was inexpressible — the parser
/// rejected the second occurrence as a duplicate router option (#236).
#[test]
fn a_repeated_router_option_is_forwarded_to_the_client() {
let split = split(&["with-router", "qwen", "--model", "A", "--model", "B"]);
let separator = split
.iter()
.position(|value| value == "--")
.expect("an explicit boundary is inserted");
let router = &split[..separator];
let client = &split[separator + 1..];
assert!(
router.windows(2).any(|pair| pair == ["--model", "A"]),
"the router keeps the first occurrence: {router:?}"
);
assert!(
client.windows(2).any(|pair| pair == ["--model", "B"]),
"the client receives the repeat: {client:?}"
);
}
/// The same holds for a boolean router option.
#[test]
fn a_repeated_boolean_option_is_forwarded() {
let split = split(&[
"with-router",
"qwen",
"--non-interactive",
"--non-interactive",
]);
let separator = split.iter().position(|value| value == "--").expect("--");
assert_eq!(
split[..separator]
.iter()
.filter(|value| *value == "--non-interactive")
.count(),
1,
"the router consumes exactly one"
);
assert_eq!(
split[separator + 1..]
.iter()
.filter(|value| *value == "--non-interactive")
.count(),
1,
"the repeat is forwarded"
);
}
/// A single occurrence still belongs to the router, so this is not a
/// behaviour change for ordinary use.
#[test]
fn a_single_router_option_is_still_consumed_by_the_router() {
let split = split(&["with-router", "qwen", "--model", "A", "--prompt", "hi"]);
let separator = split.iter().position(|value| value == "--").expect("--");
assert!(split[..separator].windows(2).any(|p| p == ["--model", "A"]));
assert!(
split[separator + 1..]
.windows(2)
.any(|p| p == ["--prompt", "hi"]),
"client options still forward: {split:?}"
);
}
/// Options before the client name are the router's, and a matching name
/// after it then goes to the client.
#[test]
fn an_option_before_the_client_claims_the_router_slot() {
let split = split(&["with-router", "--model", "A", "qwen", "--model", "B"]);
let separator = split.iter().position(|value| value == "--").expect("--");
assert!(split[..separator].windows(2).any(|p| p == ["--model", "A"]));
assert!(
split[separator + 1..]
.windows(2)
.any(|p| p == ["--model", "B"]),
"{split:?}"
);
}
}