use rmcp::model::{CallToolResult, JsonObject};
use serde::de::DeserializeOwned;
use serde_json::Value;
pub(crate) use crate::index_lifecycle::guidance::request_refusal::{
ABSENT, ModeSelection, StrictError, When, decode_strict, from_value_noting, note_keys_of,
unknown_arguments_refusal,
};
use crate::index_lifecycle::guidance::request_refusal::{
condition_refusal, conflicting_selection_refusal, current_value, literal, normalized,
};
use crate::protocol::SymForgeServer;
use crate::protocol::edit::{
BatchEditInput, BatchInsertInput, BatchRenameInput, DeleteSymbolInput, EditWithinSymbolInput,
InsertSymbolInput, ReplaceSymbolBodyInput,
};
use crate::protocol::read_tools::SymforgeRetrieveInput;
use crate::protocol::result_status::{OutcomeClass, ResultStatus};
use crate::protocol::secret_remediate::SecretRemediateInput;
use crate::protocol::tools::{
AnalyzeFileImpactInput, CheckpointNowInput, CurateKnowledgeInput, DetectImpactInput,
DiffSymbolsInput, EditPlanInput, ExploreInput, FindDependentsInput, FindReferencesInput,
GetFileContentInput, GetFileContextInput, GetRepoMapInput, GetSymbolContextInput,
GetSymbolInput, HealthInput, IndexFolderInput, InspectMatchInput, InvestigationInput,
ReviewKnowledgeInput, SearchFilesInput, SearchKnowledgeInput, SearchSymbolsInput,
SearchTextInput, SmartQueryInput, ValidateFileSyntaxInput, WhatChangedInput,
};
use crate::stel::{StelEditRequest, StelRequest, StelStatusRequest};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum OnMismatch {
#[allow(dead_code)]
SelectMode,
Refuse,
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Condition {
pub argument: &'static str,
pub applies_when: When,
pub on_mismatch: OnMismatch,
#[allow(dead_code)]
pub adaptive_with: Option<&'static str>,
}
#[derive(Debug)]
pub(crate) struct ToolContract {
pub tool: &'static str,
pub conditions: &'static [Condition],
#[allow(dead_code)] pub primary: &'static str,
pub optional_sections: &'static [&'static str],
#[allow(dead_code)] pub explicit_arguments: &'static [&'static str],
pub typical_call: &'static str,
}
const fn cond(argument: &'static str, applies_when: When, on_mismatch: OnMismatch) -> Condition {
Condition {
argument,
applies_when,
on_mismatch,
adaptive_with: None,
}
}
use OnMismatch::{Refuse, SelectMode};
use When::{Absent, Unless};
const fn row(
tool: &'static str,
primary: &'static str,
typical_call: &'static str,
) -> ToolContract {
ToolContract {
tool,
conditions: &[],
primary,
optional_sections: &[],
explicit_arguments: &[],
typical_call,
}
}
impl ToolContract {
const fn when(mut self, conditions: &'static [Condition]) -> Self {
self.conditions = conditions;
self
}
const fn drops(mut self, optional_sections: &'static [&'static str]) -> Self {
self.optional_sections = optional_sections;
self
}
const fn explicit(mut self, explicit_arguments: &'static [&'static str]) -> Self {
self.explicit_arguments = explicit_arguments;
self
}
}
pub(crate) static TOOL_CONTRACTS: &[ToolContract] = &[
row("health", "index state, counts and watcher state", "{}")
.explicit(&["quarantine_limit", "quarantine_offset"]),
row("health_compact", "one-line index and watcher state", "{}"),
row("checkpoint_now", "snapshot write confirmation", "{}"),
row(
"index_folder",
"index summary of the opened folder",
r#"{"path":"{root}"}"#,
),
row(
"validate_file_syntax",
"syntax verdict with diagnostic locations",
r#"{"path":"src/lib.rs"}"#,
),
row(
"get_file_content",
"the requested lines, numbered",
r#"{"path":"src/long.rs","start_line":1,"end_line":75}"#,
)
.explicit(&[
"start_line",
"end_line",
"around_line",
"around_match",
"around_symbol",
"chunk_index",
"offset",
"limit",
]),
row(
"get_symbol",
"the full source of each named symbol",
r#"{"path":"src/long.rs","name":"long_body"}"#,
)
.when(&[
cond("path", Absent("targets"), Refuse),
cond("name", Absent("targets"), Refuse),
cond("kind", Absent("targets"), Refuse),
cond("symbol_line", Absent("targets"), Refuse),
])
.explicit(&["name", "targets"]),
row(
"get_repo_map",
"directory and key-type overview, source directories first",
"{}",
)
.when(&[cond("depth", Unless("detail", &["full"]), Refuse)])
.explicit(&["detail", "path", "depth"]),
row(
"get_file_context",
"outline of the file",
r#"{"path":"src/parent/leaf.rs"}"#,
)
.drops(&["knowledge"])
.explicit(&["sections"]),
row(
"get_symbol_context",
"definition with every caller and callee named",
r#"{"name":"target_fn","path":"src/parent/leaf.rs"}"#,
)
.drops(&["knowledge"])
.explicit(&["sections", "bundle"]),
row(
"search_symbols",
"symbols whose name matches the query",
r#"{"query":"long_body"}"#,
),
row(
"search_text",
"matching lines with their file and line",
r#"{"query":"LONG_FIRST"}"#,
),
row(
"search_files",
"paths ranked by relevance to the query",
r#"{"query":"leaf"}"#,
),
row(
"search_knowledge",
"matching documentation passages with provenance",
r#"{"query":"zebrafish_quorum"}"#,
),
row(
"review_knowledge",
"knowledge-corpus review for the chosen mode",
r#"{"mode":"summary"}"#,
)
.explicit(&["path"]),
row(
"curate_knowledge",
"preview or result of the policy actions",
r#"{"actions":[],"if_source_review_hash":"{source_review_hash}","if_manifest_digest":"{manifest_digest}","if_policy_digest":"{policy_digest}","apply":false}"#,
),
row(
"find_references",
"every reference site of the symbol",
r#"{"name":"target_fn","path":"src/parent/leaf.rs"}"#,
)
.explicit(&["name"]),
row(
"find_dependents",
"every file that depends on the file",
r#"{"path":"src/parent/leaf.rs"}"#,
)
.explicit(&["path"]),
row(
"inspect_match",
"enclosing symbol, parent and siblings of the line",
r#"{"path":"src/parent/leaf.rs","line":8}"#,
)
.explicit(&["line"]),
row(
"analyze_file_impact",
"symbol churn and dependents of the re-read file",
r#"{"path":"src/parent/leaf.rs"}"#,
),
row(
"what_changed",
"the changed paths",
r#"{"uncommitted":true}"#,
),
row(
"diff_symbols",
"added, removed and modified symbols between the refs",
r#"{"base":"HEAD"}"#,
),
row(
"detect_impact",
"affected symbols or files ranked by distance",
r#"{"base_branch":"main"}"#,
),
row(
"explore",
"symbols, text patterns and files for the concept",
r#"{"query":"target function"}"#,
),
row(
"replace_symbol_body",
"edit confirmation with the changed range",
r#"{"path":"src/short.rs","name":"SHORT_A","new_body":"pub const SHORT_A: u32 = 1;","dry_run":true}"#,
),
row(
"edit_within_symbol",
"edit confirmation with the changed range",
r#"{"path":"src/user.rs","name":"use_it","old_text":"target_fn(7)","new_text":"target_fn(8)","dry_run":true}"#,
),
row(
"insert_symbol",
"insert confirmation with the new range",
r#"{"path":"src/short.rs","name":"SHORT_A","content":"pub const SHORT_Z: u32 = 26;","dry_run":true}"#,
),
row(
"delete_symbol",
"delete confirmation with the removed range",
r#"{"path":"src/short.rs","name":"SHORT_G","dry_run":true}"#,
),
row(
"batch_edit",
"per-edit results of the atomic batch",
r#"{"edits":[{"path":"src/short.rs","name":"SHORT_A","operation":{"type":"replace","new_body":"pub const SHORT_A: u32 = 1;"}}],"dry_run":true}"#,
),
row(
"batch_insert",
"per-target results of the insertion",
r#"{"content":"// x","position":"before","targets":[{"path":"src/short.rs","name":"SHORT_A"}],"dry_run":true}"#,
),
row(
"batch_rename",
"every site the rename changes and every site it leaves alone",
r#"{"path":"src/parent/leaf.rs","name":"target_fn","new_name":"renamed_fn","dry_run":true}"#,
),
row(
"edit_plan",
"recommended edit tool with reference count",
r#"{"target":"target_fn"}"#,
),
row(
"ask",
"the routed tool's answer",
r#"{"query":"who calls target_fn"}"#,
),
row(
"conventions",
"detected conventions of the repository",
"{}",
),
row(
"context_inventory",
"symbols and files fetched this session with token counts",
"{}",
),
row("investigation_suggest", "next things to investigate", "{}"),
row(
"symforge_retrieve",
"the full output behind the handle",
r#"{"hash":"{ccr_hash}"}"#,
)
.explicit(&["hash"]),
row("status", "trust envelope and index health summary", "{}"),
row(
"symforge",
"the routed tool's answer",
r#"{"query":"who calls target_fn"}"#,
),
row(
"symforge_edit",
"edit preview or confirmation",
r#"{"path":"src/short.rs","symbol":"SHORT_A","op":"replace","body":"pub const SHORT_A: u32 = 1;"}"#,
),
row(
"secret_remediate",
"remediation preview or result, never secret bytes",
r#"{"scope":"repo","finding_ids":["{finding_id}"],"action":"externalize"}"#,
),
];
pub(crate) fn contract_for(tool: &str) -> Option<&'static ToolContract> {
TOOL_CONTRACTS.iter().find(|c| c.tool == tool)
}
pub(crate) fn in_drop_order<'a>(tool: &str, sections: &mut [(&'a str, &'a str)]) {
let order = contract_for(tool).map_or(&[][..], |c| c.optional_sections);
sections.sort_by_key(|(name, _)| {
order
.iter()
.position(|listed| listed == name)
.unwrap_or(order.len())
});
}
#[derive(serde::Deserialize, schemars::JsonSchema)]
struct NoArguments {}
type Probe = fn(Value) -> Result<(), StrictError>;
type SchemaNames = fn() -> Vec<String>;
fn probe<P: DeserializeOwned>(value: Value) -> Result<(), StrictError> {
decode_strict::<P>(value).map(drop)
}
fn schema_names<T: schemars::JsonSchema>() -> Vec<String> {
serde_json::to_value(schemars::schema_for!(T))
.ok()
.and_then(|schema| {
schema
.get("properties")
.and_then(Value::as_object)
.map(|properties| properties.keys().cloned().collect())
})
.unwrap_or_default()
}
fn probe_symforge_facade(mut value: Value) -> Result<(), StrictError> {
if let Value::Object(map) = &mut value {
map.remove("_probe_legacy_tool");
map.remove("_probe_legacy_args");
}
probe::<StelRequest>(value)
}
macro_rules! probe_table {
($(($name:literal, $ty:ty)),* $(,)?) => {
static PROBES: &[(&str, Probe, SchemaNames)] = &[
$(($name, probe::<$ty>, schema_names::<$ty>),)*
(
"symforge",
probe_symforge_facade,
schema_names::<crate::stel::SymforgeCallInput>,
),
];
};
}
probe_table! {
("health", HealthInput),
("health_compact", NoArguments),
("checkpoint_now", CheckpointNowInput),
("index_folder", IndexFolderInput),
("validate_file_syntax", ValidateFileSyntaxInput),
("get_file_content", GetFileContentInput),
("get_symbol", GetSymbolInput),
("get_repo_map", GetRepoMapInput),
("get_file_context", GetFileContextInput),
("get_symbol_context", GetSymbolContextInput),
("search_symbols", SearchSymbolsInput),
("search_text", SearchTextInput),
("search_files", SearchFilesInput),
("search_knowledge", SearchKnowledgeInput),
("review_knowledge", ReviewKnowledgeInput),
("curate_knowledge", CurateKnowledgeInput),
("find_references", FindReferencesInput),
("find_dependents", FindDependentsInput),
("inspect_match", InspectMatchInput),
("analyze_file_impact", AnalyzeFileImpactInput),
("what_changed", WhatChangedInput),
("diff_symbols", DiffSymbolsInput),
("detect_impact", DetectImpactInput),
("explore", ExploreInput),
("replace_symbol_body", ReplaceSymbolBodyInput),
("edit_within_symbol", EditWithinSymbolInput),
("insert_symbol", InsertSymbolInput),
("delete_symbol", DeleteSymbolInput),
("batch_edit", BatchEditInput),
("batch_insert", BatchInsertInput),
("batch_rename", BatchRenameInput),
("edit_plan", EditPlanInput),
("ask", SmartQueryInput),
("conventions", NoArguments),
("context_inventory", NoArguments),
("investigation_suggest", InvestigationInput),
("symforge_retrieve", SymforgeRetrieveInput),
("status", StelStatusRequest),
("symforge_edit", StelEditRequest),
("secret_remediate", SecretRemediateInput),
}
fn is_given(value: Option<&Value>) -> bool {
!matches!(normalized(value), None | Some(Value::Bool(false)))
}
fn controller_of(when: &When) -> &'static str {
match when {
When::Equals(argument, _)
| When::Unless(argument, _)
| When::Present(argument)
| When::Absent(argument) => argument,
}
}
fn holds(when: &When, args: &JsonObject) -> bool {
match when {
When::Equals(controller, values) => {
values.contains(¤t_value(args, controller).as_str())
}
When::Unless(controller, values) => {
!values.contains(¤t_value(args, controller).as_str())
}
When::Present(controller) => is_given(args.get(*controller)),
When::Absent(controller) => !is_given(args.get(*controller)),
}
}
pub(crate) fn selection_note(selected: &[ModeSelection]) -> String {
selected
.iter()
.map(|s| {
format!(
"Mode: `{}` selected {}={} for this call.",
s.argument,
s.controller,
literal(s.mode)
)
})
.collect::<Vec<_>>()
.join("\n")
}
pub(crate) fn append_selection_note(result: &mut CallToolResult, selected: &[ModeSelection]) {
if selected.is_empty() {
return;
}
let note = selection_note(selected);
for block in result.content.iter_mut().rev() {
if let rmcp::model::ContentBlock::Text(text) = block {
text.text.push_str(&format!("\n\n{note}"));
return;
}
}
}
pub(crate) fn apply_conditions(
tool: &str,
args: &mut JsonObject,
) -> Result<Vec<ModeSelection>, String> {
match contract_for(tool) {
Some(contract) => apply_rows(tool, contract.conditions, args),
None => Ok(Vec::new()),
}
}
fn apply_rows(
tool: &str,
rows: &[Condition],
args: &mut JsonObject,
) -> Result<Vec<ModeSelection>, String> {
let mut selected: Vec<ModeSelection> = Vec::new();
for condition in rows {
if condition.on_mismatch != SelectMode || !is_given(args.get(condition.argument)) {
continue;
}
let When::Equals(controller, values) = &condition.applies_when else {
continue;
};
let Some(mode) = values.iter().find(|v| **v != ABSENT) else {
continue;
};
if let Some(earlier) = selected.iter().find(|s| s.controller == *controller) {
if earlier.mode != *mode {
return Err(conflicting_selection_refusal(
controller,
earlier,
condition.argument,
mode,
));
}
continue;
}
if current_value(args, controller) != ABSENT {
continue;
}
if condition
.adaptive_with
.is_some_and(|other| is_given(args.get(other)))
{
continue;
}
let value = match *mode {
"true" => Value::Bool(true),
"false" => Value::Bool(false),
other => Value::String(other.to_string()),
};
args.insert((*controller).to_string(), value);
selected.push(ModeSelection {
controller,
mode,
argument: condition.argument,
});
}
for condition in rows {
if !is_given(args.get(condition.argument)) || holds(&condition.applies_when, args) {
continue;
}
let controller = controller_of(&condition.applies_when);
let controller_absent = current_value(args, controller) == ABSENT;
if controller_absent
&& condition
.adaptive_with
.is_some_and(|other| is_given(args.get(other)))
{
continue;
}
return Err(condition_refusal(
tool,
condition.argument,
&condition.applies_when,
args,
&selected,
));
}
Ok(selected)
}
fn schema_argument_names(tool: &rmcp::model::Tool) -> Vec<String> {
tool.input_schema
.get("properties")
.and_then(Value::as_object)
.map(|properties| properties.keys().cloned().collect())
.unwrap_or_default()
}
impl SymForgeServer {
pub(crate) fn refuse_invalid_arguments(
&self,
tool: &str,
arguments: &mut Option<JsonObject>,
) -> Result<Vec<ModeSelection>, CallToolResult> {
let Some(args) = arguments.as_mut() else {
return Ok(Vec::new());
};
let refuse = |text: String| {
ResultStatus::new(OutcomeClass::InvalidRequest).into_call_tool_result(text)
};
if let Some((_, probe, _)) = PROBES.iter().find(|(name, _, _)| *name == tool)
&& let Err(StrictError::Unknown(unknown)) = probe(Value::Object(args.clone()))
{
let valid = self
.tool_router
.get(tool)
.map(schema_argument_names)
.unwrap_or_default();
return Err(refuse(unknown_arguments_refusal(tool, &unknown, &valid)));
}
apply_conditions(tool, args).map_err(refuse)
}
}
impl crate::protocol::SymForgeServer {
#[doc(hidden)]
pub fn typical_calls_for_tests() -> Vec<(&'static str, &'static str)> {
TOOL_CONTRACTS
.iter()
.map(|c| (c.tool, c.typical_call))
.collect()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::cli::init::SYMFORGE_TOOL_NAMES;
use crate::protocol::SymForgeServer;
use std::collections::{BTreeMap, BTreeSet};
const CLIENT_PREFIX: &str = "mcp__symforge__";
fn schema_arguments() -> BTreeMap<String, BTreeSet<String>> {
SymForgeServer::tool_definitions()
.into_iter()
.map(|tool| {
let args = tool
.input_schema
.get("properties")
.and_then(|p| p.as_object())
.map(|p| p.keys().cloned().collect())
.unwrap_or_default();
(tool.name.to_string(), args)
})
.collect()
}
fn controlling_argument(when: &When) -> &'static str {
controller_of(when)
}
#[test]
fn every_registered_tool_has_exactly_one_contract() {
for client_name in SYMFORGE_TOOL_NAMES {
let name = client_name
.strip_prefix(CLIENT_PREFIX)
.unwrap_or(client_name);
let rows = TOOL_CONTRACTS.iter().filter(|c| c.tool == name).count();
assert_eq!(
rows, 1,
"tool `{name}` must have exactly one contract, has {rows}"
);
}
assert_eq!(
TOOL_CONTRACTS.len(),
SYMFORGE_TOOL_NAMES.len(),
"a contract names a tool that is not in SYMFORGE_TOOL_NAMES"
);
let router: BTreeSet<String> = schema_arguments().into_keys().collect();
let contracts: BTreeSet<String> =
TOOL_CONTRACTS.iter().map(|c| c.tool.to_string()).collect();
assert_eq!(
router, contracts,
"contracts differ from the registered router"
);
}
fn typical_args(contract: &ToolContract) -> Value {
fn fill(value: &mut Value) {
match value {
Value::String(s) if s.starts_with('{') && s.ends_with('}') => *s = "x".into(),
Value::Array(items) => items.iter_mut().for_each(fill),
Value::Object(map) => map.values_mut().for_each(fill),
_ => {}
}
}
let mut value: Value = serde_json::from_str(contract.typical_call).expect("typical call");
fill(&mut value);
value
}
#[test]
fn every_contract_row_has_one_strict_decoder_that_accepts_its_typical_call() {
for contract in TOOL_CONTRACTS {
let probes = PROBES
.iter()
.filter(|(n, _, _)| *n == contract.tool)
.count();
assert_eq!(probes, 1, "{}: needs exactly one decoder", contract.tool);
}
assert_eq!(PROBES.len(), TOOL_CONTRACTS.len());
for contract in TOOL_CONTRACTS {
let (_, probe, _) = PROBES.iter().find(|(n, _, _)| *n == contract.tool).unwrap();
let args = typical_args(contract);
if let Err(StrictError::Unknown(unknown)) = probe(args.clone()) {
panic!(
"{}: typical call has unknown keys {unknown:?}",
contract.tool
);
}
let mut bad = args;
bad.as_object_mut()
.unwrap()
.insert("zz_unknown".into(), Value::from(1));
assert!(
matches!(probe(bad), Err(StrictError::Unknown(ref u)) if u.paths() == ["zz_unknown"]),
"{}: an extra key must be reported",
contract.tool
);
}
}
#[test]
fn every_decoder_row_matches_the_published_schema_of_its_tool() {
let published = schema_arguments();
for (tool, _, schema) in PROBES {
let row: BTreeSet<String> = schema().into_iter().collect();
assert_eq!(
&row,
published
.get(*tool)
.unwrap_or_else(|| panic!("{tool}: not a registered tool")),
"{tool}: the decoder row's type differs from the handler's argument type"
);
}
let flattened: BTreeSet<String> = schema_names::<StelRequest>().into_iter().collect();
assert_eq!(&flattened, &published["symforge"]);
}
#[test]
fn lenient_wire_forms_normalise_like_the_decoders_read_them() {
let given = |v: Value| is_given(Some(&v));
for absent in [
Value::Null,
Value::from(""),
Value::from("0"),
Value::from("false"),
Value::from(false),
serde_json::json!([]),
] {
assert!(!given(absent.clone()), "{absent} must read as absent");
}
for present in [
Value::from("1"),
Value::from("true"),
Value::from(true),
Value::from(7),
] {
assert!(given(present.clone()), "{present} must read as given");
}
let args: JsonObject = serde_json::from_value(serde_json::json!({
"mode": "", "a": "1", "b": "0", "c": null
}))
.unwrap();
assert_eq!(current_value(&args, "mode"), ABSENT);
assert_eq!(current_value(&args, "a"), "true");
assert_eq!(current_value(&args, "b"), "false");
assert_eq!(current_value(&args, "c"), ABSENT);
assert_eq!(current_value(&args, "missing"), ABSENT);
}
#[test]
fn decode_strict_reports_nested_keys_and_honours_aliases() {
let nested = serde_json::json!({
"edits": [{ "path": "a.rs", "name": "f",
"operation": { "type": "delete" }, "zz": 1 }]
});
match decode_strict::<BatchEditInput>(nested) {
Err(StrictError::Unknown(u)) => assert_eq!(u.paths(), ["edits.0.zz"]),
other => panic!("nested key not reported: {:?}", other.err()),
}
let aliased = serde_json::json!({
"path": "a.rs", "symbol": "f", "body": "fn f() {}"
});
assert!(decode_strict::<ReplaceSymbolBodyInput>(aliased).is_ok());
let denied = serde_json::json!({ "targets": [{ "path": "a.rs", "zz": 1 }] });
match decode_strict::<GetSymbolInput>(denied) {
Err(StrictError::Unknown(u)) => assert_eq!(u.paths(), ["targets.0.zz"]),
other => panic!("deny_unknown_fields not located: {:?}", other.err()),
}
assert!(matches!(
decode_strict::<BatchEditInput>(serde_json::json!({ "dry_run": true })),
Err(StrictError::Decode(_))
));
}
#[test]
fn every_table_row_refuses_only_in_the_situation_it_names() {
let sensitive = Value::from("SENSITIVE_VALUE_123");
for contract in TOOL_CONTRACTS {
for c in contract.conditions {
let controller = controller_of(&c.applies_when);
let mut args = JsonObject::new();
args.insert(c.argument.to_string(), sensitive.clone());
let label = format!("{}: {}", contract.tool, c.argument);
match &c.applies_when {
When::Absent(_) => {
let mut with = args.clone();
with.insert(controller.to_string(), Value::from(1));
let text = apply_conditions(contract.tool, &mut with)
.expect_err(&format!("{label} with {controller}"));
assert!(
text.contains(c.argument) && text.contains(controller),
"{text}"
);
assert!(!text.contains("SENSITIVE_VALUE"), "{label}: echoed a value");
assert!(
apply_conditions(contract.tool, &mut args).is_ok(),
"{label}"
);
}
When::Unless(_, values) => {
for value in *values {
let mut with = args.clone();
with.insert(controller.to_string(), Value::from(*value));
let text = apply_conditions(contract.tool, &mut with)
.expect_err(&format!("{label} with {controller}={value}"));
assert!(
text.contains(c.argument) && text.contains(controller),
"{text}"
);
assert!(!text.contains("SENSITIVE_VALUE"), "{label}: echoed a value");
}
for other in ["Compact", "summary", "refs", "", "zz"] {
let mut with = args.clone();
with.insert(controller.to_string(), Value::from(other));
assert!(
apply_conditions(contract.tool, &mut with).is_ok(),
"{label}: refused for {controller}={other:?}"
);
}
assert!(
apply_conditions(contract.tool, &mut args).is_ok(),
"{label}"
);
}
other => panic!("{label}: a table row must be Absent or Unless, got {other:?}"),
}
}
}
}
#[test]
fn mode_selection_is_order_independent_and_conflicts_are_refused() {
const ROWS: &[Condition] = &[
Condition {
argument: "limit",
applies_when: When::Equals("flag", &["true"]),
on_mismatch: OnMismatch::SelectMode,
adaptive_with: None,
},
Condition {
argument: "kind",
applies_when: When::Equals("mode", &["refs", ABSENT]),
on_mismatch: OnMismatch::Refuse,
adaptive_with: None,
},
Condition {
argument: "dir",
applies_when: When::Equals("mode", &["impl"]),
on_mismatch: OnMismatch::SelectMode,
adaptive_with: None,
},
Condition {
argument: "extra",
applies_when: When::Equals("mode", &["other"]),
on_mismatch: OnMismatch::SelectMode,
adaptive_with: None,
},
Condition {
argument: "max_files",
applies_when: When::Equals("detail", &["full"]),
on_mismatch: OnMismatch::SelectMode,
adaptive_with: Some("max_tokens"),
},
Condition {
argument: "depth",
applies_when: When::Equals("detail", &["tree", "compact", ABSENT]),
on_mismatch: OnMismatch::Refuse,
adaptive_with: None,
},
];
let run = |json: serde_json::Value| {
let mut args: JsonObject = serde_json::from_value(json).unwrap();
let result = apply_rows("synthetic", ROWS, &mut args);
(result, args)
};
let (result, args) = run(serde_json::json!({ "limit": 5 }));
assert_eq!(result.unwrap()[0].argument, "limit");
assert_eq!(args["flag"], Value::Bool(true));
let (result, _) = run(serde_json::json!({ "limit": 5, "flag": "false" }));
assert!(result.unwrap_err().contains("flag"));
for json in [
serde_json::json!({ "dir": "auto", "kind": "call" }),
serde_json::json!({ "kind": "call", "dir": "auto" }),
] {
let text = run(json).0.unwrap_err();
assert!(text.contains("`dir` selected mode=\"impl\""), "{text}");
}
let text = run(serde_json::json!({ "max_files": 50, "depth": 3 }))
.0
.unwrap_err();
assert!(
text.contains("`max_files` selected detail=\"full\""),
"{text}"
);
let text = run(serde_json::json!({ "dir": 1, "extra": 1 }))
.0
.unwrap_err();
assert!(
text.contains("\"dir\"") && text.contains("\"extra\"") && text.contains("combined")
);
let (result, args) =
run(serde_json::json!({ "max_files": 5, "max_tokens": 100, "depth": 2 }));
assert!(result.unwrap().is_empty());
assert!(!args.contains_key("detail"));
}
#[test]
fn conditioned_arguments_name_their_controller_in_the_schema() {
let tools = SymForgeServer::tool_definitions();
for contract in TOOL_CONTRACTS {
let tool = tools.iter().find(|t| t.name == contract.tool).unwrap();
for c in contract.conditions {
let controller = controller_of(&c.applies_when);
let description = tool
.input_schema
.get("properties")
.and_then(|p| p.get(c.argument))
.and_then(|p| p.get("description"))
.and_then(Value::as_str)
.unwrap_or_else(|| {
panic!("{}: `{}` has no description", contract.tool, c.argument)
});
assert!(
description.contains(controller),
"{}: the description of `{}` must name `{controller}`: {description}",
contract.tool,
c.argument
);
}
}
}
#[test]
fn contract_arguments_are_in_the_published_schema() {
let schemas = schema_arguments();
for contract in TOOL_CONTRACTS {
let schema = &schemas[contract.tool];
let mut named: Vec<&str> = Vec::new();
for condition in contract.conditions {
named.push(condition.argument);
named.push(controlling_argument(&condition.applies_when));
}
named.extend(contract.explicit_arguments);
let typical: serde_json::Value = serde_json::from_str(contract.typical_call)
.unwrap_or_else(|e| panic!("{}: typical_call is not JSON: {e}", contract.tool));
let object = typical
.as_object()
.unwrap_or_else(|| panic!("{}: typical_call is not an object", contract.tool));
named.extend(object.keys().map(String::as_str));
for argument in named {
assert!(
schema.contains(argument),
"{}: `{argument}` is not in the tool's input schema {schema:?}",
contract.tool
);
}
assert!(
!contract.primary.is_empty(),
"{}: empty primary",
contract.tool
);
}
}
}