Skip to main content

vtcode_utility_tool_specs/
matrix.rs

1//! Passive schemas for durable local matrix control and attempt-owned reports.
2
3use serde_json::{Value, json};
4
5/// Worker-report outcome spellings.
6///
7/// This is deliberately a separate vocabulary from the snapshot
8/// `MatrixOutcome` enum: `"executed"` means the worker finished its
9/// instructions, not that final verification succeeded. The schema, report
10/// validation, and outcome parser all reference one table so they cannot drift.
11pub mod report_outcome {
12    pub const EXECUTED: &str = "executed";
13    pub const FAILED: &str = "failed";
14    pub const INTERRUPTED: &str = "interrupted";
15    pub const TIMED_OUT: &str = "timed_out";
16    pub const PERMISSION_DENIED: &str = "permission_denied";
17    pub const BUDGET_EXHAUSTED: &str = "budget_exhausted";
18
19    pub const ALL: [&str; 6] = [
20        EXECUTED,
21        FAILED,
22        INTERRUPTED,
23        TIMED_OUT,
24        PERMISSION_DENIED,
25        BUDGET_EXHAUSTED,
26    ];
27}
28
29/// Model-visible description of deterministic matrix orchestration.
30pub const MATRIX_DESCRIPTION: &str = "Manage a durable local task matrix. create persists an explicit specification without launching workers; start freezes it and begins scheduler-owned execution. status shows task/resource/verification progress; pause stops new dispatch, resume reconciles durable state before dispatch, retry requests a coordinator decision for a task, and cancel is terminal. report is worker-only: runtime supplies task and attempt identity and validates durable command evidence. Final success requires every declared check against the final workspace generation; worker summaries are insufficient.";
31
32/// Wire parameters; lifecycle validation and report authorization are runtime-owned.
33#[must_use]
34pub fn matrix_parameters() -> Value {
35    json!({
36        "type": "object",
37        "additionalProperties": false,
38        "required": ["action"],
39        "properties": {
40            "action": {"type": "string", "enum": ["create", "start", "status", "pause", "resume", "retry", "cancel", "report"]},
41            "matrix_id": {"type": "string", "minLength": 1, "description": "Coordinator controls: matrix to inspect or control."},
42            "task_id": {"type": "string", "minLength": 1, "description": "Coordinator retry only: task requiring another attempt. Reports cannot select a task."},
43            "spec": matrix_spec_schema(),
44            "outcome": {"type": "string", "enum": report_outcome::ALL, "description": "Worker report only; runtime authenticates the current assignment. executed means instructions finished, not final verification success."},
45            "evidence_ids": {"type": "array", "items": {"type": "string", "minLength": 1}, "description": "Worker report only: canonical durable evidence references; unrelated, stale, or cancelled commands cannot verify success."}
46        }
47    })
48}
49
50fn matrix_spec_schema() -> Value {
51    json!({
52        "type": "object",
53        "additionalProperties": false,
54        "required": ["id", "tasks", "resources"],
55        "properties": {
56            "id": {"type": "string", "minLength": 1},
57            "resources": resource_map_schema(),
58            "tasks": {"type": "array", "minItems": 1, "items": {
59                "type": "object", "additionalProperties": false,
60                "required": ["id", "instructions", "workspace", "access", "checks", "timeout_secs"],
61                "properties": {
62                    "id": {"type": "string", "minLength": 1, "description": "Stable unique task ID."},
63                    "instructions": {"type": "string", "minLength": 1},
64                    "dependencies": {"type": "array", "items": {"type": "string", "minLength": 1}, "default": []},
65                    "workspace": {"type": "string", "minLength": 1, "description": "Session-workspace-relative directory; use . for the root. Absolute paths, traversal, and symlink escapes are rejected."},
66                    "access": {"type": "string", "enum": ["read", "write"], "description": "Readers may overlap; a writer excludes every other matrix task."},
67                    "checks": {"type": "array", "minItems": 1, "items": {"type": "string", "minLength": 1}, "description": "Standalone verification commands rerun after execution against the final workspace generation."},
68                    "inputs": {"type": "array", "items": {"type": "string", "minLength": 1}, "default": [], "description": "Declared untracked input files relative to this task's workspace, included with tracked source in the verification fingerprint."},
69                    "resources": resource_map_schema(),
70                    "timeout_secs": {"type": "integer", "minimum": 1},
71                    "replay_safe": {"type": "boolean", "default": false, "description": "Allows one automatic interrupted/timeout retry only after owned work is confirmed stopped."}
72                }
73            }}
74        }
75    })
76}
77
78fn resource_map_schema() -> Value {
79    json!({"type": "object", "additionalProperties": {"type": "integer", "minimum": 1}, "description": "Named resource capacities on the spec, or required quantities on a task; all quantities are positive integers."})
80}
81
82#[cfg(test)]
83mod tests {
84    use super::*;
85
86    #[test]
87    fn matrix_schema_keeps_report_identity_runtime_owned() {
88        let schema = matrix_parameters();
89        assert_eq!(schema["additionalProperties"], false);
90        for forbidden in ["attempt_id", "worker_id", "thread_id", "generation"] {
91            assert!(schema["properties"].get(forbidden).is_none(), "{forbidden}");
92        }
93        assert!(
94            schema["properties"]["task_id"]["description"]
95                .as_str()
96                .unwrap()
97                .contains("retry only")
98        );
99        assert_eq!(
100            schema["properties"]["spec"]["properties"]["tasks"]["items"]["properties"]["replay_safe"]["default"],
101            false
102        );
103    }
104
105    #[test]
106    fn report_outcome_enum_matches_canonical_table() {
107        use super::report_outcome;
108        let schema = matrix_parameters();
109        let values = schema["properties"]["outcome"]["enum"].as_array().expect("outcome enum");
110        let expected: Vec<Value> = report_outcome::ALL.iter().map(|value| json!(value)).collect();
111        assert_eq!(*values, expected);
112    }
113
114    #[test]
115    fn matrix_schema_requires_checks_timeouts_and_positive_resources() {
116        let schema = matrix_parameters();
117        let task = &schema["properties"]["spec"]["properties"]["tasks"]["items"];
118        for field in ["id", "instructions", "workspace", "access", "checks", "timeout_secs"] {
119            assert!(task["required"].as_array().unwrap().contains(&json!(field)), "{field}");
120        }
121        assert_eq!(task["properties"]["checks"]["minItems"], 1);
122        assert_eq!(task["properties"]["resources"]["additionalProperties"]["minimum"], 1);
123        assert_eq!(task["properties"]["timeout_secs"]["minimum"], 1);
124    }
125}