weavatrix_rust/operations/
mod.rs1mod architecture;
2mod build;
3mod catalog;
4mod graph;
5mod health;
6mod history;
7mod memory;
8mod semantic;
9mod source;
10mod syntax;
11mod token_budget;
12mod transport_contracts;
13mod vector;
14mod workflow;
15
16pub use catalog::{ToolDefinition, ToolProfile, catalog, catalog_for_profile};
17
18use crate::engine::{RepositoryState, Weavatrix};
19use blazingly_json::{Value, json};
20
21#[allow(clippy::needless_pass_by_value)]
28pub fn call(weavatrix: &mut Weavatrix, name: &str, arguments: Value) -> Result<Value, String> {
29 token_budget::reject_unsupported(name, &arguments)?;
30 if name == "trace_api_contract" {
31 return workflow::trace_api_cached(weavatrix, &arguments);
32 }
33 let state = weavatrix.state();
34 match name {
35 "graph_stats" => Ok(graph::stats(state)),
36 "get_node" => graph::get_node(state, &arguments),
37 "get_neighbors" => graph::neighbors(state, &arguments),
38 "query_graph" => graph::query(state, &arguments),
39 "god_nodes" => Ok(graph::hubs(state, &arguments)),
40 "shortest_path" => graph::path(state, &arguments),
41 "get_dependents" => graph::dependents(state, &arguments),
42 "change_impact" => workflow::change_impact(state, &arguments),
43 "map_stacktrace" => workflow::map_stacktrace(state, &arguments),
44 "select_tests" => workflow::select_tests(state, &arguments),
45 "git_history" => history::history(state, &arguments),
46 "cross_repo_git" => history::cross_repo(state, &arguments),
47 "verified_change" => workflow::verified_change(state, &arguments),
48 "get_community" | "list_communities" => graph::communities(state, &arguments),
49 "search_code" => source::search(state, &arguments),
50 "read_source" => source::read_source(state, &arguments),
51 "inspect_symbol" => source::inspect(state, &arguments),
52 "context_bundle" => source::context(state, &arguments),
53 "find_duplicates" => health::duplicates(state, &arguments),
54 "find_dead_code" => health::dead_code(state, &arguments),
55 "run_audit" => health::audit(state, &arguments),
56 "coverage_map" => health::coverage(state, &arguments),
57 "hot_path_review" => health::hot_paths(state, &arguments),
58 "module_map" => graph::module_map(state, &arguments),
59 "build_graph" => build::build_graph(state, &arguments),
60 "list_endpoints" => graph::endpoints(state, &arguments),
61 "trace_endpoint" => graph::trace_endpoint(state, &arguments),
62 "graph_diff" => history::graph_diff(state, &arguments),
63 "get_architecture_contract" => architecture::contract(state, &arguments),
64 "prepare_change" => architecture::prepare(state, &arguments),
65 "verify_architecture" => architecture::verify(state),
66 "explain_architecture_violation" => architecture::explain(state, &arguments),
67 "propose_architecture_exception" => architecture::propose_exception(state, &arguments),
68 "semantic_link" => semantic::semantic_link(state, &arguments),
69 "vector_search" => vector::search(&arguments),
70 "seo_link_suggestions" => semantic::seo_links(state, &arguments),
71 "memory_context" => memory::context(state, &arguments),
72 "rebuild_graph" => {
73 let before = graph::stats(state);
74 weavatrix.rebuild().map_err(|error| error.to_string())?;
75 Ok(json!({"before": before, "after": graph::stats(weavatrix.state())}))
76 }
77 "open_repo" => {
78 let path = arg_str(&arguments, "path")?.to_owned();
79 let should_build = arg_bool(&arguments, "build").unwrap_or(true);
80 let graph_built = weavatrix
81 .open_repository_with_build(&path, should_build)
82 .map_err(|error| error.to_string())?;
83 Ok(json!({
84 "repository": weavatrix.state().root(),
85 "built": graph_built,
86 "graph": graph::stats(weavatrix.state())
87 }))
88 }
89 "list_known_repos" => Ok(json!({
90 "repositories": weavatrix.known_roots().collect::<Vec<_>>()
91 })),
92 _ => Err(format!("unknown tool: {name}")),
93 }
94}
95
96fn arg_value<'value, T>(
97 args: &'value Value,
98 key: &str,
99 expected: &str,
100 extract: impl FnOnce(&'value Value) -> Option<T>,
101) -> Result<T, String> {
102 args.get(key)
103 .and_then(extract)
104 .ok_or_else(|| format!("{key} must be {expected}"))
105}
106
107pub(crate) fn arg_str<'value>(args: &'value Value, key: &str) -> Result<&'value str, String> {
108 arg_value(args, key, "a string", Value::as_str)
109}
110
111pub(crate) fn arg_u64(args: &Value, key: &str) -> Result<u64, String> {
112 arg_value(args, key, "a non-negative integer", Value::as_u64)
113}
114
115pub(crate) fn arg_bool(args: &Value, key: &str) -> Result<bool, String> {
116 arg_value(args, key, "a boolean", Value::as_bool)
117}
118
119pub(crate) fn optional_str<'value>(
120 args: &'value Value,
121 key: &str,
122) -> Result<Option<&'value str>, String> {
123 args.get(key)
124 .map(|value| {
125 value
126 .as_str()
127 .ok_or_else(|| format!("{key} must be a string"))
128 })
129 .transpose()
130}
131
132pub(crate) fn optional_u64(args: &Value, key: &str) -> Result<Option<u64>, String> {
133 args.get(key)
134 .map(|value| {
135 value
136 .as_u64()
137 .ok_or_else(|| format!("{key} must be a non-negative integer"))
138 })
139 .transpose()
140}
141
142pub(crate) fn optional_bool(args: &Value, key: &str) -> Result<Option<bool>, String> {
143 args.get(key)
144 .map(|value| {
145 value
146 .as_bool()
147 .ok_or_else(|| format!("{key} must be a boolean"))
148 })
149 .transpose()
150}
151
152pub(crate) fn require_graph_precision(args: &Value) -> Result<(), String> {
153 let Some(precision) = optional_str(args, "precision")? else {
154 return Ok(());
155 };
156 if precision == "graph" {
157 return Ok(());
158 }
159 Err(format!(
160 "precision '{precision}' is unsupported; this operation supports only 'graph' bounded static precision"
161 ))
162}
163
164#[cfg(any(feature = "semantic", feature = "vector"))]
165fn vector_values(value: &Value, array_error: &str) -> Result<Vec<f32>, String> {
166 value
167 .as_array()
168 .ok_or_else(|| array_error.to_owned())?
169 .iter()
170 .map(|value| {
171 let value = value
172 .as_f64()
173 .filter(|value| value.is_finite())
174 .ok_or_else(|| "vector value must be finite".to_owned())?;
175 if !(f64::from(f32::MIN)..=f64::from(f32::MAX)).contains(&value) {
176 return Err("vector value is outside finite f32 range".to_owned());
177 }
178 value
179 .to_string()
180 .parse::<f32>()
181 .map_err(|error| format!("invalid vector value: {error}"))
182 })
183 .collect()
184}
185
186pub(crate) fn node_path(node: &weavatrix_graph::Node) -> Option<&str> {
188 node.span
189 .as_ref()
190 .map(|span| span.file.as_str())
191 .or_else(|| (node.kind == weavatrix_graph::NodeKind::File).then_some(node.label.as_str()))
192}
193
194pub(crate) fn node_is_visible(state: &RepositoryState, slot: usize, args: &Value) -> bool {
201 let index = weavatrix_graph::NodeIndex::new(u32::try_from(slot).unwrap_or(u32::MAX));
202 let Some(node) = state.graph().node_at(index) else {
203 return true;
204 };
205 if node_path(node).is_some() {
206 return evidence_node_is_visible(node, args);
207 }
208 let mut declared = false;
212 for edge in state.graph().incoming_at(index) {
213 let Some(source) = state.graph().node(edge.source.as_str()) else {
214 continue;
215 };
216 if node_path(source).is_none() {
217 continue;
218 }
219 declared = true;
220 if evidence_node_is_visible(source, args) {
221 return true;
222 }
223 }
224 !declared
227}
228
229fn evidence_node_is_visible(node: &weavatrix_graph::Node, args: &Value) -> bool {
230 if matches!(
231 node.attributes.get("test_only"),
232 Some(weavatrix_graph::AttributeValue::Bool(true))
233 ) {
234 return args.get("include_tests").and_then(Value::as_bool) == Some(true);
235 }
236 node_path(node).is_none_or(|path| health::path_is_visible(path, args))
237}