Skip to content

Commit 5dc451f

Browse files
committed
Return trusted output and split model vs script errors
Replace `ToolOutput::untrusted` with `ToolOutput::trusted` in `shell.rs` and branch on `call.caller()` in `pack.rs`. `Caller::Model` receives domain and argument errors as plain trusted text while `Caller::Script` receives `Err(ToolError)`.
1 parent 04e0c86 commit 5dc451f

8 files changed

Lines changed: 144 additions & 44 deletions

File tree

‎crates/plugin-bashkit/src/files-tests.rs‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ use std::task::{Context, Poll, Waker};
1111
use bashkit::FileSystem;
1212
use runvil_plugin_sdk::sys::RV_MAX_WRITE;
1313
use runvil_plugin_sdk::testing::Files;
14-
use runvil_plugin_sdk::{FileError, FileErrorKind, FileType, Stat, ToolError, ToolErrorKind};
14+
use runvil_plugin_sdk::{
15+
Caller, FileError, FileErrorKind, FileType, Stat, ToolError, ToolErrorKind,
16+
};
1517
use serde_json::json;
1618

1719
use super::{
1820
FIXED_MODE, FileRequest, MAX_FILE_BYTES, NO_DEVICE, RunFiles, host_path, io_error, metadata,
1921
};
20-
use crate::fixture::{answer, loaded, over, run, shell, stepped};
22+
use crate::fixture::{answer, loaded, over, run, shell, shell_as, stepped};
2123

2224
/// What the Host answers for `/secret`, a path the run's policy denies.
2325
fn denied() -> FileError {
@@ -418,7 +420,7 @@ fn sed_in_place_fails_its_own_command_and_tee_ends_the_call_when_a_write_fails()
418420
"echo before; echo hi | tee -a /secret; echo after",
419421
] {
420422
assert_eq!(
421-
shell(&pack, &json!({ "command": command })),
423+
shell_as(&pack, Caller::Script, &json!({ "command": command })),
422424
Err(ToolError::new(
423425
ToolErrorKind::Backend,
424426
"Shell: the shell did not finish the command: io error: the mode allows only \

‎crates/plugin-bashkit/src/fixture.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//! SDK's fake Host, and a `Shell` call on one run's instance of the Pack.
33
44
use runvil_plugin_sdk::testing::{self, Call, Files, Loaded, PackRun};
5-
use runvil_plugin_sdk::{ToolError, ToolOutput};
5+
use runvil_plugin_sdk::{Caller, ToolError, ToolOutput};
66
use serde_json::Value;
77

88
use crate::Bashkit;
@@ -30,6 +30,19 @@ pub(crate) fn shell(pack: &PackRun<'_>, args: &Value) -> Result<ToolOutput, Tool
3030
.expect("the call follows the contract")
3131
}
3232

33+
/// Runs one `Shell` call with `args` on `pack` as `caller` to its end,
34+
/// as the Harness does.
35+
pub(crate) fn shell_as(
36+
pack: &PackRun<'_>,
37+
caller: Caller,
38+
args: &Value,
39+
) -> Result<ToolOutput, ToolError> {
40+
pack.start("shell", &args.to_string())
41+
.with_caller(caller)
42+
.run()
43+
.expect("the call follows the contract")
44+
}
45+
3346
/// Steps `call`, which a test started, to its end, as the Harness does:
3447
/// it sends a step only when the call is due one. So it waits with
3548
/// [`Call::wait`] before each step. That wait is over at once for a call
@@ -59,10 +72,7 @@ pub(crate) fn finish(call: &mut Call<'_>) -> ToolOutput {
5972
pub(crate) fn run(pack: &PackRun<'_>, command: &str) -> String {
6073
let output =
6174
shell(pack, &serde_json::json!({ "command": command })).expect("the command finishes");
62-
assert!(
63-
!output.is_trusted(),
64-
"a command's output may hold text from outside, so it is untrusted"
65-
);
75+
assert!(output.is_trusted(), "a command's output is trusted");
6676
output.text().to_owned()
6777
}
6878

‎crates/plugin-bashkit/src/lib.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
//!
3535
//! A call runs its `command` in a shell of its own and answers the exit
3636
//! code and then standard output followed by standard error, in one
37-
//! fenced block, as untrusted output. A command that exits with a failure
37+
//! fenced block, as trusted output. A command that exits with a failure
3838
//! is an output too. Output past 20,000 characters keeps its first and
3939
//! its last 10,000 around a line that counts what is left out.
4040
//!
@@ -190,7 +190,10 @@
190190
//! definition.
191191
//! - A call whose arguments do not match the parameters is an
192192
//! [`InvalidArguments`](runvil_plugin_sdk::ToolErrorKind::InvalidArguments)
193-
//! failure, and a key the schema does not declare is refused by name.
193+
//! failure on [`Caller::Script`](runvil_plugin_sdk::Caller::Script), and
194+
//! a key the schema does not declare is refused by name. On
195+
//! [`Caller::Model`](runvil_plugin_sdk::Caller::Model), any tool failure
196+
//! is returned as trusted output holding the error message.
194197
195198
mod files;
196199
#[cfg(test)]

‎crates/plugin-bashkit/src/pack.rs‎

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
use std::sync::Arc;
55

66
use runvil_plugin_sdk::{
7-
BoxFuture, Pack, PackInfo, ToolCall, ToolError, ToolErrorKind, ToolInfo, ToolOutput,
7+
BoxFuture, Caller, Pack, PackInfo, ToolCall, ToolError, ToolErrorKind, ToolInfo, ToolOutput,
88
};
99
use tokio::runtime::Handle;
1010

@@ -55,19 +55,29 @@ impl Pack for BashPack {
5555

5656
fn call<'a>(&'a self, call: ToolCall<'a>) -> BoxFuture<'a, Result<ToolOutput, ToolError>> {
5757
Box::pin(async move {
58-
if call.tool() != SHELL {
59-
return Err(ToolError::new(
58+
let caller = call.caller();
59+
let result = if call.tool() == SHELL {
60+
crate::shell::call(&self.timers, call).await
61+
} else {
62+
Err(ToolError::new(
6063
ToolErrorKind::Other,
6164
format!("bash has no tool named {}", call.tool()),
62-
));
65+
))
66+
};
67+
match result {
68+
Err(error) if caller == Caller::Model => {
69+
Ok(ToolOutput::trusted(error.message().to_owned()))
70+
}
71+
other => other,
6372
}
64-
crate::shell::call(&self.timers, call).await
6573
})
6674
}
6775
}
6876

6977
#[cfg(test)]
7078
mod tests {
79+
use runvil_plugin_sdk::{Caller, ToolErrorKind, ToolOutput};
80+
7181
use crate::fixture::{PACK, loaded};
7282

7383
#[test]
@@ -110,4 +120,27 @@ mod tests {
110120
let second = loaded.new_pack(PACK).expect("the Pack instance is made");
111121
assert_eq!(first.tools(), second.tools());
112122
}
123+
124+
#[test]
125+
fn a_failing_call_is_an_error_for_a_script_and_trusted_output_for_a_model() {
126+
let loaded = loaded();
127+
let pack = loaded.new_pack(PACK).expect("the Pack instance is made");
128+
let script_result = pack
129+
.start("shell", "{}")
130+
.with_caller(Caller::Script)
131+
.run()
132+
.expect("the call follows the contract");
133+
let error = script_result.expect_err("a script call receives a ToolError");
134+
assert_eq!(error.kind(), ToolErrorKind::InvalidArguments);
135+
136+
let model_result = pack
137+
.start("shell", "{}")
138+
.with_caller(Caller::Model)
139+
.run()
140+
.expect("the call follows the contract");
141+
assert_eq!(
142+
model_result,
143+
Ok(ToolOutput::trusted(error.message().to_owned()))
144+
);
145+
}
113146
}

‎crates/plugin-bashkit/src/session-tests.rs‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::thread;
77
use std::time::{Duration, Instant};
88

99
use runvil_plugin_sdk::testing::Files;
10-
use runvil_plugin_sdk::{ToolError, ToolErrorKind, ToolOutput};
10+
use runvil_plugin_sdk::{Caller, ToolError, ToolErrorKind, ToolOutput};
1111
use serde_json::json;
1212

1313
use super::POLL_CAP;
@@ -29,7 +29,7 @@ fn files() -> Files {
2929
/// The output of a call whose command exited with `code` after writing
3030
/// `output`.
3131
fn exited(code: i32, output: &str) -> ToolOutput {
32-
ToolOutput::untrusted(answer(code, output))
32+
ToolOutput::trusted(answer(code, output))
3333
}
3434

3535
/// The failure of a call whose command was stopped at a time limit of
@@ -150,14 +150,16 @@ fn a_script_that_ended_waits_for_its_reads_in_progress() {
150150
// Three jobs ask for a file each, and the script sleeps past its time
151151
// limit. bashkit stops the script at the limit and keeps its jobs,
152152
// which still wait for their reads.
153-
let mut call = pack.start(
154-
"shell",
155-
&json!({
156-
"command": "cat /notes/a.md & cat /notes/b.md & cat /notes/c.md & sleep 20",
157-
"block_until_ms": 50,
158-
})
159-
.to_string(),
160-
);
153+
let mut call = pack
154+
.start(
155+
"shell",
156+
&json!({
157+
"command": "cat /notes/a.md & cat /notes/b.md & cat /notes/c.md & sleep 20",
158+
"block_until_ms": 50,
159+
})
160+
.to_string(),
161+
)
162+
.with_caller(Caller::Script);
161163
assert_eq!(call.step(), Ok(None), "the Host has the first read");
162164
// The limit passes here. The next step brings the first answer and
163165
// stops the script, with the two other reads in progress.
@@ -243,15 +245,17 @@ fn a_script_that_ended_waits_for_its_changes_in_progress() {
243245
// Three jobs ask for a write each, and the script sleeps past its
244246
// time limit. bashkit stops the script at the limit and keeps its
245247
// jobs, which still wait for their writes.
246-
let mut call = pack.start(
247-
"shell",
248-
&json!({
249-
"command": "echo x > /notes/x.md & echo y > /notes/y.md & echo z > /notes/z.md & \
250-
sleep 20",
251-
"block_until_ms": 50,
252-
})
253-
.to_string(),
254-
);
248+
let mut call = pack
249+
.start(
250+
"shell",
251+
&json!({
252+
"command": "echo x > /notes/x.md & echo y > /notes/y.md & echo z > /notes/z.md & \
253+
sleep 20",
254+
"block_until_ms": 50,
255+
})
256+
.to_string(),
257+
)
258+
.with_caller(Caller::Script);
255259
assert_eq!(call.step(), Ok(None), "the Host has the first write");
256260
// The limit passes here. The next step brings the first answer and
257261
// stops the script, with the two other writes in progress.

‎crates/plugin-bashkit/src/shell-tests.rs‎

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
//! answers, what a call leaves for the next, the time limit, the output
33
//! cap, and the arguments a call may pass.
44
5-
use runvil_plugin_sdk::ToolErrorKind;
5+
use runvil_plugin_sdk::{Caller, ToolErrorKind};
66
use serde_json::json;
77

88
use super::{OUTPUT_CAP, middle_cut, quoted};
9-
use crate::fixture::{PACK, answer, loaded, run, shell};
9+
use crate::fixture::{PACK, answer, loaded, run, shell, shell_as};
1010

1111
#[test]
1212
fn a_command_answers_its_exit_code_and_its_output() {
@@ -84,8 +84,9 @@ fn a_variable_or_a_function_from_one_call_is_gone_in_the_next() {
8484
fn a_command_past_its_time_limit_is_stopped_and_the_call_fails_with_the_limit() {
8585
let loaded = loaded();
8686
let pack = loaded.new_pack(PACK).expect("the Pack instance is made");
87-
let stopped = shell(
87+
let stopped = shell_as(
8888
&pack,
89+
Caller::Script,
8990
&json!({ "command": "sleep 20", "block_until_ms": 50 }),
9091
)
9192
.expect_err("the command runs past its limit");
@@ -113,8 +114,12 @@ fn a_time_limit_over_the_ceiling_is_lowered_to_it() {
113114
fn a_command_that_does_not_parse_fails_with_the_shells_reason() {
114115
let loaded = loaded();
115116
let pack = loaded.new_pack(PACK).expect("the Pack instance is made");
116-
let error = shell(&pack, &json!({ "command": "if true; then" }))
117-
.expect_err("the command does not parse");
117+
let error = shell_as(
118+
&pack,
119+
Caller::Script,
120+
&json!({ "command": "if true; then" }),
121+
)
122+
.expect_err("the command does not parse");
118123
assert_eq!(error.kind(), ToolErrorKind::Backend);
119124
assert_eq!(
120125
error.message(),
@@ -138,8 +143,12 @@ fn a_command_the_shell_does_not_have_is_an_output_that_says_so() {
138143
fn a_command_that_loops_without_end_is_stopped_by_the_shells_limits() {
139144
let loaded = loaded();
140145
let pack = loaded.new_pack(PACK).expect("the Pack instance is made");
141-
let error = shell(&pack, &json!({ "command": "while true; do :; done" }))
142-
.expect_err("the loop passes a limit");
146+
let error = shell_as(
147+
&pack,
148+
Caller::Script,
149+
&json!({ "command": "while true; do :; done" }),
150+
)
151+
.expect_err("the loop passes a limit");
143152
assert_eq!(error.kind(), ToolErrorKind::Backend);
144153
assert_eq!(
145154
error.message(),
@@ -224,7 +233,7 @@ fn arguments_that_do_not_match_the_parameters_are_refused() {
224233
"invalid value",
225234
),
226235
] {
227-
let error = shell(&pack, &args).expect_err("the arguments are refused");
236+
let error = shell_as(&pack, Caller::Script, &args).expect_err("the arguments are refused");
228237
assert_eq!(error.kind(), ToolErrorKind::InvalidArguments, "{args}");
229238
assert!(
230239
error.message().starts_with("Shell: ") && error.message().contains(part),
@@ -234,6 +243,44 @@ fn arguments_that_do_not_match_the_parameters_are_refused() {
234243
}
235244
}
236245

246+
#[test]
247+
fn model_calls_return_invalid_arguments_and_backend_errors_as_trusted_output() {
248+
let loaded = loaded();
249+
let pack = loaded.new_pack(PACK).expect("the Pack instance is made");
250+
251+
let invalid = shell(&pack, &json!({ "command": "true", "block_until_ms": 0 }))
252+
.expect("a model call receives invalid-arguments errors as output");
253+
assert!(invalid.is_trusted());
254+
assert!(
255+
invalid.text().starts_with("Shell: ")
256+
&& invalid
257+
.text()
258+
.contains("block_until_ms is 0, and it must be at least 1"),
259+
"{}",
260+
invalid.text()
261+
);
262+
263+
let timed_out = shell(
264+
&pack,
265+
&json!({ "command": "sleep 20", "block_until_ms": 50 }),
266+
)
267+
.expect("a model call receives timeout errors as output");
268+
assert!(timed_out.is_trusted());
269+
assert_eq!(
270+
timed_out.text(),
271+
"Shell: the command was stopped at its time limit of 50 ms, before it finished."
272+
);
273+
274+
let parse_error = shell(&pack, &json!({ "command": "if true; then" }))
275+
.expect("a model call receives backend errors as output");
276+
assert!(parse_error.is_trusted());
277+
assert_eq!(
278+
parse_error.text(),
279+
"Shell: the shell did not finish the command: parse error at line 2, column 1: syntax \
280+
error: unexpected end of file"
281+
);
282+
}
283+
237284
#[test]
238285
fn a_description_and_null_for_an_optional_parameter_are_accepted() {
239286
let loaded = loaded();

‎crates/plugin-bashkit/src/shell.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ impl Invocation {
8080
let moved = shell.run(call, &change, self.limit).await;
8181
let moved = moved.map_err(|error| self.failure(&error))?;
8282
if moved.exit_code != 0 {
83-
return Ok(ToolOutput::untrusted(render(&moved)));
83+
return Ok(ToolOutput::trusted(render(&moved)));
8484
}
8585
}
8686
let ran = shell.run(call, &self.command, self.limit).await;
8787
let ran = ran.map_err(|error| self.failure(&error))?;
88-
Ok(ToolOutput::untrusted(render(&ran)))
88+
Ok(ToolOutput::trusted(render(&ran)))
8989
}
9090

9191
/// The failure of a call whose command bashkit ended with `error`.

‎crates/plugin-bashkit/tests/it/main.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ fn a_host_that_links_the_crate_loads_the_plugin_and_runs_a_command() {
1717
.call("shell", &json!({ "command": "echo linked" }).to_string())
1818
.expect("the call follows the contract")
1919
.expect("the command finishes");
20+
assert!(output.is_trusted());
2021
assert_eq!(
2122
output.text(),
2223
"Exit code: 0\n\nCommand output:\n\n```\nlinked\n```"

0 commit comments

Comments
 (0)