Skip to main content

glass_dev/
kernels.rs

1//! Named persistent execution kernels for Glass Dev.
2
3use rusqlite::types::ValueRef;
4use serde::{Deserialize, Serialize};
5use serde_json::{Value, json};
6use std::collections::{BTreeMap, BTreeSet};
7use std::fmt;
8use std::io::{BufRead, BufReader, Write};
9use std::path::{Path, PathBuf};
10use std::process::{Child, ChildStdin, Command, Stdio};
11use std::sync::mpsc::{self, Receiver, RecvTimeoutError, TrySendError};
12use std::thread::JoinHandle;
13use std::time::{Duration, Instant};
14
15const MAX_KERNEL_OUTPUT_BYTES: usize = 1024 * 1024;
16const MAX_KERNEL_CODE_BYTES: usize = 512 * 1024;
17const MAX_KERNEL_SESSIONS: usize = 32;
18const MAX_KERNEL_MESSAGES: usize = 64;
19const MAX_KERNEL_TOOL_CALLS: usize = 32;
20const DEFAULT_KERNEL_TIMEOUT: Duration = Duration::from_secs(30);
21
22const PYTHON_WORKER: &str = r#"
23import ast, contextlib, io, json, sys, traceback
24class GlassBindings:
25    def __init__(self): self.sequence = 0
26    def call(self, tool, arguments=None):
27        self.sequence += 1
28        request_id = "python-%d" % self.sequence
29        sys.__stdout__.write(json.dumps({"kind":"toolCall", "id":request_id, "tool":tool, "arguments":arguments or {}}, ensure_ascii=True) + "\n")
30        sys.__stdout__.flush()
31        response = json.loads(sys.__stdin__.readline())
32        if response.get("kind") != "toolResult" or response.get("id") != request_id:
33            raise RuntimeError("invalid Glass tool response")
34        if not response.get("ok"):
35            raise RuntimeError(response.get("error", "Glass tool call failed"))
36        return response.get("result")
37state = {"__name__": "__glass_kernel__"}
38state["glass"] = GlassBindings()
39for line in sys.stdin:
40    try:
41        request = json.loads(line)
42        source = request["code"]
43        output = io.StringIO()
44        result = None
45        with contextlib.redirect_stdout(output), contextlib.redirect_stderr(output):
46            tree = ast.parse(source, mode="exec")
47            if tree.body and isinstance(tree.body[-1], ast.Expr):
48                prefix = ast.Module(body=tree.body[:-1], type_ignores=[])
49                exec(compile(prefix, "<glass>", "exec"), state, state)
50                result = eval(compile(ast.Expression(tree.body[-1].value), "<glass>", "eval"), state, state)
51            else:
52                exec(compile(tree, "<glass>", "exec"), state, state)
53        response = {"kind":"executionResult", "ok": True, "output": output.getvalue(), "value": repr(result) if result is not None else None}
54    except Exception:
55        response = {"kind":"executionResult", "ok": False, "output": "", "error": traceback.format_exc(limit=16)}
56    print(json.dumps(response, ensure_ascii=True), flush=True)
57"#;
58
59const JAVASCRIPT_WORKER: &str = r#"
60const readline = require('readline');
61const vm = require('vm');
62const context = vm.createContext({});
63let toolSequence = 0;
64const pendingTools = new Map();
65context.glass = {
66  call: (tool, arguments = {}) => new Promise((resolve, reject) => {
67    const id = `javascript-${++toolSequence}`;
68    pendingTools.set(id, { resolve, reject });
69    process.stdout.write(JSON.stringify({ kind: 'toolCall', id, tool, arguments }) + '\n');
70  }),
71};
72const input = readline.createInterface({ input: process.stdin, crlfDelay: Infinity });
73input.on('line', async (line) => {
74  let request;
75  try {
76    request = JSON.parse(line);
77    if (request.kind === 'toolResult') {
78      const pending = pendingTools.get(request.id);
79      if (!pending) throw new Error('unknown Glass tool response');
80      pendingTools.delete(request.id);
81      if (request.ok) pending.resolve(request.result);
82      else pending.reject(new Error(request.error || 'Glass tool call failed'));
83      return;
84    }
85    const output = [];
86    context.console = {
87      log: (...values) => output.push(values.map(String).join(' ')),
88      error: (...values) => output.push(values.map(String).join(' ')),
89    };
90    let value = new vm.Script(request.code, { filename: '<glass>' })
91      .runInContext(context, { timeout: request.timeoutMs });
92    if (value && typeof value.then === 'function') value = await value;
93    process.stdout.write(JSON.stringify({ kind: 'executionResult', ok: true, output: output.join('\n') + (output.length ? '\n' : ''), value: value === undefined ? null : String(value) }) + '\n');
94  } catch (error) {
95    process.stdout.write(JSON.stringify({ kind: 'executionResult', ok: false, output: '', error: String(error && error.stack || error) }) + '\n');
96  }
97});
98"#;
99
100pub type KernelResult<T> = Result<T, KernelError>;
101
102#[derive(Debug)]
103pub enum KernelError {
104    Io(std::io::Error),
105    Sql(rusqlite::Error),
106    InvalidInput(String),
107    Unavailable(String),
108    Protocol(String),
109    Execution(String),
110    Timeout(String),
111}
112
113impl fmt::Display for KernelError {
114    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
115        match self {
116            Self::Io(error) => write!(formatter, "kernel I/O error: {error}"),
117            Self::Sql(error) => write!(formatter, "SQL kernel error: {error}"),
118            Self::InvalidInput(message) => write!(formatter, "invalid kernel input: {message}"),
119            Self::Unavailable(message) => write!(formatter, "kernel unavailable: {message}"),
120            Self::Protocol(message) => write!(formatter, "kernel protocol error: {message}"),
121            Self::Execution(message) => write!(formatter, "kernel execution failed: {message}"),
122            Self::Timeout(message) => write!(formatter, "kernel execution timed out: {message}"),
123        }
124    }
125}
126
127impl std::error::Error for KernelError {}
128
129impl From<std::io::Error> for KernelError {
130    fn from(error: std::io::Error) -> Self {
131        Self::Io(error)
132    }
133}
134
135impl From<rusqlite::Error> for KernelError {
136    fn from(error: rusqlite::Error) -> Self {
137        Self::Sql(error)
138    }
139}
140
141#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
142#[serde(rename_all = "camelCase")]
143pub enum KernelKind {
144    Python,
145    JavaScript,
146    Shell,
147    Sql,
148}
149
150impl KernelKind {
151    /// Human-readable label for terminal and TUI surfaces.
152    pub fn label(&self) -> &'static str {
153        match self {
154            Self::Python => "python",
155            Self::JavaScript => "javascript",
156            Self::Shell => "shell",
157            Self::Sql => "sql",
158        }
159    }
160}
161
162#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
163#[serde(rename_all = "camelCase")]
164pub enum KernelState {
165    Ready,
166    Failed,
167    Stopped,
168}
169
170impl KernelState {
171    /// Human-readable label for terminal and TUI surfaces.
172    pub fn label(&self) -> &'static str {
173        match self {
174            Self::Ready => "ready",
175            Self::Failed => "failed",
176            Self::Stopped => "stopped",
177        }
178    }
179}
180
181#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
182#[serde(rename_all = "camelCase")]
183pub struct KernelSnapshot {
184    pub name: String,
185    pub kind: KernelKind,
186    pub state: KernelState,
187    pub executions: u64,
188    pub actor_id: String,
189    pub workspace_revision: u64,
190    #[serde(default)]
191    pub capabilities: Vec<String>,
192    #[serde(default)]
193    pub mutation_authority: bool,
194}
195
196#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
197#[serde(rename_all = "camelCase")]
198pub struct KernelExecution {
199    pub name: String,
200    pub kind: KernelKind,
201    pub sequence: u64,
202    pub actor_id: String,
203    pub initiator_id: String,
204    pub executor_id: String,
205    pub workspace_revision: u64,
206    pub duration_ms: u64,
207    pub output: String,
208    pub value: Value,
209    pub truncated: bool,
210    pub tool_calls: u64,
211}
212
213#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
214#[serde(rename_all = "camelCase")]
215pub struct KernelToolCall {
216    pub id: String,
217    pub tool: String,
218    #[serde(default)]
219    pub arguments: Value,
220}
221
222enum KernelBackend {
223    Json(JsonProcessKernel),
224    Shell(ShellKernel),
225    Sql(SqlKernel),
226}
227
228struct KernelSession {
229    snapshot: KernelSnapshot,
230    backend: KernelBackend,
231    capabilities: BTreeSet<String>,
232}
233
234pub struct KernelManager {
235    root: PathBuf,
236    sessions: BTreeMap<String, KernelSession>,
237}
238
239impl KernelManager {
240    pub fn new(root: impl AsRef<Path>) -> KernelResult<Self> {
241        Ok(Self {
242            root: root.as_ref().canonicalize()?,
243            sessions: BTreeMap::new(),
244        })
245    }
246
247    pub fn start(&mut self, name: &str, kind: KernelKind, actor_id: &str) -> KernelResult<()> {
248        self.start_governed(name, kind, actor_id, &[], false)
249    }
250
251    pub fn start_governed(
252        &mut self,
253        name: &str,
254        kind: KernelKind,
255        actor_id: &str,
256        capabilities: &[String],
257        mutation_authority: bool,
258    ) -> KernelResult<()> {
259        validate_name(name, "kernel")?;
260        validate_actor_id(actor_id, "kernel actor")?;
261        let capabilities = validate_capabilities(capabilities)?;
262        if self.sessions.contains_key(name) {
263            return Err(KernelError::InvalidInput(format!(
264                "kernel {name} already exists"
265            )));
266        }
267        if self.sessions.len() == MAX_KERNEL_SESSIONS {
268            return Err(KernelError::InvalidInput(format!(
269                "kernel session limit is {MAX_KERNEL_SESSIONS}"
270            )));
271        }
272        let backend = match kind {
273            KernelKind::Python => KernelBackend::Json(JsonProcessKernel::spawn(
274                &self.root,
275                if cfg!(windows) { "python" } else { "python3" },
276                &["-u", "-c", PYTHON_WORKER],
277            )?),
278            KernelKind::JavaScript => KernelBackend::Json(JsonProcessKernel::spawn(
279                &self.root,
280                "node",
281                &["-e", JAVASCRIPT_WORKER],
282            )?),
283            KernelKind::Shell => KernelBackend::Shell(ShellKernel::spawn(&self.root)?),
284            KernelKind::Sql => KernelBackend::Sql(SqlKernel::new()?),
285        };
286        self.sessions.insert(
287            name.to_string(),
288            KernelSession {
289                snapshot: KernelSnapshot {
290                    name: name.to_string(),
291                    kind,
292                    state: KernelState::Ready,
293                    executions: 0,
294                    actor_id: actor_id.to_string(),
295                    workspace_revision: 0,
296                    capabilities: capabilities.iter().cloned().collect(),
297                    mutation_authority,
298                },
299                backend,
300                capabilities,
301            },
302        );
303        Ok(())
304    }
305
306    pub fn execute(
307        &mut self,
308        name: &str,
309        code: &str,
310        actor_id: &str,
311        workspace_revision: u64,
312        timeout: Option<Duration>,
313    ) -> KernelResult<KernelExecution> {
314        self.execute_with_tools(name, code, actor_id, workspace_revision, timeout, |_| {
315            Err(KernelError::Execution(
316                "kernel has no governed Glass tool dispatcher".into(),
317            ))
318        })
319    }
320
321    pub fn execute_with_tools(
322        &mut self,
323        name: &str,
324        code: &str,
325        initiator_id: &str,
326        workspace_revision: u64,
327        timeout: Option<Duration>,
328        mut dispatch: impl FnMut(&KernelToolCall) -> KernelResult<Value>,
329    ) -> KernelResult<KernelExecution> {
330        validate_code(code)?;
331        validate_actor_id(initiator_id, "kernel initiator")?;
332        let timeout = timeout.unwrap_or(DEFAULT_KERNEL_TIMEOUT);
333        if timeout.is_zero() || timeout > Duration::from_secs(600) {
334            return Err(KernelError::InvalidInput(
335                "kernel timeout must be between 1 ms and 600 seconds".into(),
336            ));
337        }
338        let session = self
339            .sessions
340            .get_mut(name)
341            .ok_or_else(|| KernelError::InvalidInput(format!("unknown kernel {name}")))?;
342        if session.snapshot.state != KernelState::Ready {
343            return Err(KernelError::Execution(format!(
344                "kernel {name} is not ready"
345            )));
346        }
347        let started = Instant::now();
348        let executor_id = format!("kernel:{name}");
349        let capabilities = session.capabilities.clone();
350        let mut tool_calls = 0_u64;
351        let mut governed_dispatch = |call: &KernelToolCall| {
352            validate_name(&call.id, "kernel tool call ID")?;
353            tool_calls = tool_calls.saturating_add(1);
354            if tool_calls > MAX_KERNEL_TOOL_CALLS as u64 {
355                return Err(KernelError::Execution(format!(
356                    "kernel execution exceeded {MAX_KERNEL_TOOL_CALLS} Glass tool calls"
357                )));
358            }
359            if call.tool.starts_with("glass.eval.") {
360                return Err(KernelError::Execution(
361                    "recursive glass.eval tool calls are forbidden".into(),
362                ));
363            }
364            if !capabilities.contains(&call.tool) {
365                return Err(KernelError::Execution(format!(
366                    "kernel capability {} was not granted",
367                    call.tool
368                )));
369            }
370            dispatch(call)
371        };
372        let result = match &mut session.backend {
373            KernelBackend::Json(kernel) => kernel.execute(code, timeout, &mut governed_dispatch),
374            KernelBackend::Shell(kernel) => kernel.execute(code, timeout, &mut governed_dispatch),
375            KernelBackend::Sql(kernel) => kernel.execute(code, &mut governed_dispatch),
376        };
377        let (output, value, truncated) = match result {
378            Ok(result) => result,
379            Err(error) => {
380                if matches!(error, KernelError::Protocol(_) | KernelError::Timeout(_)) {
381                    session.snapshot.state = KernelState::Failed;
382                }
383                return Err(error);
384            }
385        };
386        session.snapshot.executions = session
387            .snapshot
388            .executions
389            .checked_add(1)
390            .ok_or_else(|| KernelError::Protocol("kernel sequence overflowed".into()))?;
391        session.snapshot.actor_id = executor_id.clone();
392        session.snapshot.workspace_revision = workspace_revision;
393        Ok(KernelExecution {
394            name: name.to_string(),
395            kind: session.snapshot.kind,
396            sequence: session.snapshot.executions,
397            actor_id: executor_id.clone(),
398            initiator_id: initiator_id.to_string(),
399            executor_id,
400            workspace_revision,
401            duration_ms: started.elapsed().as_millis() as u64,
402            output,
403            value,
404            truncated,
405            tool_calls,
406        })
407    }
408
409    pub fn snapshots(&self) -> impl Iterator<Item = &KernelSnapshot> {
410        self.sessions.values().map(|session| &session.snapshot)
411    }
412
413    pub fn snapshot(&self, name: &str) -> Option<&KernelSnapshot> {
414        self.sessions.get(name).map(|session| &session.snapshot)
415    }
416
417    pub fn stop(&mut self, name: &str) -> KernelResult<KernelSnapshot> {
418        let mut session = self
419            .sessions
420            .remove(name)
421            .ok_or_else(|| KernelError::InvalidInput(format!("unknown kernel {name}")))?;
422        session.stop();
423        Ok(session.snapshot.clone())
424    }
425
426    pub fn cancel(&mut self, name: &str) -> KernelResult<KernelSnapshot> {
427        let session = self
428            .sessions
429            .get_mut(name)
430            .ok_or_else(|| KernelError::InvalidInput(format!("unknown kernel {name}")))?;
431        session.stop();
432        session.snapshot.state = KernelState::Failed;
433        Ok(session.snapshot.clone())
434    }
435
436    pub fn reset(&mut self, name: &str, actor_id: &str) -> KernelResult<()> {
437        let session = self
438            .sessions
439            .get(name)
440            .ok_or_else(|| KernelError::InvalidInput(format!("unknown kernel {name}")))?;
441        let kind = session.snapshot.kind;
442        let capabilities = session.snapshot.capabilities.clone();
443        let mutation_authority = session.snapshot.mutation_authority;
444        self.stop(name)?;
445        self.start_governed(name, kind, actor_id, &capabilities, mutation_authority)
446    }
447}
448
449impl KernelSession {
450    fn stop(&mut self) {
451        match &mut self.backend {
452            KernelBackend::Json(kernel) => kernel.stop(),
453            KernelBackend::Shell(kernel) => kernel.stop(),
454            KernelBackend::Sql(_) => {}
455        }
456        self.snapshot.state = KernelState::Stopped;
457    }
458}
459
460impl Drop for KernelSession {
461    fn drop(&mut self) {
462        self.stop();
463    }
464}
465
466struct JsonProcessKernel {
467    child: Child,
468    stdin: ChildStdin,
469    responses: Receiver<KernelResult<Value>>,
470    reader: Option<JoinHandle<()>>,
471    stderr_drain: Option<JoinHandle<()>>,
472}
473
474impl JsonProcessKernel {
475    fn spawn(root: &Path, program: &str, arguments: &[&str]) -> KernelResult<Self> {
476        let mut child = Command::new(program)
477            .args(arguments)
478            .current_dir(root)
479            .stdin(Stdio::piped())
480            .stdout(Stdio::piped())
481            .stderr(Stdio::piped())
482            .spawn()
483            .map_err(|error| {
484                if error.kind() == std::io::ErrorKind::NotFound {
485                    KernelError::Unavailable(format!("{program} was not found"))
486                } else {
487                    KernelError::Io(error)
488                }
489            })?;
490        let stdin = child
491            .stdin
492            .take()
493            .ok_or_else(|| KernelError::Protocol("kernel stdin was unavailable".into()))?;
494        let stdout = child
495            .stdout
496            .take()
497            .ok_or_else(|| KernelError::Protocol("kernel stdout was unavailable".into()))?;
498        let stderr = child
499            .stderr
500            .take()
501            .ok_or_else(|| KernelError::Protocol("kernel stderr was unavailable".into()))?;
502        let (sender, responses) = mpsc::sync_channel(MAX_KERNEL_MESSAGES);
503        let reader = std::thread::spawn(move || {
504            for line in BufReader::new(stdout).lines() {
505                let response = line.map_err(KernelError::Io).and_then(|line| {
506                    if line.len() > MAX_KERNEL_OUTPUT_BYTES {
507                        return Err(KernelError::Protocol(
508                            "kernel response exceeded the output limit".into(),
509                        ));
510                    }
511                    serde_json::from_str(&line).map_err(|error| {
512                        KernelError::Protocol(format!("invalid kernel response: {error}"))
513                    })
514                });
515                match sender.try_send(response) {
516                    Ok(()) => {}
517                    Err(TrySendError::Full(_) | TrySendError::Disconnected(_)) => break,
518                }
519            }
520        });
521        let stderr_drain = std::thread::spawn(move || {
522            let mut stderr = stderr;
523            let _ = std::io::copy(&mut stderr, &mut std::io::sink());
524        });
525        Ok(Self {
526            child,
527            stdin,
528            responses,
529            reader: Some(reader),
530            stderr_drain: Some(stderr_drain),
531        })
532    }
533
534    fn execute(
535        &mut self,
536        code: &str,
537        timeout: Duration,
538        dispatch: &mut impl FnMut(&KernelToolCall) -> KernelResult<Value>,
539    ) -> KernelResult<KernelOutput> {
540        serde_json::to_writer(
541            &mut self.stdin,
542            &json!({"code": code, "timeoutMs": timeout.as_millis()}),
543        )
544        .map_err(|error| {
545            KernelError::Protocol(format!("could not encode kernel input: {error}"))
546        })?;
547        self.stdin.write_all(b"\n")?;
548        self.stdin.flush()?;
549        let deadline = Instant::now() + timeout;
550        let response = loop {
551            let remaining = deadline.saturating_duration_since(Instant::now());
552            let response = match self.responses.recv_timeout(remaining) {
553                Ok(response) => response?,
554                Err(RecvTimeoutError::Timeout) => {
555                    self.stop();
556                    return Err(KernelError::Timeout(format!(
557                        "execution exceeded {} ms",
558                        timeout.as_millis()
559                    )));
560                }
561                Err(RecvTimeoutError::Disconnected) => {
562                    return Err(KernelError::Protocol(
563                        "kernel response stream closed".into(),
564                    ));
565                }
566            };
567            if response.get("kind").and_then(Value::as_str) == Some("toolCall") {
568                let call: KernelToolCall = serde_json::from_value(response).map_err(|error| {
569                    KernelError::Protocol(format!("invalid kernel tool call: {error}"))
570                })?;
571                let dispatched = dispatch(&call);
572                let response = match dispatched {
573                    Ok(result) => {
574                        json!({"kind":"toolResult","id":call.id,"ok":true,"result":result})
575                    }
576                    Err(error) => {
577                        json!({"kind":"toolResult","id":call.id,"ok":false,"error":error.to_string()})
578                    }
579                };
580                serde_json::to_writer(&mut self.stdin, &response).map_err(|error| {
581                    KernelError::Protocol(format!("could not encode kernel tool result: {error}"))
582                })?;
583                self.stdin.write_all(b"\n")?;
584                self.stdin.flush()?;
585                continue;
586            }
587            if response.get("kind").and_then(Value::as_str) != Some("executionResult") {
588                return Err(KernelError::Protocol(
589                    "kernel response has an unknown message kind".into(),
590                ));
591            }
592            break response;
593        };
594        if response.get("ok").and_then(Value::as_bool) != Some(true) {
595            return Err(KernelError::Execution(
596                response
597                    .get("error")
598                    .and_then(Value::as_str)
599                    .unwrap_or("kernel returned an unsuccessful response")
600                    .to_string(),
601            ));
602        }
603        bounded_output(
604            response
605                .get("output")
606                .and_then(Value::as_str)
607                .unwrap_or_default(),
608            response.get("value").cloned().unwrap_or(Value::Null),
609        )
610    }
611
612    fn stop(&mut self) {
613        if self.child.try_wait().ok().flatten().is_none() {
614            let _ = self.child.kill();
615            let _ = self.child.wait();
616        }
617        if let Some(reader) = self.reader.take() {
618            let _ = reader.join();
619        }
620        if let Some(stderr_drain) = self.stderr_drain.take() {
621            let _ = stderr_drain.join();
622        }
623    }
624}
625
626struct ShellKernel {
627    child: Child,
628    stdin: ChildStdin,
629    lines: Receiver<KernelResult<String>>,
630    reader: Option<JoinHandle<()>>,
631    sequence: u64,
632    response_dir: PathBuf,
633}
634
635impl ShellKernel {
636    #[cfg(unix)]
637    fn spawn(root: &Path) -> KernelResult<Self> {
638        let response_dir = std::env::temp_dir().join(format!(
639            "glass-shell-kernel-{}-{}",
640            std::process::id(),
641            std::time::SystemTime::now()
642                .duration_since(std::time::UNIX_EPOCH)
643                .map_err(|error| KernelError::Protocol(error.to_string()))?
644                .as_nanos()
645        ));
646        std::fs::create_dir(&response_dir)?;
647        let mut child = Command::new("/bin/sh")
648            .current_dir(root)
649            .env("GLASS_SHELL_RESPONSE_DIR", &response_dir)
650            .stdin(Stdio::piped())
651            .stdout(Stdio::piped())
652            .stderr(Stdio::null())
653            .spawn()?;
654        let mut stdin = child
655            .stdin
656            .take()
657            .ok_or_else(|| KernelError::Protocol("shell stdin was unavailable".into()))?;
658        let stdout = child
659            .stdout
660            .take()
661            .ok_or_else(|| KernelError::Protocol("shell stdout was unavailable".into()))?;
662        stdin.write_all(
663            br#"glass_tool_sequence=0
664glass_call() {
665  glass_tool_sequence=$((glass_tool_sequence + 1))
666  glass_tool_arguments="$2"
667  [ -n "$glass_tool_arguments" ] || glass_tool_arguments='{}'
668  glass_tool_response="$GLASS_SHELL_RESPONSE_DIR/response-$glass_tool_sequence"
669  rm -f "$glass_tool_response"
670  printf '__GLASS_TOOL_CALL__:%s\t%s\t%s\n' "$glass_tool_sequence" "$1" "$glass_tool_arguments"
671  while [ ! -f "$glass_tool_response" ]; do sleep 0.01; done
672  cat "$glass_tool_response"
673  rm -f "$glass_tool_response"
674  printf '\n'
675}
676"#,
677        )?;
678        stdin.flush()?;
679        let (sender, lines) = mpsc::sync_channel(MAX_KERNEL_MESSAGES);
680        let reader = std::thread::spawn(move || {
681            for line in BufReader::new(stdout).lines() {
682                let line = line.map_err(KernelError::Io);
683                match sender.try_send(line) {
684                    Ok(()) => {}
685                    Err(TrySendError::Full(_) | TrySendError::Disconnected(_)) => break,
686                }
687            }
688        });
689        Ok(Self {
690            child,
691            stdin,
692            lines,
693            reader: Some(reader),
694            sequence: 0,
695            response_dir,
696        })
697    }
698
699    #[cfg(windows)]
700    fn spawn(_root: &Path) -> KernelResult<Self> {
701        Err(KernelError::Unavailable(
702            "persistent shell kernels are not yet available on Windows".into(),
703        ))
704    }
705
706    #[cfg(unix)]
707    fn execute(
708        &mut self,
709        code: &str,
710        timeout: Duration,
711        dispatch: &mut impl FnMut(&KernelToolCall) -> KernelResult<Value>,
712    ) -> KernelResult<KernelOutput> {
713        self.sequence = self
714            .sequence
715            .checked_add(1)
716            .ok_or_else(|| KernelError::Protocol("shell sequence overflowed".into()))?;
717        let marker = format!("__GLASS_SHELL_{}__", self.sequence);
718        writeln!(
719            self.stdin,
720            "{{\n{code}\n}} 2>&1\nglass_status=$?\nprintf '{marker}:%s\\n' \"$glass_status\""
721        )?;
722        self.stdin.flush()?;
723        let deadline = Instant::now() + timeout;
724        let mut output = String::new();
725        let mut truncated = false;
726        loop {
727            let remaining = deadline.saturating_duration_since(Instant::now());
728            if remaining.is_zero() {
729                self.stop();
730                return Err(KernelError::Timeout(format!(
731                    "execution exceeded {} ms",
732                    timeout.as_millis()
733                )));
734            }
735            let line = match self.lines.recv_timeout(remaining) {
736                Ok(line) => line?,
737                Err(RecvTimeoutError::Timeout) => {
738                    self.stop();
739                    return Err(KernelError::Timeout(format!(
740                        "execution exceeded {} ms",
741                        timeout.as_millis()
742                    )));
743                }
744                Err(RecvTimeoutError::Disconnected) => {
745                    return Err(KernelError::Protocol("shell output stream closed".into()));
746                }
747            };
748            let marker_prefix = format!("{marker}:");
749            if let Some(marker_index) = line.find(&marker_prefix) {
750                append_bounded(&mut output, &line[..marker_index], &mut truncated);
751                let status = line[marker_index + marker_prefix.len()..].trim();
752                let status = status.parse::<i32>().map_err(|_| {
753                    KernelError::Protocol("shell returned an invalid exit status".into())
754                })?;
755                if status != 0 {
756                    return Err(KernelError::Execution(format!(
757                        "shell command exited with status {status}: {output}"
758                    )));
759                }
760                return Ok((output, json!({"exitCode": status}), truncated));
761            }
762            if let Some(request) = line.strip_prefix("__GLASS_TOOL_CALL__:") {
763                let mut fields = request.splitn(3, '\t');
764                let request_id = fields
765                    .next()
766                    .and_then(|value| value.parse::<u64>().ok())
767                    .filter(|value| *value > 0)
768                    .ok_or_else(|| {
769                        KernelError::Protocol("shell Glass tool call has an invalid ID".into())
770                    })?;
771                let tool = fields.next().ok_or_else(|| {
772                    KernelError::Protocol("shell Glass tool call has no tool".into())
773                })?;
774                let arguments = fields.next().ok_or_else(|| {
775                    KernelError::Protocol("shell Glass tool call has no arguments".into())
776                })?;
777                let call = KernelToolCall {
778                    id: format!("shell-{request_id}"),
779                    tool: tool.to_string(),
780                    arguments: serde_json::from_str(arguments).map_err(|error| {
781                        KernelError::Protocol(format!(
782                            "shell Glass tool arguments are invalid JSON: {error}"
783                        ))
784                    })?,
785                };
786                let result = dispatch(&call)?;
787                let response = serde_json::to_vec(&result).map_err(|error| {
788                    KernelError::Protocol(format!("could not encode shell tool result: {error}"))
789                })?;
790                let response_path = self.response_dir.join(format!("response-{request_id}"));
791                let temporary_path = self.response_dir.join(format!("response-{request_id}.tmp"));
792                std::fs::write(&temporary_path, response)?;
793                std::fs::rename(temporary_path, response_path)?;
794                continue;
795            }
796            append_bounded(&mut output, &line, &mut truncated);
797            append_bounded(&mut output, "\n", &mut truncated);
798        }
799    }
800
801    #[cfg(windows)]
802    fn execute(
803        &mut self,
804        _code: &str,
805        _timeout: Duration,
806        _dispatch: &mut impl FnMut(&KernelToolCall) -> KernelResult<Value>,
807    ) -> KernelResult<KernelOutput> {
808        Err(KernelError::Unavailable(
809            "persistent shell kernels are not yet available on Windows".into(),
810        ))
811    }
812
813    fn stop(&mut self) {
814        if self.child.try_wait().ok().flatten().is_none() {
815            let _ = self.child.kill();
816            let _ = self.child.wait();
817        }
818        if let Some(reader) = self.reader.take() {
819            let _ = reader.join();
820        }
821        let _ = std::fs::remove_dir_all(&self.response_dir);
822    }
823}
824
825struct SqlKernel {
826    connection: rusqlite::Connection,
827}
828
829impl SqlKernel {
830    fn new() -> KernelResult<Self> {
831        Ok(Self {
832            connection: rusqlite::Connection::open_in_memory()?,
833        })
834    }
835
836    fn execute(
837        &mut self,
838        code: &str,
839        dispatch: &mut impl FnMut(&KernelToolCall) -> KernelResult<Value>,
840    ) -> KernelResult<KernelOutput> {
841        if let Some(request) = code.trim().strip_prefix("GLASS CALL ") {
842            let call: KernelToolCall = serde_json::from_str(request).map_err(|error| {
843                KernelError::InvalidInput(format!("invalid SQL Glass tool call: {error}"))
844            })?;
845            return bounded_output("", dispatch(&call)?);
846        }
847        let leading = code.trim_start().to_ascii_uppercase();
848        if !["SELECT", "WITH", "PRAGMA", "EXPLAIN"]
849            .iter()
850            .any(|keyword| leading.starts_with(keyword))
851        {
852            self.connection.execute_batch(code)?;
853            return Ok((String::new(), Value::Null, false));
854        }
855        let mut statement = self.connection.prepare(code)?;
856        if statement.column_count() == 0 {
857            drop(statement);
858            self.connection.execute_batch(code)?;
859            return Ok((String::new(), Value::Null, false));
860        }
861        let names = statement
862            .column_names()
863            .into_iter()
864            .map(str::to_string)
865            .collect::<Vec<_>>();
866        let column_count = names.len();
867        let rows = statement.query_map([], |row| {
868            let mut record = serde_json::Map::new();
869            for (index, name) in names.iter().enumerate().take(column_count) {
870                record.insert(name.clone(), sql_value(row.get_ref(index)?));
871            }
872            Ok(Value::Object(record))
873        })?;
874        let mut values = Vec::new();
875        for row in rows.take(100_000) {
876            values.push(row?);
877            if serde_json::to_vec(&values)
878                .map_err(|error| KernelError::Protocol(error.to_string()))?
879                .len()
880                > MAX_KERNEL_OUTPUT_BYTES
881            {
882                values.pop();
883                return Ok((String::new(), Value::Array(values), true));
884            }
885        }
886        Ok((String::new(), Value::Array(values), false))
887    }
888}
889
890type KernelOutput = (String, Value, bool);
891
892fn sql_value(value: ValueRef<'_>) -> Value {
893    match value {
894        ValueRef::Null => Value::Null,
895        ValueRef::Integer(value) => json!(value),
896        ValueRef::Real(value) => json!(value),
897        ValueRef::Text(value) => String::from_utf8_lossy(value).into_owned().into(),
898        ValueRef::Blob(value) => json!({"blobBytes": value.len()}),
899    }
900}
901
902fn bounded_output(output: &str, value: Value) -> KernelResult<KernelOutput> {
903    let mut retained = String::new();
904    let mut truncated = false;
905    append_bounded(&mut retained, output, &mut truncated);
906    let encoded_value = serde_json::to_vec(&value).map_err(|error| {
907        KernelError::Protocol(format!("could not encode kernel value: {error}"))
908    })?;
909    if encoded_value.len() > MAX_KERNEL_OUTPUT_BYTES {
910        return Ok((retained, Value::Null, true));
911    }
912    Ok((retained, value, truncated))
913}
914
915fn append_bounded(output: &mut String, value: &str, truncated: &mut bool) {
916    let remaining = MAX_KERNEL_OUTPUT_BYTES.saturating_sub(output.len());
917    if value.len() <= remaining {
918        output.push_str(value);
919    } else {
920        let boundary = value
921            .char_indices()
922            .take_while(|(index, _)| *index <= remaining)
923            .map(|(index, _)| index)
924            .last()
925            .unwrap_or(0);
926        output.push_str(&value[..boundary]);
927        *truncated = true;
928    }
929}
930
931fn validate_code(code: &str) -> KernelResult<()> {
932    if code.is_empty() || code.len() > MAX_KERNEL_CODE_BYTES {
933        return Err(KernelError::InvalidInput(format!(
934            "kernel code must contain 1..={MAX_KERNEL_CODE_BYTES} bytes"
935        )));
936    }
937    Ok(())
938}
939
940fn validate_capabilities(capabilities: &[String]) -> KernelResult<BTreeSet<String>> {
941    if capabilities.len() > 64 {
942        return Err(KernelError::InvalidInput(
943            "kernel capability limit is 64".into(),
944        ));
945    }
946    capabilities
947        .iter()
948        .map(|capability| {
949            if capability.is_empty()
950                || capability.len() > 128
951                || capability.starts_with("glass.eval.")
952                || !capability
953                    .chars()
954                    .all(|character| character.is_ascii_alphanumeric() || ".-_".contains(character))
955            {
956                return Err(KernelError::InvalidInput(format!(
957                    "invalid or recursive kernel capability {capability}"
958                )));
959            }
960            Ok(capability.clone())
961        })
962        .collect()
963}
964
965fn validate_name(name: &str, description: &str) -> KernelResult<()> {
966    if name.is_empty()
967        || name.len() > 128
968        || !name
969            .chars()
970            .all(|character| character.is_ascii_alphanumeric() || "-_.".contains(character))
971    {
972        return Err(KernelError::InvalidInput(format!(
973            "{description} must be 1..=128 ASCII letters, digits, '-', '_' or '.'"
974        )));
975    }
976    Ok(())
977}
978
979fn validate_actor_id(name: &str, description: &str) -> KernelResult<()> {
980    if name.is_empty()
981        || name.len() > 128
982        || !name
983            .chars()
984            .all(|character| character.is_ascii_alphanumeric() || "-_.:".contains(character))
985    {
986        return Err(KernelError::InvalidInput(format!(
987            "{description} must be 1..=128 ASCII letters, digits, '-', '_', '.' or ':'"
988        )));
989    }
990    Ok(())
991}
992
993#[cfg(test)]
994mod tests {
995    use super::*;
996    use std::sync::atomic::{AtomicU64, Ordering};
997
998    static NEXT_ROOT: AtomicU64 = AtomicU64::new(0);
999
1000    fn root() -> PathBuf {
1001        let sequence = NEXT_ROOT.fetch_add(1, Ordering::Relaxed);
1002        let root = std::env::temp_dir().join(format!(
1003            "glass-kernel-manager-{}-{sequence}",
1004            std::process::id()
1005        ));
1006        std::fs::create_dir_all(&root).unwrap();
1007        root
1008    }
1009
1010    #[test]
1011    fn python_kernel_preserves_state_and_attribution() {
1012        let root = root();
1013        let mut kernels = KernelManager::new(&root).unwrap();
1014        kernels
1015            .start("python", KernelKind::Python, "human")
1016            .unwrap();
1017        kernels
1018            .execute("python", "answer = 40", "human", 1, None)
1019            .unwrap();
1020        let result = kernels
1021            .execute("python", "answer + 2", "agent", 2, None)
1022            .unwrap();
1023        assert_eq!(result.value, "42");
1024        assert_eq!(result.actor_id, "kernel:python");
1025        assert_eq!(result.initiator_id, "agent");
1026        assert_eq!(result.executor_id, "kernel:python");
1027        assert_eq!(result.workspace_revision, 2);
1028        assert_eq!(kernels.snapshot("python").unwrap().executions, 2);
1029        kernels.stop("python").unwrap();
1030        std::fs::remove_dir_all(root).unwrap();
1031    }
1032
1033    #[cfg(unix)]
1034    #[test]
1035    fn shell_kernel_preserves_environment_and_directory_state() {
1036        let root = root();
1037        let mut kernels = KernelManager::new(&root).unwrap();
1038        kernels.start("shell", KernelKind::Shell, "human").unwrap();
1039        kernels
1040            .execute("shell", "value=glass", "human", 1, None)
1041            .unwrap();
1042        let result = kernels
1043            .execute("shell", "printf '%s' \"$value\"", "human", 1, None)
1044            .unwrap();
1045        assert_eq!(result.output, "glass");
1046        kernels.stop("shell").unwrap();
1047        std::fs::remove_dir_all(root).unwrap();
1048    }
1049
1050    #[test]
1051    fn sql_kernel_preserves_tables_and_returns_structured_rows() {
1052        let root = root();
1053        let mut kernels = KernelManager::new(&root).unwrap();
1054        kernels.start("sql", KernelKind::Sql, "agent").unwrap();
1055        kernels
1056            .execute(
1057                "sql",
1058                "CREATE TABLE inventory (sku TEXT, count INTEGER); INSERT INTO inventory VALUES ('x', 0);",
1059                "agent",
1060                1,
1061                None,
1062            )
1063            .unwrap();
1064        let result = kernels
1065            .execute("sql", "SELECT sku, count FROM inventory", "agent", 1, None)
1066            .unwrap();
1067        assert_eq!(result.value[0]["sku"], "x");
1068        assert_eq!(result.value[0]["count"], 0);
1069        drop(kernels);
1070        std::fs::remove_dir_all(root).unwrap();
1071    }
1072
1073    #[test]
1074    fn kernel_names_code_and_timeouts_fail_closed() {
1075        assert!(validate_name("python-1", "kernel").is_ok());
1076        assert!(validate_name("../escape", "kernel").is_err());
1077        assert!(validate_code("").is_err());
1078        assert!(validate_code(&"x".repeat(MAX_KERNEL_CODE_BYTES + 1)).is_err());
1079    }
1080
1081    #[test]
1082    fn python_and_javascript_bindings_dispatch_granted_capabilities() {
1083        let root = root();
1084        let capability = vec!["glass.test.results".to_string()];
1085        for (name, kind, code) in [
1086            (
1087                "python-binding",
1088                KernelKind::Python,
1089                "glass.call('glass.test.results', {'limit': 1})",
1090            ),
1091            (
1092                "javascript-binding",
1093                KernelKind::JavaScript,
1094                "glass.call('glass.test.results', {limit: 1}).then(value => value.count)",
1095            ),
1096        ] {
1097            let mut kernels = KernelManager::new(&root).unwrap();
1098            if let Err(error) =
1099                kernels.start_governed(name, kind, &format!("kernel:{name}"), &capability, false)
1100            {
1101                if matches!(error, KernelError::Unavailable(_)) {
1102                    continue;
1103                }
1104                panic!("could not start {name}: {error}");
1105            }
1106            let mut observed = Vec::new();
1107            let execution = kernels
1108                .execute_with_tools(name, code, "agent-0003", 7, None, |call| {
1109                    observed.push(call.clone());
1110                    Ok(json!({"count": 3}))
1111                })
1112                .unwrap();
1113            assert_eq!(execution.initiator_id, "agent-0003");
1114            assert_eq!(execution.executor_id, format!("kernel:{name}"));
1115            assert_eq!(execution.tool_calls, 1);
1116            assert_eq!(observed[0].tool, "glass.test.results");
1117            assert_eq!(observed[0].arguments["limit"], 1);
1118        }
1119        std::fs::remove_dir_all(root).unwrap();
1120    }
1121
1122    #[test]
1123    fn sql_and_shell_bindings_use_the_same_bounded_dispatch_contract() {
1124        let root = root();
1125        let capability = vec!["glass.graph.path".to_string()];
1126        let mut kinds = vec![(
1127            "sql-binding",
1128            KernelKind::Sql,
1129            r#"GLASS CALL {"id":"sql-1","tool":"glass.graph.path","arguments":{"from":"a","to":"b"}}"#,
1130        )];
1131        if cfg!(unix) {
1132            kinds.push((
1133                "shell-binding",
1134                KernelKind::Shell,
1135                r#"glass_call glass.graph.path '{"from":"a","to":"b"}'"#,
1136            ));
1137        }
1138        for (name, kind, code) in kinds {
1139            let mut kernels = KernelManager::new(&root).unwrap();
1140            kernels
1141                .start_governed(name, kind, &format!("kernel:{name}"), &capability, false)
1142                .unwrap();
1143            let result = kernels
1144                .execute_with_tools(name, code, "human:local", 9, None, |call| {
1145                    assert_eq!(call.tool, "glass.graph.path");
1146                    Ok(json!({"path":["a","b"]}))
1147                })
1148                .unwrap();
1149            assert_eq!(result.tool_calls, 1);
1150        }
1151        std::fs::remove_dir_all(root).unwrap();
1152    }
1153
1154    #[test]
1155    fn ungranted_and_recursive_kernel_capabilities_fail_closed() {
1156        assert!(validate_capabilities(&["glass.eval.execute".into()]).is_err());
1157        let root = root();
1158        let mut kernels = KernelManager::new(&root).unwrap();
1159        kernels
1160            .start_governed(
1161                "python-denied",
1162                KernelKind::Python,
1163                "kernel:python-denied",
1164                &["glass.test.results".into()],
1165                false,
1166            )
1167            .unwrap();
1168        let error = kernels
1169            .execute_with_tools(
1170                "python-denied",
1171                "glass.call('glass.file.write', {'path':'x'})",
1172                "agent-0003",
1173                1,
1174                None,
1175                |_| panic!("ungranted call reached the dispatcher"),
1176            )
1177            .unwrap_err();
1178        assert!(error.to_string().contains("not granted"));
1179        drop(kernels);
1180        std::fs::remove_dir_all(root).unwrap();
1181    }
1182}