Skip to main content

vtcode_core/tools/registry/executors/
matrix.rs

1use super::ToolRegistry;
2use anyhow::{Context, Result, ensure};
3use futures::future::BoxFuture;
4use serde_json::Value;
5use std::sync::atomic::Ordering;
6
7impl ToolRegistry {
8    pub(crate) fn matrix_worker_outcome(&self) -> Option<crate::exec::events::matrix::MatrixOutcome> {
9        self.matrix_worker.read().as_ref().and_then(|worker| worker.reported_outcome())
10    }
11    pub(crate) fn has_matrix_access(&self) -> bool {
12        self.matrix_coordinator.load(Ordering::Acquire) || self.matrix_worker.read().is_some()
13    }
14    pub(crate) fn set_matrix_worker(&self, context: crate::subagents::matrix::MatrixWorkerContext) {
15        *self.matrix_worker.write() = Some(context);
16    }
17    pub fn set_matrix_coordinator(&self, active: bool) {
18        self.matrix_coordinator.store(active, Ordering::Release);
19    }
20    pub(crate) fn matrix_executor(&self, args: Value) -> BoxFuture<'_, Result<Value>> {
21        Box::pin(async move {
22            let worker = self.matrix_worker.read().clone();
23            if let Some(worker) = worker {
24                if let Some(evidence) = args.get("evidence_ids") {
25                    let ids = evidence
26                        .as_array()
27                        .context("evidence_ids must be an array")?
28                        .iter()
29                        .map(|id| {
30                            id.as_str()
31                                .filter(|id| !id.is_empty())
32                                .map(str::to_owned)
33                                .context("evidence IDs must be nonempty strings")
34                        })
35                        .collect::<Result<Vec<_>>>()?;
36                    if !ids.is_empty() {
37                        let validator = self
38                            .harness_context
39                            .decision_validator
40                            .read()
41                            .clone()
42                            .context("canonical worker evidence validator unavailable")?;
43                        let task = self
44                            .harness_context_snapshot()
45                            .task_id
46                            .context("worker task identity unavailable")?;
47                        validator(task, ids).await?;
48                    }
49                }
50                return worker.report(args);
51            }
52            ensure!(
53                self.matrix_coordinator.load(Ordering::Acquire),
54                "matrix control requires selected coordinator role"
55            );
56            let controller = self.subagent_controller().context("matrix requires a subagent controller")?;
57            controller.matrix_control(args).await
58        })
59    }
60    pub(crate) fn enforce_matrix_role(&self, canonical: &str) -> Result<()> {
61        if self.matrix_worker.read().is_some() {
62            ensure!(!crate::subagents::is_subagent_tool(canonical), "nested delegation is disabled for matrix workers");
63        } else if self.matrix_coordinator.load(Ordering::Acquire) {
64            ensure!(
65                [
66                    "matrix",
67                    "agent",
68                    "spawn_agent",
69                    "wait_agent",
70                    "close_agent",
71                    "request_user_input",
72                    "record_decision",
73                    "task_tracker"
74                ]
75                .contains(&canonical),
76                "coordinator delegates discovery, execution, edits and verification"
77            );
78        }
79        Ok(())
80    }
81}