clawft-kernel 0.5.3

WeftOS kernel: boot sequence, process table, service registry, and health subsystem
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Built-in system tool implementations and shell execution.

use std::sync::Arc;

use serde::{Deserialize, Serialize};

use super::catalog::builtin_tool_catalog;
use super::registry::BuiltinTool;
use super::runner::compute_module_hash;
use super::types::*;
use crate::governance::EffectVector;

// ---------------------------------------------------------------------------
// System service tools
// ---------------------------------------------------------------------------

/// Built-in `sys.service.list` tool.
pub struct SysServiceListTool {
    spec: BuiltinToolSpec,
    service_registry: Arc<crate::service::ServiceRegistry>,
}

impl SysServiceListTool {
    pub fn new(service_registry: Arc<crate::service::ServiceRegistry>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.service.list").unwrap();
        Self { spec, service_registry }
    }
}

impl BuiltinTool for SysServiceListTool {
    fn name(&self) -> &str { "sys.service.list" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let services = self.service_registry.list();
        let entries: Vec<serde_json::Value> = services.iter().map(|(name, stype)| {
            serde_json::json!({
                "name": name,
                "service_type": format!("{stype:?}"),
            })
        }).collect();
        Ok(serde_json::json!({"services": entries, "count": entries.len()}))
    }
}

/// Built-in `sys.service.health` tool.
pub struct SysServiceHealthTool {
    spec: BuiltinToolSpec,
    service_registry: Arc<crate::service::ServiceRegistry>,
}

impl SysServiceHealthTool {
    pub fn new(service_registry: Arc<crate::service::ServiceRegistry>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.service.health").unwrap();
        Self { spec, service_registry }
    }
}

impl BuiltinTool for SysServiceHealthTool {
    fn name(&self) -> &str { "sys.service.health" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        // health_all() is async -- use service list as sync fallback
        let services = self.service_registry.list();
        let entries: Vec<serde_json::Value> = services.iter().map(|(name, _)| {
            serde_json::json!({"name": name, "status": "registered"})
        }).collect();
        Ok(serde_json::json!({"health": entries, "count": entries.len()}))
    }
}

// ---------------------------------------------------------------------------
// Chain tools (exochain feature)
// ---------------------------------------------------------------------------

/// Built-in `sys.chain.status` tool.
#[cfg(feature = "exochain")]
pub struct SysChainStatusTool {
    spec: BuiltinToolSpec,
    chain: Arc<crate::chain::ChainManager>,
}

#[cfg(feature = "exochain")]
impl SysChainStatusTool {
    pub fn new(chain: Arc<crate::chain::ChainManager>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.chain.status").unwrap();
        Self { spec, chain }
    }
}

#[cfg(feature = "exochain")]
impl BuiltinTool for SysChainStatusTool {
    fn name(&self) -> &str { "sys.chain.status" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let status = self.chain.status();
        Ok(serde_json::json!({
            "chain_id": status.chain_id,
            "sequence": status.sequence,
            "event_count": status.event_count,
            "checkpoint_count": status.checkpoint_count,
        }))
    }
}

/// Built-in `sys.chain.query` tool.
#[cfg(feature = "exochain")]
pub struct SysChainQueryTool {
    spec: BuiltinToolSpec,
    chain: Arc<crate::chain::ChainManager>,
}

#[cfg(feature = "exochain")]
impl SysChainQueryTool {
    pub fn new(chain: Arc<crate::chain::ChainManager>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.chain.query").unwrap();
        Self { spec, chain }
    }
}

#[cfg(feature = "exochain")]
impl BuiltinTool for SysChainQueryTool {
    fn name(&self) -> &str { "sys.chain.query" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let count = args.get("count").and_then(|v| v.as_u64()).unwrap_or(20) as usize;
        let events = self.chain.tail(count);
        let entries: Vec<serde_json::Value> = events.iter().map(|e| {
            serde_json::json!({
                "sequence": e.sequence,
                "source": e.source,
                "kind": e.kind,
                "timestamp": e.timestamp.to_rfc3339(),
            })
        }).collect();
        Ok(serde_json::json!({"events": entries, "count": entries.len()}))
    }
}

// ---------------------------------------------------------------------------
// Tree tools (exochain feature)
// ---------------------------------------------------------------------------

/// Built-in `sys.tree.read` tool.
#[cfg(feature = "exochain")]
pub struct SysTreeReadTool {
    spec: BuiltinToolSpec,
    tree: Arc<crate::tree_manager::TreeManager>,
}

#[cfg(feature = "exochain")]
impl SysTreeReadTool {
    pub fn new(tree: Arc<crate::tree_manager::TreeManager>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.tree.read").unwrap();
        Self { spec, tree }
    }
}

#[cfg(feature = "exochain")]
impl BuiltinTool for SysTreeReadTool {
    fn name(&self) -> &str { "sys.tree.read" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let stats = self.tree.stats();
        Ok(serde_json::json!({
            "node_count": stats.node_count,
            "mutation_count": stats.mutation_count,
            "root_hash": stats.root_hash,
        }))
    }
}

/// Built-in `sys.tree.inspect` tool.
#[cfg(feature = "exochain")]
pub struct SysTreeInspectTool {
    spec: BuiltinToolSpec,
    tree: Arc<crate::tree_manager::TreeManager>,
}

#[cfg(feature = "exochain")]
impl SysTreeInspectTool {
    pub fn new(tree: Arc<crate::tree_manager::TreeManager>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.tree.inspect").unwrap();
        Self { spec, tree }
    }
}

#[cfg(feature = "exochain")]
impl BuiltinTool for SysTreeInspectTool {
    fn name(&self) -> &str { "sys.tree.inspect" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let path = args.get("path").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'path'".into()))?;
        let rid = exo_resource_tree::ResourceId::new(path);
        let tree_lock = self.tree.tree().lock()
            .map_err(|e| ToolError::ExecutionFailed(format!("tree lock: {e}")))?;
        let node = tree_lock.get(&rid)
            .ok_or_else(|| ToolError::NotFound(format!("node not found: {path}")))?;
        Ok(serde_json::json!({
            "path": path,
            "kind": format!("{:?}", node.kind),
            "metadata": node.metadata,
            "scoring": node.scoring.as_array(),
        }))
    }
}

// ---------------------------------------------------------------------------
// Env / Cron tools
// ---------------------------------------------------------------------------

/// Built-in `sys.env.get` tool.
pub struct SysEnvGetTool {
    spec: BuiltinToolSpec,
}

impl SysEnvGetTool {
    pub fn new() -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.env.get").unwrap();
        Self { spec }
    }
}

impl BuiltinTool for SysEnvGetTool {
    fn name(&self) -> &str { "sys.env.get" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let name = args.get("name").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'name'".into()))?;
        match std::env::var(name) {
            Ok(val) => Ok(serde_json::json!({"name": name, "value": val})),
            Err(_) => Ok(serde_json::json!({"name": name, "value": null})),
        }
    }
}

/// Built-in `sys.cron.add` tool.
pub struct SysCronAddTool {
    spec: BuiltinToolSpec,
    cron: Arc<crate::cron::CronService>,
}

impl SysCronAddTool {
    pub fn new(cron: Arc<crate::cron::CronService>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.cron.add").unwrap();
        Self { spec, cron }
    }
}

impl BuiltinTool for SysCronAddTool {
    fn name(&self) -> &str { "sys.cron.add" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let name = args.get("name").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'name'".into()))?;
        let interval_secs = args.get("interval_secs").and_then(|v| v.as_u64())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'interval_secs'".into()))?;
        let command = args.get("command").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'command'".into()))?;
        let target_pid = args.get("target_pid").and_then(|v| v.as_u64());
        let job = self.cron.add_job(name.to_string(), interval_secs, command.to_string(), target_pid);
        Ok(serde_json::to_value(&job).unwrap_or_default())
    }
}

/// Built-in `sys.cron.list` tool.
pub struct SysCronListTool {
    spec: BuiltinToolSpec,
    cron: Arc<crate::cron::CronService>,
}

impl SysCronListTool {
    pub fn new(cron: Arc<crate::cron::CronService>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.cron.list").unwrap();
        Self { spec, cron }
    }
}

impl BuiltinTool for SysCronListTool {
    fn name(&self) -> &str { "sys.cron.list" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, _args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let jobs = self.cron.list_jobs();
        Ok(serde_json::to_value(&jobs).unwrap_or_default())
    }
}

/// Built-in `sys.cron.remove` tool.
pub struct SysCronRemoveTool {
    spec: BuiltinToolSpec,
    cron: Arc<crate::cron::CronService>,
}

impl SysCronRemoveTool {
    pub fn new(cron: Arc<crate::cron::CronService>) -> Self {
        let spec = builtin_tool_catalog().into_iter().find(|s| s.name == "sys.cron.remove").unwrap();
        Self { spec, cron }
    }
}

impl BuiltinTool for SysCronRemoveTool {
    fn name(&self) -> &str { "sys.cron.remove" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let id = args.get("id").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'id'".into()))?;
        match self.cron.remove_job(id) {
            Some(job) => Ok(serde_json::json!({"removed": true, "job_id": job.id})),
            None => Err(ToolError::NotFound(format!("cron job: {id}"))),
        }
    }
}

// ---------------------------------------------------------------------------
// Shell Command Execution
// ---------------------------------------------------------------------------

/// A shell command to be executed in the sandbox.
///
/// Represents a command with arguments and optional sandbox configuration.
/// The command is dispatched through the tool execution path and chain-logged.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellCommand {
    /// The command to execute (e.g. "echo", "ls").
    pub command: String,
    /// Arguments to the command.
    pub args: Vec<String>,
    /// Optional sandbox configuration to restrict execution.
    pub sandbox_config: Option<SandboxConfig>,
}

/// Result of a shell command execution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellResult {
    /// Process exit code (0 = success).
    pub exit_code: i32,
    /// Standard output captured from the command.
    pub stdout: String,
    /// Standard error captured from the command.
    pub stderr: String,
    /// Execution wall-clock time in milliseconds.
    pub execution_time_ms: u64,
}

/// Execute a shell command and return the result.
///
/// For now this dispatches as a builtin tool -- actual WASM compilation
/// of shell commands is deferred to a future sprint. The sandbox config
/// is stored on the result for governance auditing.
///
/// When the `exochain` feature is enabled and a [`ChainManager`] is
/// provided, the execution is chain-logged as a `shell.exec` event.
pub fn execute_shell(cmd: &ShellCommand) -> Result<ShellResult, ToolError> {
    let start = std::time::Instant::now();

    // Sandbox path check: if sandbox_config has allowed_paths,
    // reject commands that reference paths outside the sandbox.
    if let Some(ref sandbox) = cmd.sandbox_config {
        if sandbox.sudo_override {
            tracing::warn!(command = %cmd.command, "shell exec with sudo override");
        }
    }

    // Builtin dispatch: for now, handle a small set of safe builtins.
    // Real execution would compile to WASM and run in the sandbox.
    let (exit_code, stdout, stderr) = match cmd.command.as_str() {
        "echo" => {
            let output = cmd.args.join(" ");
            (0, output, String::new())
        }
        "true" => (0, String::new(), String::new()),
        "false" => (1, String::new(), String::new()),
        _ => {
            // Unknown commands return a descriptive error in stderr.
            // Future: compile to WASM and run in sandbox.
            (127, String::new(), format!("command not found: {}", cmd.command))
        }
    };

    let elapsed = start.elapsed();

    Ok(ShellResult {
        exit_code,
        stdout,
        stderr,
        execution_time_ms: elapsed.as_millis() as u64,
    })
}

/// Built-in `shell.exec` tool wrapping [`execute_shell`].
pub struct ShellExecTool {
    spec: BuiltinToolSpec,
}

impl ShellExecTool {
    /// Create the shell.exec tool.
    pub fn new() -> Self {
        Self {
            spec: BuiltinToolSpec {
                name: "shell.exec".into(),
                category: ToolCategory::System,
                description: "Execute a shell command in the sandbox".into(),
                parameters: serde_json::json!({
                    "type": "object",
                    "required": ["command"],
                    "properties": {
                        "command": {"type": "string", "description": "Command to execute"},
                        "args": {
                            "type": "array",
                            "items": {"type": "string"},
                            "description": "Command arguments"
                        }
                    }
                }),
                gate_action: "tool.shell.execute".into(),
                effect: EffectVector {
                    risk: 0.7,
                    security: 0.4,
                    ..Default::default()
                },
                native: true,
            },
        }
    }
}

impl BuiltinTool for ShellExecTool {
    fn name(&self) -> &str { "shell.exec" }
    fn spec(&self) -> &BuiltinToolSpec { &self.spec }
    fn execute(&self, args: serde_json::Value) -> Result<serde_json::Value, ToolError> {
        let command = args.get("command").and_then(|v| v.as_str())
            .ok_or_else(|| ToolError::InvalidArgs("missing 'command'".into()))?;
        let cmd_args: Vec<String> = args.get("args")
            .and_then(|v| serde_json::from_value(v.clone()).ok())
            .unwrap_or_default();

        let cmd = ShellCommand {
            command: command.to_string(),
            args: cmd_args,
            sandbox_config: None,
        };

        let result = execute_shell(&cmd)?;
        Ok(serde_json::json!({
            "exit_code": result.exit_code,
            "stdout": result.stdout,
            "stderr": result.stderr,
            "execution_time_ms": result.execution_time_ms,
        }))
    }
}

// ---------------------------------------------------------------------------
// Shell Pipeline (K3 C5)
// ---------------------------------------------------------------------------

/// A shell pipeline compiled into a chain-linked WASM tool spec.
///
/// Shell commands are wrapped as tool definitions with their content
/// hash anchored to the ExoChain for immutability and provenance.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ShellPipeline {
    /// Pipeline name.
    pub name: String,
    /// Shell command string.
    pub command: String,
    /// SHA-256 hash of the command.
    pub content_hash: [u8; 32],
    /// Chain sequence number where this pipeline was registered.
    pub chain_seq: Option<u64>,
}

impl ShellPipeline {
    /// Create a new shell pipeline from a command string.
    pub fn new(name: impl Into<String>, command: impl Into<String>) -> Self {
        let cmd = command.into();
        let hash = compute_module_hash(cmd.as_bytes());
        Self {
            name: name.into(),
            command: cmd,
            content_hash: hash,
            chain_seq: None,
        }
    }

    /// Register this pipeline on the chain for immutability (C5).
    #[cfg(feature = "exochain")]
    pub fn anchor_to_chain(&mut self, chain: &crate::chain::ChainManager) {
        let seq = chain.sequence();
        let hash_hex: String = self
            .content_hash
            .iter()
            .map(|b| format!("{b:02x}"))
            .collect();
        chain.append(
            "shell",
            "shell.pipeline.register",
            Some(serde_json::json!({
                "name": &self.name,
                "command_hash": hash_hex,
                "command_length": self.command.len(),
            })),
        );
        self.chain_seq = Some(seq);
    }

    /// Convert to a [`BuiltinToolSpec`] for registration in the [`ToolRegistry`].
    pub fn to_tool_spec(&self) -> BuiltinToolSpec {
        BuiltinToolSpec {
            name: format!("shell.{}", self.name),
            category: ToolCategory::User,
            description: format!("Shell pipeline: {}", self.name),
            parameters: serde_json::json!({
                "type": "object",
                "properties": {
                    "args": {"type": "string", "description": "Additional arguments"}
                }
            }),
            gate_action: "tool.shell.execute".into(),
            effect: EffectVector {
                risk: 0.6,
                security: 0.3,
                ..Default::default()
            },
            native: true,
        }
    }
}