diff --git a/crates/plugin-bashkit/src/files-tests.rs b/crates/plugin-bashkit/src/files-tests.rs index 248192b..e36e146 100644 --- a/crates/plugin-bashkit/src/files-tests.rs +++ b/crates/plugin-bashkit/src/files-tests.rs @@ -11,13 +11,15 @@ use std::task::{Context, Poll, Waker}; use bashkit::FileSystem; use runvil_plugin_sdk::sys::RV_MAX_WRITE; use runvil_plugin_sdk::testing::Files; -use runvil_plugin_sdk::{FileError, FileErrorKind, FileType, Stat, ToolError, ToolErrorKind}; +use runvil_plugin_sdk::{ + Caller, FileError, FileErrorKind, FileType, Stat, ToolError, ToolErrorKind, +}; use serde_json::json; use super::{ FIXED_MODE, FileRequest, MAX_FILE_BYTES, NO_DEVICE, RunFiles, host_path, io_error, metadata, }; -use crate::fixture::{answer, loaded, over, run, shell, stepped}; +use crate::fixture::{answer, loaded, over, run, shell, shell_as, stepped}; /// What the Host answers for `/secret`, a path the run's policy denies. fn denied() -> FileError { @@ -418,7 +420,7 @@ fn sed_in_place_fails_its_own_command_and_tee_ends_the_call_when_a_write_fails() "echo before; echo hi | tee -a /secret; echo after", ] { assert_eq!( - shell(&pack, &json!({ "command": command })), + shell_as(&pack, Caller::Script, &json!({ "command": command })), Err(ToolError::new( ToolErrorKind::Backend, "Shell: the shell did not finish the command: io error: the mode allows only \ diff --git a/crates/plugin-bashkit/src/fixture.rs b/crates/plugin-bashkit/src/fixture.rs index 620b4af..662c21c 100644 --- a/crates/plugin-bashkit/src/fixture.rs +++ b/crates/plugin-bashkit/src/fixture.rs @@ -2,7 +2,7 @@ //! SDK's fake Host, and a `Shell` call on one run's instance of the Pack. use runvil_plugin_sdk::testing::{self, Call, Files, Loaded, PackRun}; -use runvil_plugin_sdk::{ToolError, ToolOutput}; +use runvil_plugin_sdk::{Caller, ToolError, ToolOutput}; use serde_json::Value; use crate::Bashkit; @@ -30,6 +30,19 @@ pub(crate) fn shell(pack: &PackRun<'_>, args: &Value) -> Result, + caller: Caller, + args: &Value, +) -> Result { + pack.start("shell", &args.to_string()) + .with_caller(caller) + .run() + .expect("the call follows the contract") +} + /// Steps `call`, which a test started, to its end, as the Harness does: /// it sends a step only when the call is due one. So it waits with /// [`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 { pub(crate) fn run(pack: &PackRun<'_>, command: &str) -> String { let output = shell(pack, &serde_json::json!({ "command": command })).expect("the command finishes"); - assert!( - !output.is_trusted(), - "a command's output may hold text from outside, so it is untrusted" - ); + assert!(output.is_trusted(), "a command's output is trusted"); output.text().to_owned() } diff --git a/crates/plugin-bashkit/src/lib.rs b/crates/plugin-bashkit/src/lib.rs index a731927..3c3141e 100644 --- a/crates/plugin-bashkit/src/lib.rs +++ b/crates/plugin-bashkit/src/lib.rs @@ -34,7 +34,7 @@ //! //! A call runs its `command` in a shell of its own and answers the exit //! code and then standard output followed by standard error, in one -//! fenced block, as untrusted output. A command that exits with a failure +//! fenced block, as trusted output. A command that exits with a failure //! is an output too. Output past 20,000 characters keeps its first and //! its last 10,000 around a line that counts what is left out. //! @@ -190,7 +190,10 @@ //! definition. //! - A call whose arguments do not match the parameters is an //! [`InvalidArguments`](runvil_plugin_sdk::ToolErrorKind::InvalidArguments) -//! failure, and a key the schema does not declare is refused by name. +//! failure on [`Caller::Script`](runvil_plugin_sdk::Caller::Script), and +//! a key the schema does not declare is refused by name. On +//! [`Caller::Model`](runvil_plugin_sdk::Caller::Model), any tool failure +//! is returned as trusted output holding the error message. mod files; #[cfg(test)] diff --git a/crates/plugin-bashkit/src/pack.rs b/crates/plugin-bashkit/src/pack.rs index e32651f..57a22b7 100644 --- a/crates/plugin-bashkit/src/pack.rs +++ b/crates/plugin-bashkit/src/pack.rs @@ -4,7 +4,7 @@ use std::sync::Arc; use runvil_plugin_sdk::{ - BoxFuture, Pack, PackInfo, ToolCall, ToolError, ToolErrorKind, ToolInfo, ToolOutput, + BoxFuture, Caller, Pack, PackInfo, ToolCall, ToolError, ToolErrorKind, ToolInfo, ToolOutput, }; use tokio::runtime::Handle; @@ -55,19 +55,29 @@ impl Pack for BashPack { fn call<'a>(&'a self, call: ToolCall<'a>) -> BoxFuture<'a, Result> { Box::pin(async move { - if call.tool() != SHELL { - return Err(ToolError::new( + let caller = call.caller(); + let result = if call.tool() == SHELL { + crate::shell::call(&self.timers, call).await + } else { + Err(ToolError::new( ToolErrorKind::Other, format!("bash has no tool named {}", call.tool()), - )); + )) + }; + match result { + Err(error) if caller == Caller::Model => { + Ok(ToolOutput::trusted(error.message().to_owned())) + } + other => other, } - crate::shell::call(&self.timers, call).await }) } } #[cfg(test)] mod tests { + use runvil_plugin_sdk::{Caller, ToolErrorKind, ToolOutput}; + use crate::fixture::{PACK, loaded}; #[test] @@ -110,4 +120,27 @@ mod tests { let second = loaded.new_pack(PACK).expect("the Pack instance is made"); assert_eq!(first.tools(), second.tools()); } + + #[test] + fn a_failing_call_is_an_error_for_a_script_and_trusted_output_for_a_model() { + let loaded = loaded(); + let pack = loaded.new_pack(PACK).expect("the Pack instance is made"); + let script_result = pack + .start("shell", "{}") + .with_caller(Caller::Script) + .run() + .expect("the call follows the contract"); + let error = script_result.expect_err("a script call receives a ToolError"); + assert_eq!(error.kind(), ToolErrorKind::InvalidArguments); + + let model_result = pack + .start("shell", "{}") + .with_caller(Caller::Model) + .run() + .expect("the call follows the contract"); + assert_eq!( + model_result, + Ok(ToolOutput::trusted(error.message().to_owned())) + ); + } } diff --git a/crates/plugin-bashkit/src/session-tests.rs b/crates/plugin-bashkit/src/session-tests.rs index 22493c3..85c44bf 100644 --- a/crates/plugin-bashkit/src/session-tests.rs +++ b/crates/plugin-bashkit/src/session-tests.rs @@ -7,7 +7,7 @@ use std::thread; use std::time::{Duration, Instant}; use runvil_plugin_sdk::testing::Files; -use runvil_plugin_sdk::{ToolError, ToolErrorKind, ToolOutput}; +use runvil_plugin_sdk::{Caller, ToolError, ToolErrorKind, ToolOutput}; use serde_json::json; use super::POLL_CAP; @@ -29,7 +29,7 @@ fn files() -> Files { /// The output of a call whose command exited with `code` after writing /// `output`. fn exited(code: i32, output: &str) -> ToolOutput { - ToolOutput::untrusted(answer(code, output)) + ToolOutput::trusted(answer(code, output)) } /// 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() { // Three jobs ask for a file each, and the script sleeps past its time // limit. bashkit stops the script at the limit and keeps its jobs, // which still wait for their reads. - let mut call = pack.start( - "shell", - &json!({ - "command": "cat /notes/a.md & cat /notes/b.md & cat /notes/c.md & sleep 20", - "block_until_ms": 50, - }) - .to_string(), - ); + let mut call = pack + .start( + "shell", + &json!({ + "command": "cat /notes/a.md & cat /notes/b.md & cat /notes/c.md & sleep 20", + "block_until_ms": 50, + }) + .to_string(), + ) + .with_caller(Caller::Script); assert_eq!(call.step(), Ok(None), "the Host has the first read"); // The limit passes here. The next step brings the first answer and // 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() { // Three jobs ask for a write each, and the script sleeps past its // time limit. bashkit stops the script at the limit and keeps its // jobs, which still wait for their writes. - let mut call = pack.start( - "shell", - &json!({ - "command": "echo x > /notes/x.md & echo y > /notes/y.md & echo z > /notes/z.md & \ - sleep 20", - "block_until_ms": 50, - }) - .to_string(), - ); + let mut call = pack + .start( + "shell", + &json!({ + "command": "echo x > /notes/x.md & echo y > /notes/y.md & echo z > /notes/z.md & \ + sleep 20", + "block_until_ms": 50, + }) + .to_string(), + ) + .with_caller(Caller::Script); assert_eq!(call.step(), Ok(None), "the Host has the first write"); // The limit passes here. The next step brings the first answer and // stops the script, with the two other writes in progress. diff --git a/crates/plugin-bashkit/src/shell-tests.rs b/crates/plugin-bashkit/src/shell-tests.rs index 81dc724..fa12474 100644 --- a/crates/plugin-bashkit/src/shell-tests.rs +++ b/crates/plugin-bashkit/src/shell-tests.rs @@ -2,11 +2,11 @@ //! answers, what a call leaves for the next, the time limit, the output //! cap, and the arguments a call may pass. -use runvil_plugin_sdk::ToolErrorKind; +use runvil_plugin_sdk::{Caller, ToolErrorKind}; use serde_json::json; use super::{OUTPUT_CAP, middle_cut, quoted}; -use crate::fixture::{PACK, answer, loaded, run, shell}; +use crate::fixture::{PACK, answer, loaded, run, shell, shell_as}; #[test] 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() { fn a_command_past_its_time_limit_is_stopped_and_the_call_fails_with_the_limit() { let loaded = loaded(); let pack = loaded.new_pack(PACK).expect("the Pack instance is made"); - let stopped = shell( + let stopped = shell_as( &pack, + Caller::Script, &json!({ "command": "sleep 20", "block_until_ms": 50 }), ) .expect_err("the command runs past its limit"); @@ -113,8 +114,12 @@ fn a_time_limit_over_the_ceiling_is_lowered_to_it() { fn a_command_that_does_not_parse_fails_with_the_shells_reason() { let loaded = loaded(); let pack = loaded.new_pack(PACK).expect("the Pack instance is made"); - let error = shell(&pack, &json!({ "command": "if true; then" })) - .expect_err("the command does not parse"); + let error = shell_as( + &pack, + Caller::Script, + &json!({ "command": "if true; then" }), + ) + .expect_err("the command does not parse"); assert_eq!(error.kind(), ToolErrorKind::Backend); assert_eq!( error.message(), @@ -138,8 +143,12 @@ fn a_command_the_shell_does_not_have_is_an_output_that_says_so() { fn a_command_that_loops_without_end_is_stopped_by_the_shells_limits() { let loaded = loaded(); let pack = loaded.new_pack(PACK).expect("the Pack instance is made"); - let error = shell(&pack, &json!({ "command": "while true; do :; done" })) - .expect_err("the loop passes a limit"); + let error = shell_as( + &pack, + Caller::Script, + &json!({ "command": "while true; do :; done" }), + ) + .expect_err("the loop passes a limit"); assert_eq!(error.kind(), ToolErrorKind::Backend); assert_eq!( error.message(), @@ -224,7 +233,7 @@ fn arguments_that_do_not_match_the_parameters_are_refused() { "invalid value", ), ] { - let error = shell(&pack, &args).expect_err("the arguments are refused"); + let error = shell_as(&pack, Caller::Script, &args).expect_err("the arguments are refused"); assert_eq!(error.kind(), ToolErrorKind::InvalidArguments, "{args}"); assert!( error.message().starts_with("Shell: ") && error.message().contains(part), @@ -234,6 +243,44 @@ fn arguments_that_do_not_match_the_parameters_are_refused() { } } +#[test] +fn model_calls_return_invalid_arguments_and_backend_errors_as_trusted_output() { + let loaded = loaded(); + let pack = loaded.new_pack(PACK).expect("the Pack instance is made"); + + let invalid = shell(&pack, &json!({ "command": "true", "block_until_ms": 0 })) + .expect("a model call receives invalid-arguments errors as output"); + assert!(invalid.is_trusted()); + assert!( + invalid.text().starts_with("Shell: ") + && invalid + .text() + .contains("block_until_ms is 0, and it must be at least 1"), + "{}", + invalid.text() + ); + + let timed_out = shell( + &pack, + &json!({ "command": "sleep 20", "block_until_ms": 50 }), + ) + .expect("a model call receives timeout errors as output"); + assert!(timed_out.is_trusted()); + assert_eq!( + timed_out.text(), + "Shell: the command was stopped at its time limit of 50 ms, before it finished." + ); + + let parse_error = shell(&pack, &json!({ "command": "if true; then" })) + .expect("a model call receives backend errors as output"); + assert!(parse_error.is_trusted()); + assert_eq!( + parse_error.text(), + "Shell: the shell did not finish the command: parse error at line 2, column 1: syntax \ + error: unexpected end of file" + ); +} + #[test] fn a_description_and_null_for_an_optional_parameter_are_accepted() { let loaded = loaded(); diff --git a/crates/plugin-bashkit/src/shell.rs b/crates/plugin-bashkit/src/shell.rs index 3e2c58c..bfeea55 100644 --- a/crates/plugin-bashkit/src/shell.rs +++ b/crates/plugin-bashkit/src/shell.rs @@ -80,12 +80,12 @@ impl Invocation { let moved = shell.run(call, &change, self.limit).await; let moved = moved.map_err(|error| self.failure(&error))?; if moved.exit_code != 0 { - return Ok(ToolOutput::untrusted(render(&moved))); + return Ok(ToolOutput::trusted(render(&moved))); } } let ran = shell.run(call, &self.command, self.limit).await; let ran = ran.map_err(|error| self.failure(&error))?; - Ok(ToolOutput::untrusted(render(&ran))) + Ok(ToolOutput::trusted(render(&ran))) } /// The failure of a call whose command bashkit ended with `error`. diff --git a/crates/plugin-bashkit/tests/it/main.rs b/crates/plugin-bashkit/tests/it/main.rs index 26e2b13..5374e1e 100644 --- a/crates/plugin-bashkit/tests/it/main.rs +++ b/crates/plugin-bashkit/tests/it/main.rs @@ -17,6 +17,7 @@ fn a_host_that_links_the_crate_loads_the_plugin_and_runs_a_command() { .call("shell", &json!({ "command": "echo linked" }).to_string()) .expect("the call follows the contract") .expect("the command finishes"); + assert!(output.is_trusted()); assert_eq!( output.text(), "Exit code: 0\n\nCommand output:\n\n```\nlinked\n```"