Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/plugin-web/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ use runvil_plugin_sdk::ToolErrorKind;

/// How `call` should treat a [`FetchError`].
///
/// A [`Disposition::SoftOutput`] error is returned to the model as untrusted
/// tool text so it can try a different URL; a [`Disposition::Hard`] error aborts
/// the call with the given [`ToolErrorKind`].
/// A [`Disposition::SoftOutput`] error is returned to the model as trusted
/// tool text so it can try a different URL; a [`Disposition::Hard`] error
/// aborts the call with the given [`ToolErrorKind`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Disposition {
/// Returns the model-facing text as untrusted tool output.
/// Returns the model-facing text as trusted tool output.
SoftOutput,
/// Aborts the call as a failure of this kind.
Hard(ToolErrorKind),
Expand Down
22 changes: 11 additions & 11 deletions crates/plugin-web/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::response::{
use crate::task;
use crate::url_policy::check_url;

/// The result [`WebFetch::call`] returns: untrusted page text on success,
/// The result [`WebFetch::call`] returns: trusted page text on success,
/// a narrow [`ToolError`] on a hard failure.
type CallResult = Result<ToolOutput, ToolError>;

Expand Down Expand Up @@ -137,7 +137,7 @@ impl FetchClient {
url: SafeUrl::new(final_url.as_str()),
status: status.as_u16(),
};
return Ok(ToolOutput::untrusted(err.model_facing()));
return Ok(ToolOutput::trusted(err.model_facing()));
}

let content_type = response
Expand Down Expand Up @@ -219,7 +219,7 @@ impl FetchClient {
let (text, char_truncated) = truncate_to_chars(&decoded, max_chars);
let truncated = size_truncated || char_truncated;

Ok(ToolOutput::untrusted(format!(
Ok(ToolOutput::trusted(format!(
"url: {final_url}\ntruncated: {truncated}\nextraction: {}\n\n{text}",
extraction.label()
)))
Expand Down Expand Up @@ -267,18 +267,18 @@ fn did_not_finish() -> ToolError {
/// [`Disposition`].
fn soft_or_hard(err: &FetchError) -> CallResult {
match err.classify() {
Disposition::SoftOutput => Ok(ToolOutput::untrusted(err.model_facing())),
Disposition::SoftOutput => Ok(ToolOutput::trusted(err.model_facing())),
Disposition::Hard(kind) => Err(ToolError::new(kind, err.model_facing())),
}
}

/// Maps a body-read [`FetchError`] into soft, untrusted tool text.
/// Maps a body-read [`FetchError`] into soft, trusted tool text.
///
/// A body-read failure is a size cap ([`FetchError::TooLarge`]) or a mid-stream
/// transport failure ([`FetchError::BodyRead`]); both are soft and returned as
/// model-facing tool text so the model can try a different URL.
fn body_read_outcome(err: &FetchError) -> ToolOutput {
ToolOutput::untrusted(err.model_facing())
ToolOutput::trusted(err.model_facing())
}

/// Maps a reqwest send error into either a soft tool result or a hard `Err`.
Expand All @@ -291,21 +291,21 @@ fn map_send_error_to_outcome(err: &reqwest::Error, url: &str) -> CallResult {
while let Some(current) = source {
if let Some(fetch_err) = current.downcast_ref::<FetchError>() {
return match fetch_err.classify() {
Disposition::SoftOutput => Ok(ToolOutput::untrusted(fetch_err.model_facing())),
Disposition::SoftOutput => Ok(ToolOutput::trusted(fetch_err.model_facing())),
Disposition::Hard(kind) => Err(ToolError::new(kind, fetch_err.model_facing())),
};
}
source = current.source();
}
if err.is_timeout() {
return Ok(ToolOutput::untrusted(
return Ok(ToolOutput::trusted(
FetchError::Timeout {
url: SafeUrl::new(url),
}
.model_facing(),
));
}
Ok(ToolOutput::untrusted(format!(
Ok(ToolOutput::trusted(format!(
"fetch failed for {url}: network error; try a different URL"
)))
}
Expand Down Expand Up @@ -347,13 +347,13 @@ impl WebFetch {
}

/// Fetches the URL `args` names on the Plugin's runtime and renders its
/// response as untrusted text. Dropping the returned future aborts the
/// response as trusted text. Dropping the returned future aborts the
/// fetch.
///
/// # Errors
/// Returns a [`ToolError`] for invalid arguments, a URL the policy
/// refuses outright, or a fetch task that did not finish. A page the
/// fetch could not render is a soft, untrusted output instead.
/// fetch could not render is a soft, trusted output instead.
pub(crate) async fn call(&self, args: serde_json::Value) -> CallResult {
let url = args
.get("url")
Expand Down
6 changes: 3 additions & 3 deletions crates/plugin-web/src/fetch/tests-body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,14 @@ async fn flat_text_body_read_failure_is_soft() {
let url = format!("http://localhost:{port}/plainbroken");
// The mid-stream failure must be a soft (recoverable) return, never a
// hard error: identical to the HTML and structured routes. A `text()`
// return proves the outcome was soft untrusted output.
// return proves the outcome was soft trusted output.
let outcome = tool
.call(serde_json::json!({ "url": url }))
.await
.expect("a mid-stream flat-text failure must be a soft return, not a hard error");
assert!(
!outcome.is_trusted(),
"a soft body-read failure must be untrusted output"
outcome.is_trusted(),
"a soft body-read failure must be trusted output"
);
let result = outcome.text().to_owned();
assert!(
Expand Down
27 changes: 18 additions & 9 deletions crates/plugin-web/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
//! verbatim. The tool refuses every other type, and a response that
//! declares none.
//!
//! The answer is untrusted output: three header lines, then a blank
//! line, then the text. The header lines are `url:`, the URL the text
//! came from once every redirect was followed, `truncated:`, and
//! The answer is trusted output: three header lines, then a blank line,
//! then the text. The header lines are `url:`, the URL the text came
//! from once every redirect was followed, `truncated:`, and
//! `extraction:`, how the text was made. The text is cut at 40,000
//! characters, or at `max_chars` when the call names fewer.
//!
Expand All @@ -49,10 +49,13 @@
//! port outside the policy, has an IP address for its host, or resolves
//! to no address the policy allows. A call whose fetch task panics, or
//! whose page makes the rendering panic, fails with `web_fetch: the fetch
//! task did not finish`. Anything else that goes wrong is an untrusted
//! output that says so, such as an HTTP error status, a timeout, a
//! refused redirect, a body over the size cap, or a type the tool does
//! not render. The model reads it and can try another URL.
//! task did not finish`. Anything else that goes wrong is a soft failure
//! returned as trusted output for both callers, such as an HTTP error
//! status, a timeout, a refused redirect, a body over the size cap, or a
//! type the tool does not render. For hard failures represented by
//! `ToolError` (invalid arguments, policy-refused URLs, or a panicked
//! fetch task), a model call receives the error message as trusted
//! output, while a script call receives the `ToolError`.
//!
//! # The fetch policy
//!
Expand Down Expand Up @@ -95,7 +98,7 @@
//! rules of its own, which are stricter in places, such as the form of a
//! country code, and it answers what it refuses with an error status.
//!
//! The answer is untrusted output: the Gateway's reply as compact JSON,
//! The answer is trusted output: the Gateway's reply as compact JSON,
//! an object with the `query` the Gateway ran and a `results` array.
//! Each result has a `title`, a `url`, and a `description`, and it has
//! an `age`, a `site_name`, and `extra_snippets` when the Gateway sent
Expand Down Expand Up @@ -124,6 +127,10 @@
//! and when the task of the search does not come to its end, with
//! `web_search: the search task did not finish`.
//!
//! On a script call, these failures are returned as `ToolError` with
//! those kinds. On a model call, any failure is returned as trusted
//! output holding the error message.
//!
//! # Where a search goes
//!
//! Every tool call is lent the Host's resources: the JSON text of an
Expand Down Expand Up @@ -216,7 +223,9 @@
//! output, no failure, and no `Debug` text. No failure quotes the
//! Host's resources. `gateway.rs` alone takes the key out of them, and
//! the fields it keeps the key in are private to it.
//! - Every output of both tools is untrusted.
//! - Every output of both tools is trusted. On a model call, any
//! `ToolError` is mapped to trusted output with its message; on a
//! script call, `ToolError` is returned as-is.
//! - The Plugin instance owns a tokio runtime with two worker threads,
//! with I/O and time on, and every request, a fetch and a search
//! alike, runs as a task on it. Dropping the instance drops the
Expand Down
7 changes: 2 additions & 5 deletions crates/plugin-web/src/search-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,13 @@ fn full_and_bare_results() -> Value {
}

#[tokio::test]
async fn results_render_as_the_gateways_serialization_and_are_untrusted() {
async fn results_render_as_the_gateways_serialization_and_are_trusted() {
let (mock, _seen) = gateway_answering(full_and_bare_results()).await;
let output = search(&mock.url(), serde_json::json!({ "query": "rust" }))
.await
.expect("the Gateway's results render");

assert!(
!output.is_trusted(),
"external search content must be marked untrusted"
);
assert!(output.is_trusted(), "search content must be marked trusted");
// The Gateway's `WebSearchResponse` serialization of the same results:
// its field order, with an absent `age` or `site_name` and empty
// `extra_snippets` left out.
Expand Down
19 changes: 8 additions & 11 deletions crates/plugin-web/src/search.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `web_search` tool: validate a search's arguments, send them to the
//! Gateway that the Host's resources name, on the Plugin's runtime, and
//! return the results as untrusted compact JSON in the Gateway's shape.
//! return the results as trusted compact JSON in the Gateway's shape.
//!
//! [`SearchRequest`] is the arguments as the model sent them, once they
//! deserialized and passed their bounds. Nothing sits between the tool and
Expand Down Expand Up @@ -32,10 +32,10 @@ const MAX_DOMAINS: usize = 20;
/// A tool that searches the web through the Gateway.
///
/// Each call validates its arguments, reads where the search goes from the
/// Host's resources, and returns the Gateway's results as untrusted
/// output. It holds the shared [`SearchClient`], whose deadline covers
/// every search, and the handle of the Plugin's runtime, which every
/// search is spawned onto.
/// Host's resources, and returns the Gateway's results as trusted output.
/// It holds the shared [`SearchClient`], whose deadline covers every
/// search, and the handle of the Plugin's runtime, which every search is
/// spawned onto.
#[derive(Debug, Clone)]
pub(crate) struct WebSearch {
/// The client every search goes through.
Expand Down Expand Up @@ -122,8 +122,8 @@ impl WebSearch {

/// Validates `args`, reads where the search goes from `resources`, the
/// JSON text of the Host's resources, runs the search on the Plugin's
/// runtime, and returns the results as untrusted compact JSON.
/// Dropping the returned future aborts the search.
/// runtime, and returns the results as trusted compact JSON. Dropping
/// the returned future aborts the search.
///
/// # Errors
/// Returns a [`ToolError`] for invalid arguments, for resources whose
Expand Down Expand Up @@ -157,10 +157,7 @@ impl WebSearch {
)
})?;

// The results embed third-party titles, URLs, and descriptions, so
// the body is marked untrusted: it is nonce-wrapped before it can
// reach model input.
Ok(ToolOutput::untrusted(body))
Ok(ToolOutput::trusted(body))
}
}

Expand Down
Loading
Loading