everruns-core 0.10.0

Core agent abstractions for Everruns - agent loop, events, tools, LLM providers
Documentation
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
//! Lua Code-Mode Routing Capability (experimental)
//!
//! Turns the sandboxed `lua` tool into the agent's *primary* action surface by
//! hiding "non-essential" tools from the model's direct tool list. Those tools
//! are not removed from the running session — they stay in the executable
//! registry — they are just no longer offered to the model as direct tool
//! calls. The agent reaches them inside a Lua script through the existing
//! code-mode `tools.<name>(args)` table, so one script can read inputs, call
//! several sibling tools, and shape the result in a single turn instead of a
//! chain of individual round-trips.
//!
//! ## How the non-functional requirements are met
//!
//! 1. **Capability-extensibility only (tool filtering).** The sole mechanism is
//!    the existing [`ToolDefinitionHook`] seam (see `specs/capabilities.md` →
//!    Tool definition hooks). No new agent-loop plumbing: the hook runs after
//!    the runtime agent has merged its final tool list and simply drops the
//!    eligible definitions before they reach the model. The *executable*
//!    `ToolRegistry` is built separately from capability `tools()` and is left
//!    untouched, which is what keeps the hidden tools callable from Lua.
//! 2. **Relies on the existing `lua` capability.** `lua` is declared as a hard
//!    [`dependencies`](Capability::dependencies) entry, and its code mode
//!    (`tools.<name>`) is what makes the hidden tools invokable. The filter
//!    mirrors [`is_code_mode_eligible`] exactly, so the set removed from the
//!    model is precisely the set Lua re-exposes — no tool can become
//!    unreachable.
//! 3. **Capabilities export their functions to Lua.** Every eligible capability
//!    tool is surfaced inside the VM as a `tools.<name>(args)` function by the
//!    `lua` engine. This capability is what makes that export the agent's
//!    default path rather than an occasional optimization.

use std::sync::Arc;

use serde_json::{Value, json};

use super::lua::is_code_mode_eligible;
use super::{Capability, CapabilityStatus, RiskLevel, ToolDefinitionHook};
use crate::tool_types::ToolDefinition;

pub const LUA_CODE_MODE_CAPABILITY_ID: &str = "lua_code_mode";

/// Behavior contract only (no tool inventory — that lives in the tool schemas
/// and the `lua` capability's own prompt). Kept to two dense sentences per the
/// prompt-budget guidance in `specs/capabilities.md`.
const SYSTEM_PROMPT: &str = "Most action tools are intentionally omitted from your direct tool list; \
to use them, call them from inside the `lua` tool via the `tools.<name>(args)` table, where a single \
script can chain several calls and process their results in one turn. Prefer one Lua script over a \
sequence of separate tool calls.";

/// Capability that routes non-essential tool calls through the Lua sandbox.
pub struct LuaCodeModeCapability;

impl Capability for LuaCodeModeCapability {
    fn id(&self) -> &str {
        LUA_CODE_MODE_CAPABILITY_ID
    }

    fn name(&self) -> &str {
        "Lua Code Mode"
    }

    fn description(&self) -> &str {
        r#"Route non-essential tool calls through the Lua sandbox.

Hides auto, non-destructive tools from the model's direct tool list so the agent
orchestrates them inside the `lua` tool via `tools.<name>(args)` — fewer
round-trips, structured results.

> [!NOTE]
> Requires the `lua` capability. Execution, approval-gated, destructive, and
> client-side tools stay directly callable."#
    }

    fn status(&self) -> CapabilityStatus {
        CapabilityStatus::Available
    }

    fn risk_level(&self) -> RiskLevel {
        // Inseparable from `lua` (scripted code execution) and reshapes how the
        // agent acts. Admin-gated like its dependency.
        RiskLevel::High
    }

    fn icon(&self) -> Option<&str> {
        Some("code")
    }

    fn category(&self) -> Option<&str> {
        Some("Execution")
    }

    fn system_prompt_addition(&self) -> Option<&str> {
        Some(SYSTEM_PROMPT)
    }

    fn dependencies(&self) -> Vec<&'static str> {
        vec!["lua"]
    }

    fn tool_definition_hooks(&self) -> Vec<Arc<dyn ToolDefinitionHook>> {
        vec![Arc::new(HideCodeModeToolsHook::default())]
    }

    fn tool_definition_hooks_with_config(
        &self,
        config: &Value,
    ) -> Vec<Arc<dyn ToolDefinitionHook>> {
        let opts = parse_options(config);
        vec![Arc::new(HideCodeModeToolsHook {
            keep_visible: opts.keep_visible,
            full_schemas: opts.full_schemas,
        })]
    }

    fn config_schema(&self) -> Option<Value> {
        Some(json!({
            "type": "object",
            "properties": {
                "keep_visible": {
                    "type": "array",
                    "items": { "type": "string" },
                    "description": "Tool names to keep directly callable by the model even when they would otherwise be routed through Lua code mode."
                },
                "full_schemas": {
                    "type": "boolean",
                    "default": false,
                    "description": "Embed each hidden tool's full JSON Schema in the lua tool description (lossless discovery). Off by default: a compact typed signature is shown instead."
                }
            },
            "additionalProperties": false
        }))
    }

    fn validate_config(&self, config: &Value) -> Result<(), String> {
        if config.is_null() {
            return Ok(());
        }
        let obj = config
            .as_object()
            .ok_or_else(|| "config must be a JSON object".to_string())?;
        // Enforce the same closed contract as `config_schema`
        // (`additionalProperties: false`) on every write path.
        for key in obj.keys() {
            if key != "keep_visible" && key != "full_schemas" {
                return Err(format!("unknown config key: {key}"));
            }
        }
        if let Some(keep) = obj.get("keep_visible") {
            let arr = keep
                .as_array()
                .ok_or_else(|| "keep_visible must be an array of tool names".to_string())?;
            if arr.iter().any(|v| !v.is_string()) {
                return Err("keep_visible entries must be strings".to_string());
            }
        }
        if let Some(full) = obj.get("full_schemas")
            && !full.is_boolean()
        {
            return Err("full_schemas must be a boolean".to_string());
        }
        Ok(())
    }
}

/// Per-agent options parsed from capability config.
#[derive(Default)]
struct CodeModeOptions {
    keep_visible: Vec<String>,
    full_schemas: bool,
}

fn parse_options(config: &Value) -> CodeModeOptions {
    CodeModeOptions {
        keep_visible: config
            .get("keep_visible")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str().map(str::to_string))
                    .collect()
            })
            .unwrap_or_default(),
        full_schemas: config
            .get("full_schemas")
            .and_then(|v| v.as_bool())
            .unwrap_or(false),
    }
}

/// Drops code-mode-eligible tool definitions from the model-facing tool list
/// and grafts a catalog of them onto the `lua` tool description.
///
/// Tools listed in `keep_visible` are always retained. Everything that is *not*
/// eligible (the `lua`/`bash` execution tools, destructive, approval-gated,
/// client-side, and `cpu_bound` tools) is also retained — those stay direct
/// tool calls.
///
/// Because the hidden tools' standalone schemas are removed, the model would
/// otherwise have no way to discover their names/arguments. So the eligible set
/// is summarized into a `tools.<name>{ args }` catalog appended to the `lua`
/// tool's own description — the discovery surface the model reads right before
/// it decides to call `lua`. Each entry shows a typed signature; with
/// `full_schemas` it also embeds the tool's complete JSON Schema (lossless).
#[derive(Default)]
struct HideCodeModeToolsHook {
    keep_visible: Vec<String>,
    full_schemas: bool,
}

impl ToolDefinitionHook for HideCodeModeToolsHook {
    fn transform(&self, tools: Vec<ToolDefinition>) -> Vec<ToolDefinition> {
        let mut kept: Vec<ToolDefinition> = Vec::new();
        let mut catalog: Vec<String> = Vec::new();
        for def in tools {
            let name = def.name();
            let hidden = !self.keep_visible.iter().any(|k| k == name)
                && is_code_mode_eligible(name, def.policy(), def.hints());
            if hidden {
                catalog.push(format_catalog_entry(&def, self.full_schemas));
            } else {
                kept.push(def);
            }
        }
        if !catalog.is_empty()
            && let Some(lua) = kept.iter_mut().find(|d| d.name() == "lua")
        {
            graft_catalog_onto_lua(lua, &catalog);
        }
        kept
    }

    fn applies_with_native_tool_search(&self) -> bool {
        // This is a hard routing policy (the model must go through Lua), not a
        // schema-deferral optimization, so it applies regardless of whether the
        // model uses hosted tool_search.
        true
    }
}

/// One catalog entry: `- name(a: number, b?: string) — first sentence`, with an
/// optional indented `schema: { ... }` line when `full_schemas` is set.
fn format_catalog_entry(def: &ToolDefinition, full_schemas: bool) -> String {
    let sig = typed_signature(def.full_parameters());
    let desc = first_sentence(def.description());
    let mut entry = if desc.is_empty() {
        format!("- {}({sig})", def.name())
    } else {
        format!("- {}({sig}) — {desc}", def.name())
    };
    if full_schemas {
        entry.push_str(&format!(
            "\n    schema: {}",
            compact_schema(def.full_parameters())
        ));
    }
    entry
}

/// Typed argument signature from a JSON Schema object: `name: type` for required
/// args first, then `name?: type` for optional ones. The synthetic
/// `human_intent` narration argument (added by `human_intent`) is omitted.
fn typed_signature(schema: &Value) -> String {
    let Some(props) = schema.get("properties").and_then(|v| v.as_object()) else {
        return String::new();
    };
    let required: Vec<&str> = schema
        .get("required")
        .and_then(|v| v.as_array())
        .map(|a| a.iter().filter_map(|x| x.as_str()).collect())
        .unwrap_or_default();
    let mut parts: Vec<String> = Vec::new();
    for r in &required {
        if *r != crate::tool_types::HUMAN_INTENT_ARGUMENT
            && let Some(prop) = props.get(*r)
        {
            parts.push(format!("{r}: {}", json_type(prop)));
        }
    }
    for (key, prop) in props {
        if key != crate::tool_types::HUMAN_INTENT_ARGUMENT && !required.contains(&key.as_str()) {
            parts.push(format!("{key}?: {}", json_type(prop)));
        }
    }
    parts.join(", ")
}

/// Render a JSON Schema property's `type` as a short string (e.g. `number`,
/// `string|null`, `enum`, `object`). Falls back to `any` when unspecified.
fn json_type(prop: &Value) -> String {
    match prop.get("type") {
        Some(Value::String(s)) => s.clone(),
        Some(Value::Array(types)) => types
            .iter()
            .filter_map(|v| v.as_str())
            .collect::<Vec<_>>()
            .join("|"),
        _ if prop.get("enum").is_some() => "enum".to_string(),
        _ => "any".to_string(),
    }
}

/// Minified JSON Schema for full discovery, with the synthetic `human_intent`
/// argument stripped (it is never passed to `tools.*`).
fn compact_schema(schema: &Value) -> String {
    let mut schema = schema.clone();
    if let Some(obj) = schema.as_object_mut() {
        if let Some(props) = obj.get_mut("properties").and_then(|v| v.as_object_mut()) {
            props.remove(crate::tool_types::HUMAN_INTENT_ARGUMENT);
        }
        if let Some(req) = obj.get_mut("required").and_then(|v| v.as_array_mut()) {
            req.retain(|v| v.as_str() != Some(crate::tool_types::HUMAN_INTENT_ARGUMENT));
        }
    }
    serde_json::to_string(&schema).unwrap_or_else(|_| "{}".to_string())
}

/// First sentence (up to `.` or newline), trimmed and length-capped.
fn first_sentence(description: &str) -> String {
    let trimmed = description.trim();
    let end = trimmed
        .find(['.', '\n'])
        .map(|i| i + 1)
        .unwrap_or(trimmed.len());
    let mut s = trimmed[..end].trim().to_string();
    const MAX: usize = 140;
    if s.len() > MAX {
        s.truncate(MAX);
        s.push('');
    }
    s
}

/// Append the catalog to the `lua` tool's description so the model can discover
/// the in-script `tools.*` functions and their arguments.
fn graft_catalog_onto_lua(def: &mut ToolDefinition, catalog: &[String]) {
    let block = format!(
        "\n\nThese tools are available ONLY inside this script via the `tools` table — \
call them as `tools.<name>{{ args }}` (e.g. `local r = tools.add{{ a = 1, b = 2 }}`). \
Argument types follow each name:\n{}",
        catalog.join("\n")
    );
    match def {
        ToolDefinition::Builtin(b) => b.description.push_str(&block),
        ToolDefinition::ClientSide(c) => c.description.push_str(&block),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::tool_types::{BuiltinTool, ClientSideTool, DeferrablePolicy, ToolHints, ToolPolicy};

    fn builtin(name: &str, policy: ToolPolicy, hints: ToolHints) -> ToolDefinition {
        ToolDefinition::Builtin(BuiltinTool {
            name: name.to_string(),
            display_name: None,
            description: format!("{name} tool"),
            parameters: json!({ "type": "object" }),
            policy,
            category: None,
            deferrable: DeferrablePolicy::default(),
            hints,
            full_parameters: None,
        })
    }

    fn names(defs: &[ToolDefinition]) -> Vec<&str> {
        defs.iter().map(|d| d.name()).collect()
    }

    #[test]
    fn capability_metadata() {
        let cap = LuaCodeModeCapability;
        assert_eq!(cap.id(), "lua_code_mode");
        assert_eq!(cap.risk_level(), RiskLevel::High);
        assert_eq!(cap.dependencies(), vec!["lua"]);
        assert_eq!(cap.category(), Some("Execution"));
        assert!(cap.system_prompt_addition().is_some());
    }

    #[test]
    fn hides_eligible_tools_keeps_lua_and_essential() {
        let hook = HideCodeModeToolsHook {
            keep_visible: Vec::new(),
            full_schemas: false,
        };
        let defs = vec![
            builtin("lua", ToolPolicy::Auto, ToolHints::default()),
            builtin("bash", ToolPolicy::Auto, ToolHints::default()),
            builtin("add", ToolPolicy::Auto, ToolHints::default()),
            builtin(
                "delete_file",
                ToolPolicy::Auto,
                ToolHints::default().with_destructive(true),
            ),
            builtin(
                "transfer_funds",
                ToolPolicy::RequiresApproval,
                ToolHints::default(),
            ),
            ToolDefinition::ClientSide(ClientSideTool {
                name: "pick_file".to_string(),
                display_name: None,
                description: "client".to_string(),
                parameters: json!({ "type": "object" }),
                category: None,
                deferrable: DeferrablePolicy::default(),
                hints: ToolHints::default(),
                full_parameters: None,
            }),
        ];

        let transformed = hook.transform(defs);
        let kept = names(&transformed);
        // `add` is the only eligible tool → hidden. Everything else stays.
        assert!(kept.contains(&"lua"));
        assert!(kept.contains(&"bash"));
        assert!(kept.contains(&"delete_file"));
        assert!(kept.contains(&"transfer_funds"));
        assert!(kept.contains(&"pick_file"));
        assert!(!kept.contains(&"add"), "eligible tool must be hidden");
    }

    #[test]
    fn grafts_catalog_onto_lua_description() {
        let hook = HideCodeModeToolsHook {
            keep_visible: Vec::new(),
            full_schemas: false,
        };
        let add = ToolDefinition::Builtin(BuiltinTool {
            name: "add".to_string(),
            display_name: None,
            description: "Add two numbers together and return the result.".to_string(),
            parameters: json!({
                "type": "object",
                "properties": { "a": { "type": "number" }, "b": { "type": "number" } },
                "required": ["a", "b"],
            }),
            policy: ToolPolicy::Auto,
            category: None,
            deferrable: DeferrablePolicy::default(),
            hints: ToolHints::default(),
            full_parameters: None,
        });
        let defs = vec![builtin("lua", ToolPolicy::Auto, ToolHints::default()), add];

        let kept = hook.transform(defs);
        // `add` is gone from the model's tool list...
        assert_eq!(names(&kept), vec!["lua"]);
        // ...but discoverable from the lua tool description with typed arguments.
        let lua_desc = kept[0].description();
        assert!(lua_desc.contains("tools.<name>"), "{lua_desc}");
        assert!(
            lua_desc.contains("- add(a: number, b: number)"),
            "{lua_desc}"
        );
        assert!(
            lua_desc.contains("Add two numbers together"),
            "catalog should carry the description: {lua_desc}",
        );
        // Default mode does not embed the full JSON schema.
        assert!(!lua_desc.contains("schema:"), "{lua_desc}");
    }

    #[test]
    fn full_schemas_embeds_complete_schema() {
        let hook = HideCodeModeToolsHook {
            keep_visible: Vec::new(),
            full_schemas: true,
        };
        let add = ToolDefinition::Builtin(BuiltinTool {
            name: "add".to_string(),
            display_name: None,
            description: "Add.".to_string(),
            parameters: json!({
                "type": "object",
                "properties": {
                    "a": { "type": "number" },
                    "b": { "type": "number" },
                    "human_intent": { "type": "string" }
                },
                "required": ["a", "b"],
            }),
            policy: ToolPolicy::Auto,
            category: None,
            deferrable: DeferrablePolicy::default(),
            hints: ToolHints::default(),
            full_parameters: None,
        });
        let kept = hook.transform(vec![
            builtin("lua", ToolPolicy::Auto, ToolHints::default()),
            add,
        ]);
        let lua_desc = kept[0].description();
        assert!(lua_desc.contains("schema:"), "{lua_desc}");
        assert!(lua_desc.contains("\"properties\""), "{lua_desc}");
        // The synthetic human_intent arg is stripped from both signature & schema.
        assert!(!lua_desc.contains("human_intent"), "{lua_desc}");
    }

    #[test]
    fn typed_signature_handles_optional_and_union_types() {
        let sig = typed_signature(&json!({
            "type": "object",
            "properties": {
                "path": { "type": "string" },
                "limit": { "type": ["integer", "null"] },
                "mode": { "enum": ["a", "b"] }
            },
            "required": ["path"],
        }));
        assert!(sig.starts_with("path: string"), "{sig}");
        assert!(sig.contains("limit?: integer|null"), "{sig}");
        assert!(sig.contains("mode?: enum"), "{sig}");
    }

    #[test]
    fn keep_visible_overrides_hiding() {
        let hook = HideCodeModeToolsHook {
            keep_visible: vec!["add".to_string()],
            full_schemas: false,
        };
        let defs = vec![
            builtin("add", ToolPolicy::Auto, ToolHints::default()),
            builtin("multiply", ToolPolicy::Auto, ToolHints::default()),
        ];
        let transformed = hook.transform(defs);
        let kept = names(&transformed);
        assert!(kept.contains(&"add"), "keep_visible must retain the tool");
        assert!(!kept.contains(&"multiply"));
    }

    #[test]
    fn config_aware_hook_reads_keep_visible() {
        let cap = LuaCodeModeCapability;
        let hook = cap
            .tool_definition_hooks_with_config(&json!({ "keep_visible": ["multiply"] }))
            .pop()
            .unwrap();
        let defs = vec![
            builtin("add", ToolPolicy::Auto, ToolHints::default()),
            builtin("multiply", ToolPolicy::Auto, ToolHints::default()),
        ];
        let transformed = hook.transform(defs);
        let kept = names(&transformed);
        assert_eq!(kept, vec!["multiply"]);
    }

    #[test]
    fn validate_config_rejects_bad_keep_visible() {
        let cap = LuaCodeModeCapability;
        assert!(
            cap.validate_config(&json!({ "keep_visible": ["ok"] }))
                .is_ok()
        );
        assert!(cap.validate_config(&json!({})).is_ok());
        assert!(cap.validate_config(&Value::Null).is_ok());
        assert!(
            cap.validate_config(&json!({ "keep_visible": "nope" }))
                .is_err()
        );
        assert!(
            cap.validate_config(&json!({ "keep_visible": [1, 2] }))
                .is_err()
        );
        assert!(
            cap.validate_config(&json!({ "full_schemas": true }))
                .is_ok()
        );
        assert!(
            cap.validate_config(&json!({ "full_schemas": "yes" }))
                .is_err()
        );
        // Closed contract: unknown keys are rejected (matches config_schema's
        // additionalProperties: false).
        assert!(
            cap.validate_config(&json!({ "nope": 1 })).is_err(),
            "unknown config keys must be rejected"
        );
    }

    // Prompt-budget ratchet (specs/capabilities.md → prompt size). Lowering is
    // always fine; raising requires an explicit bump + justification.
    #[test]
    fn system_prompt_within_budget() {
        let cap = LuaCodeModeCapability;
        let prompt = cap.system_prompt_addition().unwrap();
        assert!(
            prompt.len() <= 500,
            "lua_code_mode prompt grew to {} bytes (budget 500)",
            prompt.len()
        );
    }
}