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
25 changes: 18 additions & 7 deletions crates/runvil-plugin-sdk/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,16 @@ const fn is_segment(bytes: &[u8], from: usize, to: usize) -> bool {
true
}

/// Whether `name` is a wire name the Harness accepts: 1 to 64 bytes of
/// ASCII letters, digits, `_`, and `-`.
/// Whether `name` is a wire name the Harness accepts: 1 to 64 bytes
/// starting with an ASCII letter, followed by ASCII letters, digits, `_`,
/// and `-`.
#[cfg(any(test, feature = "test-support"))]
pub(crate) const fn is_valid_wire_name(name: &str) -> bool {
let bytes = name.as_bytes();
if bytes.is_empty() || bytes.len() > RV_MAX_NAME {
if bytes.is_empty() || bytes.len() > RV_MAX_NAME || !bytes[0].is_ascii_alphabetic() {
return false;
}
let mut at = 0;
let mut at = 1;
while at < bytes.len() {
if !matches!(bytes[at], b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_' | b'-') {
return false;
Expand Down Expand Up @@ -174,11 +175,21 @@ mod tests {
}

#[test]
fn a_wire_name_is_ascii_letters_digits_underscores_and_hyphens() {
for name in ["Read", "StrReplace", "web_fetch", "a-b", "0"] {
fn a_wire_name_starts_with_an_ascii_letter_and_holds_letters_digits_underscores_and_hyphens() {
for name in ["Read", "StrReplace", "web_fetch", "a-b", "a0"] {
assert!(is_valid_wire_name(name), "{name}");
}
for name in ["", "web/fetch", "a.b", "a b", "café"] {
for name in [
"",
"0",
"0_fetch",
"_fetch",
"-fetch",
"web/fetch",
"a.b",
"a b",
"café",
] {
assert!(!is_valid_wire_name(name), "{name:?}");
}
assert!(is_valid_wire_name(&"a".repeat(RV_MAX_NAME)));
Expand Down
8 changes: 7 additions & 1 deletion crates/runvil-plugin-sdk/src/testing/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ pub(super) enum Breach {
ToolTwice,
/// Adds a tool whose wire name has a `/`.
WireName,
/// Adds a tool whose wire name starts with a digit instead of an ASCII letter.
WireNameLeadingDigit,
/// Adds a tool with an empty description.
NoDescription,
/// Adds a tool whose parameters are a JSON array.
Expand Down Expand Up @@ -238,7 +240,11 @@ fn tool_info(breach: Breach) -> RvToolInfo {
size: size_field::<RvToolInfo>(),
flags: if breach == Breach::ToolFlags { 2 } else { 0 },
name: text_unless(breach, Breach::ToolName, "tool", "Not A Segment"),
wire_name: text_unless(breach, Breach::WireName, "Tool", "web/fetch"),
wire_name: match breach {
Breach::WireName => rv_str("web/fetch"),
Breach::WireNameLeadingDigit => rv_str("0_fetch"),
_ => rv_str("Tool"),
},
description: text_unless(breach, Breach::NoDescription, "A fixture tool.", ""),
parameters: text_unless(breach, Breach::Parameters, "{}", "[]"),
}
Expand Down
10 changes: 8 additions & 2 deletions crates/runvil-plugin-sdk/src/testing/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ pub(super) unsafe fn add(
// SAFETY: as above.
let wire_name = unsafe { WIRE_NAME.read(info.wire_name) }?;
if !wire_name.is_empty() && !is_valid_wire_name(&wire_name) {
return Err("the tool's wire name is not ASCII letters, digits, `_`, and `-`");
return Err(
"the tool's wire name must start with an ASCII letter and hold only ASCII letters, digits, `_`, and `-`",
);
}
// SAFETY: as above.
let description = unsafe { DESCRIPTION.read(info.description) }?;
Expand Down Expand Up @@ -139,7 +141,11 @@ mod tests {
),
(
Breach::WireName,
"the tool's wire name is not ASCII letters, digits, `_`, and `-`",
"the tool's wire name must start with an ASCII letter and hold only ASCII letters, digits, `_`, and `-`",
),
(
Breach::WireNameLeadingDigit,
"the tool's wire name must start with an ASCII letter and hold only ASCII letters, digits, `_`, and `-`",
),
(Breach::NoDescription, "the tool's description is empty"),
(
Expand Down
7 changes: 4 additions & 3 deletions crates/runvil-plugin-sdk/src/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ pub struct ToolInfo {
/// letters, digits, `-`, `_`, and `.`, such as `fetch`. The Host puts
/// the Pack id before it to form the tool's id, as in `url/fetch`.
pub name: String,
/// The name a model calls the tool by: 1 to 64 bytes of ASCII letters,
/// digits, `_`, and `-`. `None` lets the Host derive it from the
/// tool's id, as `url_fetch` from `url/fetch`.
/// The name a model calls the tool by: 1 to 64 bytes starting with an
/// ASCII letter, followed by ASCII letters, digits, `_`, and `-`. `None`
/// lets the Host derive it from the tool's id, as `url_fetch` from
/// `url/fetch`.
pub wire_name: Option<String>,
/// What the tool does, for a model to read: non-empty, at most 65536
/// bytes.
Expand Down
7 changes: 4 additions & 3 deletions crates/runvil-plugin-sys/include/runvil_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -640,9 +640,10 @@ struct RvToolInfo {
*/
struct RvStr name;
/**
* The name a model calls the tool by: 1 to `RV_MAX_NAME` bytes of
* ASCII letters, digits, `_`, and `-`. Empty lets the Host derive it
* from the tool's id, as `url_fetch` from `url/fetch`.
* The name a model calls the tool by: 1 to `RV_MAX_NAME` bytes
* starting with an ASCII letter, followed by ASCII letters, digits, `_`,
* and `-`. Empty lets the Host derive it from the tool's id, as
* `url_fetch` from `url/fetch`.
*/
struct RvStr wire_name;
/**
Expand Down
7 changes: 4 additions & 3 deletions crates/runvil-plugin-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -616,9 +616,10 @@ pub struct RvToolInfo {
pub flags: RvToolFlags,
/// The tool's name within its Pack: one name segment, such as `fetch`.
pub name: RvStr,
/// The name a model calls the tool by: 1 to [`RV_MAX_NAME`] bytes of
/// ASCII letters, digits, `_`, and `-`. Empty lets the Host derive it
/// from the tool's id, as `url_fetch` from `url/fetch`.
/// The name a model calls the tool by: 1 to [`RV_MAX_NAME`] bytes
/// starting with an ASCII letter, followed by ASCII letters, digits, `_`,
/// and `-`. Empty lets the Host derive it from the tool's id, as
/// `url_fetch` from `url/fetch`.
pub wire_name: RvStr,
/// What the tool does, for a model to read: non-empty, at most
/// [`RV_MAX_TOOL_TEXT`] bytes.
Expand Down
Loading