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