File size: 6,012 Bytes
1d3f990
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! M4 Engine — GNU M4 wrapper with sandboxing and feedback

use crate::sandbox::{SandboxConfig, SandboxLimits};
use crate::feedback::FeedbackBuffer;
use anyhow::{Result, anyhow, Context};
use std::process::{Command, Stdio};
use std::io::Write;
use tracing::{debug, trace};

/// M4 Engine with sandboxing
pub struct M4Engine {
    config: SandboxConfig,
    feedback: FeedbackBuffer,
    expansion_count: usize,
}

impl M4Engine {
    pub fn new(config: SandboxConfig) -> Result<Self> {
        // Verify M4 exists
        Command::new("m4")
            .arg("--version")
            .output()
            .context("GNU M4 not found in PATH")?;

        Ok(Self {
            config,
            feedback: FeedbackBuffer::new(),
            expansion_count: 0,
        })
    }

    pub fn default_sandbox() -> Result<Self> {
        Self::new(SandboxConfig::default())
    }

    pub fn permissive_sandbox() -> Result<Self> {
        Self::new(SandboxConfig::permissive())
    }

    pub fn strict_sandbox() -> Result<Self> {
        Self::new(SandboxConfig::strict())
    }

    /// Expand M4 input with current feedback state
    pub fn expand(&mut self, input: &str) -> Result<String> {
        self.expansion_count += 1;
        debug!(count = self.expansion_count, input_len = input.len(), "M4 expansion");

        // Check recursion depth limit
        if self.expansion_count > self.config.limits.max_expansion_depth {
            return Err(anyhow!(
                "M4 recursion depth exceeded: {} > {}",
                self.expansion_count,
                self.config.limits.max_expansion_depth
            ));
        }

        // Construct full M4 input with feedback
        let full_input = self.construct_input(input)?;

        // Execute M4
        let output = self.execute_m4(&full_input)?;

        // Validate output size
        if output.len() > self.config.limits.max_output_size {
            return Err(anyhow!(
                "M4 output too large: {} > {}",
                output.len(),
                self.config.limits.max_output_size
            ));
        }

        trace!("M4 expansion succeeded");
        Ok(output)
    }

    /// Construct M4 input with sandbox prelude and feedback
    fn construct_input(&self, user_input: &str) -> Result<String> {
        let mut input = String::new();

        // 1. Sandbox prelude (deny dangerous builtins)
        for builtin in &self.config.limits.denied_builtins {
            input.push_str(&format!("define(`{}', `')\n", builtin));
        }

        // 2. Set recursion limit
        input.push_str(&format!(
            "define(`__M4_RECURSION_LIMIT', `{}')\n",
            self.config.limits.max_call_stack
        ));

        // 3. Feedback definitions from previous cells
        input.push_str(&self.feedback.get_definitions_code());

        // 4. User input
        input.push_str(user_input);
        input.push('\n');

        Ok(input)
    }

    /// Execute M4 via subprocess
    fn execute_m4(&self, input: &str) -> Result<String> {
        let mut child = Command::new("m4")
            .arg("-P") // Prefix builtins
            .arg("-E") // Fatal warnings
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn M4 process")?;

        // Write input
        {
            let stdin = child.stdin.as_mut().context("Failed to open M4 stdin")?;
            stdin.write_all(input.as_bytes()).context("Failed to write to M4")?;
        }

        // Wait for completion
        let output = child.wait_with_output().context("Failed to wait for M4")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(anyhow!("M4 expansion failed: {}", stderr));
        }

        Ok(String::from_utf8_lossy(&output.stdout).to_string())
    }

    /// Record output as feedback for next cell
    pub fn push_feedback(&mut self, output: String) {
        self.feedback.push_output(output);
    }

    /// Define a macro for next cell
    pub fn define(&mut self, name: String, value: String) {
        let def = format!("{}={}", name, value);
        self.feedback.push_definition(def);
    }

    /// Clear feedback buffer (reset for new execution ring)
    pub fn clear_feedback(&mut self) {
        self.feedback.clear();
    }

    pub fn expansion_count(&self) -> usize {
        self.expansion_count
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_engine_creation() {
        let result = M4Engine::default_sandbox();
        // May fail if M4 not installed in test environment
        let _ = result;
    }

    #[test]
    fn test_construct_input() {
        let engine = M4Engine {
            config: SandboxConfig::default(),
            feedback: FeedbackBuffer::new(),
            expansion_count: 0,
        };

        let input = engine.construct_input("test input").unwrap();
        assert!(input.contains("test input"));
    }

    #[test]
    fn test_feedback_integration() {
        let mut engine = M4Engine {
            config: SandboxConfig::default(),
            feedback: FeedbackBuffer::new(),
            expansion_count: 0,
        };

        engine.define("VAR".into(), "42".into());
        engine.push_feedback("output: 42".into());

        assert_eq!(engine.expansion_count(), 0);
    }

    #[test]
    fn test_recursion_limit() {
        let mut engine = M4Engine {
            config: SandboxConfig::strict(),
            feedback: FeedbackBuffer::new(),
            expansion_count: 0,
        };

        engine.expansion_count = 50;
        let result = engine.expand("test");
        // Will fail due to recursion limit
        assert!(result.is_err());
    }
}