Files
optimclaw/tests/tool_schema_validation.rs
T
470de5bd2d feat: merge http/web_fetch tools, add tool output stash for large responses (#578)
* feat: merge http/web_fetch tools, add tool output stash for large responses

Merge `web_fetch` into `http` tool with smart approval: plain GETs (no
headers, no body) run without approval and follow redirects with SSRF
re-validation per hop; all other requests require approval as before.

Add `tool_output_stash` on JobContext so full tool outputs are preserved
before safety-layer truncation. The `json` tool gains a
`source_tool_call_id` parameter to reference stashed outputs, enabling
reliable parsing of large API responses that exceed the 100KB context
limit.

Other improvements:
- Descriptive User-Agent header using CARGO_PKG_VERSION
- Truncation now keeps partial data + hint about source_tool_call_id
- System prompt reinforces tool_calls over narration
- json tool query/stringify handle pre-parsed (non-string) data

[skip-regression-check]

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* chore: delete dead web_fetch.rs (merged into http tool)

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: fix rustfmt formatting

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* style: rename shadowed data binding for clarity in json tool

Address PR review: rename owned `data` to `data_value` before
re-binding as `let data = &data_value` to make ownership explicit.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix(ci): mark network-dependent trace tests as #[ignore]

The weather_sf and baseball_stats tests hit live external APIs (wttr.in,
ESPN) which are unreliable in CI. Mark them #[ignore] so they don't
block the pipeline. Run locally with `--ignored` to include them.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: replay recorded HTTP exchanges in trace tests instead of hitting live APIs

Wire ReplayingHttpInterceptor into TestRig when the trace fixture
contains http_exchanges. This replays recorded responses instead of
making live network calls, making tests deterministic and CI-stable.

Add captured HTTP responses to weather_sf.json (wttr.in) and
baseball_stats.json (ESPN API) fixtures.

Revert #[ignore] on both tests — they now run offline.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

* fix: recover inline bracket-format tool calls from LLM text responses

When flatten_tool_messages converts tool calls to text like
`[Called tool `http` with arguments: {...}]` for NEAR AI compatibility,
the LLM sometimes echoes this format back in its text responses instead
of using proper tool_calls. Add recovery for this bracket format in
recover_tool_calls_from_content and strip it in clean_response so
users don't see raw tool call syntax.

Co-Authored-By: Claude Opus 4.6 <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 <[email protected]>
2026-03-06 00:49:10 +00:00

141 lines
4.6 KiB
Rust

//! Validates that all built-in tool schemas conform to OpenAI strict-mode rules.
//!
//! This catches the class of bugs where `required` keys aren't in `properties`,
//! properties are missing `type` (intentional freeform is allowed), or nested
//! objects/arrays are malformed.
//!
//! See: <https://github.com/nearai/ironclaw/issues/352> (QA plan, item 1.1)
use ironclaw::tools::validate_tool_schema;
use ironclaw::tools::{Tool, ToolRegistry};
/// Validate schemas of all tools registered via `register_builtin_tools()` and
/// `register_dev_tools()` (echo, time, json, http, shell, file tools).
///
/// These tools can be constructed without external dependencies (no DB, no
/// workspace, no extension manager). Tools requiring dependencies (memory, job,
/// skill, extension, routine) are validated individually below where test
/// construction helpers exist.
#[tokio::test]
async fn all_core_builtin_tool_schemas_are_valid() {
let registry = ToolRegistry::new();
registry.register_builtin_tools();
registry.register_dev_tools();
let tools = registry.all().await;
assert!(
!tools.is_empty(),
"registry should have tools after registration"
);
let mut all_errors = Vec::new();
for tool in &tools {
let schema = tool.parameters_schema();
let errors = validate_tool_schema(&schema, tool.name());
if !errors.is_empty() {
all_errors.push(format!(
"Tool '{}' has schema errors:\n {}",
tool.name(),
errors.join("\n ")
));
}
}
assert!(
all_errors.is_empty(),
"Tool schema validation failures:\n{}",
all_errors.join("\n\n")
);
}
/// Verify the exact set of tools registered by the core registration methods.
/// This guards against a new tool being added without schema validation coverage.
#[tokio::test]
async fn core_registration_covers_expected_tools() {
let registry = ToolRegistry::new();
registry.register_builtin_tools();
registry.register_dev_tools();
let mut names = registry.list().await;
names.sort();
let expected = &[
"apply_patch",
"echo",
"http",
"json",
"list_dir",
"read_file",
"shell",
"time",
"write_file",
];
assert_eq!(
names, expected,
"Core tool set changed. Update this test and ensure new tools have valid schemas."
);
}
/// Validate individual tool schemas that are known to use non-trivial patterns.
/// These are regression tests for specific bugs.
#[test]
fn json_tool_freeform_data_field_is_valid() {
// Regression: json tool's "data" field intentionally has no "type" for
// OpenAI compatibility (union types with arrays require "items").
let tool = ironclaw::tools::builtin::JsonTool;
let schema = tool.parameters_schema();
let errors = validate_tool_schema(&schema, "json");
assert!(errors.is_empty(), "json tool schema errors: {errors:?}");
// Verify the freeform pattern is still in place
let data = schema
.get("properties")
.and_then(|p| p.get("data"))
.expect("json tool should have 'data' property");
assert!(
data.get("type").is_none(),
"json.data should be freeform (no type) for OpenAI compatibility"
);
}
#[test]
fn http_tool_headers_array_is_valid() {
// Regression: http tool's "headers" is an array of {name, value} objects.
let tool = ironclaw::tools::builtin::HttpTool::new();
let schema = tool.parameters_schema();
let errors = validate_tool_schema(&schema, "http");
assert!(errors.is_empty(), "http tool schema errors: {errors:?}");
// Verify array structure
let headers = schema
.get("properties")
.and_then(|p| p.get("headers"))
.expect("http tool should have 'headers' property");
assert_eq!(
headers.get("type").and_then(|t| t.as_str()),
Some("array"),
"headers should be an array"
);
assert!(
headers.get("items").is_some(),
"headers array should have items defined"
);
}
#[test]
fn time_tool_schema_is_valid() {
let tool = ironclaw::tools::builtin::TimeTool;
let schema = tool.parameters_schema();
let errors = validate_tool_schema(&schema, "time");
assert!(errors.is_empty(), "time tool schema errors: {errors:?}");
}
#[test]
fn shell_tool_schema_is_valid() {
let tool = ironclaw::tools::builtin::ShellTool::new();
let schema = tool.parameters_schema();
let errors = validate_tool_schema(&schema, "shell");
assert!(errors.is_empty(), "shell tool schema errors: {errors:?}");
}